非成员类型的成员请求

非成员类型的成员请求

本文介绍了非成员类型的成员请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个错误,我不能自己解决

i got this error and i am not able to solve by myself

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 * type

I must mention that word and site are char * type

现在,我的Keyword类定义是这样:

Now, my Keyword class definition is this one:

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

in

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


推荐答案

做你认为:

Keyword aux();

正在声明函数 aux 不带参数并返回关键字。您最有可能想写(不带括号):

Is declaring a function called aux that takes no arguments and returns a Keyword. You most likely meant to write (without the parentheses):

Keyword aux;

其中声明 类型 / code>。

Which declares an object of type Keyword.

UPDATE:

这是因为您拥有类的构造函数和析构函数的声明,而不是定义。事实上,你得到的错误来自于链接器,而不是来自编译器。

Concerning the next error you are getting, this is because you have a declaration of the constructor and destructor of your class, but not a definition. In fact, the error you are getting comes from the linker, and not from the compiler.

为你的构造函数提供一个平凡的定义, destructor,change this:

To provide a trivial definition of your constructor and destructor, change this:

Keyword();
~Keyword();

进入

Keyword() { }
~Keyword() { }

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

Or, as long as these member functions do nothing, just omit them at all - the compiler will generate them for you (unless you add some other user-declared constructor, for what concerns the constructor).

这篇关于非成员类型的成员请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:15