请求非类类型的成员

request for member which is of non-class type

本文关键字:成员 类型 请求      更新时间:2023-10-16

我得到了这个错误,我不能自己解决

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 *类型

现在,我的Keyword类定义是这样的:
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()的调用到特定函数。