-4

I want to initialize a specific num vector in a struct,

struct pgp
{
    int cnt;
    pgp():cnt(0){}
};

struct MyStruct
{
    vector<pgp> tmop(5);
};

then I get

C++ std::vector<pgp> MyStruct::tmop(error-type)
JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • Please copy-paste (as text!) the *full* and *complete* build output from your [mcve] into the question. – Some programmer dude Jul 03 '21 at 06:34
  • 2
    As for your problem: You can't do inline initialization of members using parentheses, as those are to hard to distinguish from declaring a function. You need to use either curly-braces `{}`, or use "assignment" syntax using `=`. In this specific case you need to use the latter, like in `vector tmop = vector(5);` – Some programmer dude Jul 03 '21 at 06:34

1 Answers1

1

try this

struct pgp
{
    int cnt;
    pgp(){
        cnt = 0;
    }
};

struct MyStruct
{
    MyStruct(): tmop(5) {}
    vector<pgp> tmop;

};

int main()
{
    MyStruct a;
    //a.tmop.resize(5);
    cout<<"tmop size = "<<a.tmop.size()<<endl;
    return 0;
}
Wakil Khan
  • 134
  • 1
  • 10
  • 1
    While `using namespace std;` [is a bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) it's often considered okay to use for short examples. But please don't write answers using [``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). It's a bad habit that should be eradicated. – Some programmer dude Jul 03 '21 at 08:27
  • Sure, I won't use next time. – Wakil Khan Jul 03 '21 at 08:30
  • thanks,but i dont want to resize the vector from outside – Jacks Fengggg Jul 05 '21 at 02:22