I have this code:
WItem.h
#include <vector>
#include <string>
typedef struct iteminfo {
int rowid;
char* item;
int type;
int extra;
int objectid;
} item;
class CItem {
public:
void push(int rowid, char* item, int type, int extra, int objectid);
std::vector<iteminfo> data;
};
WItem.cpp
#include "witem.h"
void CItem::push(int rowid, char* item, int type, int extra, int objectid) {
iteminfo* temp = new iteminfo;
temp->rowid = rowid;
temp->item = item;
temp->type = type;
temp->extra = extra;
temp->objectid = objectid;
this.data.push_back(temp);
}
And I get these errors:
- `data’ is not a type
- request for member of non-aggregate type before ‘.’ token
And I have no idea what is wrong.
this.datais wrong, needs to be either justdataorthis->datadatais a vector ofiteminfoandtempisiteminfo *i.e. a pointer. You don’t need to usenewhere, you should just create the item “on the stack” and then usepush_backto insert a copy of it into your vector.As this is likely to not be C at all, there is no need for the typedef but even more so use
std::stringfor strings notchar *. You are going to get into a lot of mess maintaining these pointers.Preferably do not use
itemboth as a type and a member. It’s legal but will get confusing in your code.Ideally make
dataa private member ofCItem(Incidentally if you are using the class namesitemandCItemjust to reproduce your problem here that’s fine but in real code they are poor class names, choose something more descriptive).