The problem are as followings:
- (
corrected in OP question)myVectoris definedconst myVector.push_back(1);is not in any function body.- (
corrected in OP question) Value passed tomyVector.push_back(1);isintbutvectoris of typestring
Change it as following. See example program working here:
#include "string"
#include "vector"
#include "iostream"
using namespace std;
struct Board {
vector<string> myVector;
void push_back(string val)
{
myVector.push_back(val);
}
void print()
{
for (auto it = myVector.begin(); it != myVector.end(); ++it)
cout << " | " << *it;
}
};
int main()
{
Board b;
b.push_back("Value 1");
b.push_back("Value 2");
b.print();
return 0;
}
UPDATE:
(can you actually use push_back for a vector in a struct without creating an extra function?)
No. structure can have only data members and member functions. but you can use initializer-list to initialize vector as following:
vector<string> myVector{"IVal 1", "IVal 1"};
If you wants to put the initlize value always at the end then use vector.insert() instead of vector.push_back().
2
solved vector definition in struct (c++)