请求非类类型的成员
request for member which is of non-class type
我得到了这个错误,我不能自己解决
source.cpp:85:8: error: request for member ‘put_tag’ in ‘aux’, which is of non-class type ‘Keyword()’
source.cpp:86:8: error: request for member ‘put_site’ in ‘aux’, which is of non-class type ‘Keyword()’
make: *** [source.o] Error 1
给出这个错误的代码是
Keyword aux();
aux.put_tag(word);
aux.put_site(site);
我必须提到这个词和网站是char *
类型
class Keyword{
private:
std::string tag;
Stack<std::string> weblist;
public:
Keyword();
~Keyword();
void put_tag(std::string word)
{
tag = word;
}
void put_site(std::string site)
{
weblist.push(site);
}
};
非常感谢!
<标题> 更新通过修改
Keyword aux();
aux.put_tag(word);
aux.put_site(site);
Keyword aux;
aux.put_tag(word);
aux.put_site(site);
i got this error:
source.o: In function `Algorithm::indexSite(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
source.cpp:(.text+0x2c6): undefined reference to `Keyword::Keyword()'
source.cpp:(.text+0x369): undefined reference to `Keyword::~Keyword()'
source.cpp:(.text+0x4a8): undefined reference to `Keyword::~Keyword()'
source.o: In function `Keyword::put_site(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
source.cpp:(.text._ZN7Keyword8put_siteESs[Keyword::put_site(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)]+0x2a): undefined reference to `Stack<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::push(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
make: *** [tema3] Error 1
标题>这一行不是你想的那样:
Keyword aux();
声明一个名为aux
的函数,该函数不接受参数并返回Keyword
。你很可能想写(没有括号):
Keyword aux;
声明了一个Keyword
类型的对象。
更新:
关于你得到的下一个错误,这是因为你有一个声明的构造函数和析构函数,但没有定义。实际上,您得到的错误来自链接器,而不是编译器。
为构造函数和析构函数提供一个简单的定义,修改如下:
Keyword();
~Keyword();
这:
Keyword() { }
~Keyword() { }
或者,只要这些成员函数不做任何事情,就完全省略它们——编译器将为您生成它们(除非您添加了一些其他用户声明的构造函数,因为与构造函数有关)。
Not this
Keyword aux();
aux.put_tag(word);
aux.put_site(site);
但这
Keyword aux;
aux.put_tag(word);
aux.put_site(site);
在你的版本Keyword aux();
是一个函数原型不是一个变量声明。
当我在main函数中输入以下代码时,我遇到了同样的问题,我有一个List.h和List.cpp文件包含我的List类
List<int,int> myList();
bool x=myList.isEmpty();
我得到一个错误"请求成员'isEmpty'在'myList',这是非类类型'List()'"
错误是因为编译器认为myList()是一个函数原型
当我将代码更正为
时List<int,int> myList;
bool x=myList.isEmpty();
我得到了错误"对' List::List()的未定义引用"和析构函数的几个类似错误。
进一步检查我的代码和答案在这个页面我发现,我必须包括我的List.cpp文件在main.cpp然而,我包括List.h在我的List.cpp文件,但似乎这个信息必须被告知主文件为好。本教程的进一步阅读解释了为什么,如果我编译不包含List.cpp的项目,它将编译良好,因为List.h文件具有原型,但它将在链接器阶段失败,因为链接器将无法解析对List()的调用到特定函数。
- 根据模板类型选择类模板的成员类型?
- 函数模板签名中忽略的成员类型def 的访问说明符
- 在C++如何从数组中提取成员并返回成员类型的数组?
- C++从成员类型中扣除类型的功能模板?
- 无法初始化以 std::byte 作为成员类型的位字段
- 构造函数可以更改默认成员类型吗?
- 使用相同方法但不同成员类型构建类的最佳方法
- 允许通过指向方法的成员参数的指针来推断模板参数所指向的成员类型
- 使用 std::条件根据模板参数选择成员类型
- CRTP:如何推断要用作返回类型的成员类型?
- 对成员类型的成员方法使用 std::result_of<>
- C :可以从类及其受保护的成员类型继承可以继承吗?
- 如何在不指定不必要的模板参数的情况下使用模板类的成员类型
- 基类数据成员类型取决于派生类
- C 多态性:允许模棱两可的成员类型
- 使用使用成员类型别名的构造函数来推论类模板参数
- 获取与在模板参数中传递的函数成员类型相同的类
- 如果静态成员未初始化并且成员类型是类本身,该怎么办?
- boost::bind 无法绑定到纯虚拟基类中定义的非静态函数模板成员类型
- 将联合强制转换为其成员类型之一