繁体   English   中英

头文件中的C ++ :: GET&SET

[英]C++::GET & SET in a header file

我正在尝试在C ++的头文件中实现get()

例如:这是文件:test.hpp

class A {
public:
A(std::string f, int id):file(f), index(id){};

std::string getFile(){return file;};
int getIndex(){return id;};

private:
  std::string file;
  int index;
};
}

但是然后我遇到了一个问题:

未定义对“ vtable A”的引用。

我该如何进行?

int getIndex(){return id;};

没有名为“ id”的成员,因此此行应为

int getIndex(){return index;};

然后,您的封闭括号过多。 另请注意,花括号后不需要分号。 另外,不修改对象的类成员应始终声明为const,这对于保持一致的概念(应该做什么和不应该做什么)有很大帮助。

这是最终的样子:

#include <string>

class A {
public:
A(std::string f, int id):file(f), index(id){}

std::string getFile() const {return file;}
int getIndex() const {return index;}

private:
  std::string file;
  int index;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM