I want to make a static vector in a class and I want to resize the vectors when they are created. I’m trying to do this in the constructor or in the main function. But I can’t get it to work. The problem is that I can’t call a function of the vector class on this way.
This is what I have now:
#include <vector>
using namespace std;
class test
{
public:
static vector<int> testvec;
test();
};
test::test() //Not static
{
test::testvec.resize(0); //Try 1
}
vector<int> test::testvec.resize(0); //Try 2
int main()
{
test::testvec.resize(0); //Try 3
test testclass;
system("pause");
return false;
}
I need to handle all the data on the vector in every object, that is why I want to make the vector static.
Can someone help me with this?
Thanks!
Edit: grammer. Every method that I have tried gives a compile error.
When you declare a static member attribute you also need to define it:
You can optionally use a size to the constructor, which would avoid the need to resize:
But you can still call resize from
mainif you prefer: