无法访问矢量类成员

Ste*_*wie 2 c++ vector std dev-c++

我有这个代码:

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;
};
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

我收到这些错误:

  • `data'不是一种类型
  • 在'.'之前请求非聚合类型的成员 代币

我不知道出了什么问题.

Cas*_*Cow 5

  1. this.data是错误的,必须要么只是datathis->data

  2. data是的向量iteminfotempiteminfo *即一个指针.你不需要在new这里使用,你应该只在"堆栈"上创建项目,然后使用push_back它将它的副本插入到矢量中.

  3. 因为这可能根本不是C,所以不需要typedef,但更不需要使用std::string字符串char *.保持这些指针你会陷入很多混乱.

  4. 优选地,不要将item两者用作类型和成员.这是合法的,但会在代码中混淆.

  5. 理想情况下,建立data一个私人成员CItem(顺便提一下,如果你使用的是类名item,CItem只是为了在这里重现你的问题,那很好,但在实际代码中,他们是糟糕的类名,选择更具描述性的东西).