Hi, I can't for the life of me figure out what I am doing wrong when I try and get vectors working. Here is my test program:
It all seems perfect to me, but no matter how I implement vectors it always complains on the following lines:
The error I get from gcc is:
It's almost like it's not even considering Vdata as an instance, what am I doing wrong? All the tutorials and example codes I can find do it the same way. I also tried a test with an integer vector instead of a struct, got the same problem.
Thanks in advance
Code:
#include <iostream>
#include <vector>
using namespace std;
struct data{
int location,hp;
data(int x,int y, int hits){
location = x<<8+y;
hp = hits;
}
void showData(){
cout << endl <<
"location: " << getX() << "," << getY() << endl <<
"hp: " << hp << endl << endl;
}
int getX(){
return location >> 8;
}
int getY(){
return location << 24 >> 24;
}
};
vector<data> Vdata;
Vdata.push_back(data(4,7,2)); //updated this line
Vdata.push_back(data(9,3,1)); //updated this line
Vdata.push_back(data(2,11,7)); //updated this line
int main(){
for(int i = 0;i < Vdata.size();i ++){
Vdata[i].showData();
}
return 0;
}
It all seems perfect to me, but no matter how I implement vectors it always complains on the following lines:
Code:
Vdata.push_back(4,7,2); //note: I have updated these 3 lines (see above), I still get the error involving the "."
Vdata.push_back(9,3,1);
Vdata.push_back(2,11,7);
expected constructor, destructor, or type conversion before '.' token expected `,' or `;' before '.' token
It's almost like it's not even considering Vdata as an instance, what am I doing wrong? All the tutorials and example codes I can find do it the same way. I also tried a test with an integer vector instead of a struct, got the same problem.
Thanks in advance
Last edited: