8

我收到了这个错误,我无法自己解决

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);

我必须提到 word 和 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);

我收到了这个错误:

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
4

3 回答 3

20

这条线不符合你的想法:

Keyword aux();

正在声明一个被调用的函数aux,它不接受任何参数并返回一个Keyword. 您很可能打算写(不带括号):

Keyword aux;

其中声明了一个类型的对象Keyword

更新:

关于您遇到的下一个错误,这是因为您有一个类的构造函数和析构函数的声明,但没有定义。实际上,您得到的错误来自链接器,而不是来自编译器。

要提供构造函数和析构函数的简单定义,请更改以下内容:

Keyword();
~Keyword();

进入这个:

Keyword() { }
~Keyword() { }

或者,只要这些成员函数什么都不做,就完全忽略它们 - 编译器会为你生成它们(除非你添加一些其他用户声明的构造函数,因为构造函数的问题)。

于 2013-05-03T17:00:37.947 回答
4

不是这个

Keyword aux();
aux.put_tag(word);
aux.put_site(site);

但是这个

Keyword aux;
aux.put_tag(word);
aux.put_site(site);

在您的版本Keyword aux();中是函数原型而不是变量声明。

于 2013-05-03T17:01:02.160 回答
0

当我在我的主函数中键入以下代码时,我遇到了同样的问题,我有一个 List.h 并且 List.cpp 文件包含我的 List 类。

List<int,int> myList();
bool x=myList.isEmpty();

我收到“在‘myList’中请求成员‘isEmpty’的错误,它是非类类型‘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() 到一个特定的函数。

于 2016-12-02T20:48:11.233 回答