Reputation: 727
I have a tiny .h file:
#include "stdafx.h"
#ifndef BIGNUM_H
#define BIGNUM_H
#include <vector>
class bignum{
private:
std::vector<int> num;
num.resize(4);
};
#endif
I get the following error messages:
What am I missing?
Upvotes: 3
Views: 2494
Reputation: 227400
You cannot call num.resize(4);
outside of a function. You could use your class' constructor, or a C++11 initialization at the point of declaration.
class bignum
{
private:
std::vector<int> num = std::vector<int>(4); // C++11
};
class bignum
{
bignum() : num(4) {} // C++03 and C++11
private:
std::vector<int> num;
};
Upvotes: 4
Reputation: 15916
You can't call a method on a member variable inside your class declaration. If you want to resize the vector to 4 do so in the class constructor (or in another function but the constructor is by far the best place to do so).
In your cpp file you could do something like:
bignum::bignum() { num.resize(4); }
or:
bignum::bignum(): num(4) {}
The second one calls the vector constructor that takes a size argument. Or you can directly do it in your .h file:
class bignum{
bignum(): num(4) {} // version 1
bignum(): num() { num.resize(4); } // version 2
private:
std::vector<int> num;
};
Upvotes: 11