C++ 请求非类类型的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16364116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
request for member which is of non-class type
提问by sniperu
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
the code which gives me this error is
给我这个错误的代码是
Keyword aux();
aux.put_tag(word);
aux.put_site(site);
I must mention that word and site are char *
type
我必须提到这个词和网站是char *
类型
Now, my Keyword class definition is this one:
现在,我的 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);
}
};
Thank you very much!
非常感谢!
Update
更新
By modifying
通过修改
Keyword aux();
aux.put_tag(word);
aux.put_site(site);
in
在
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
回答by Andy Prowl
This line does not do what you think:
这条线不会做你认为的那样:
Keyword aux();
Is declaring a functioncalled aux
that takes no arguments and returns a Keyword
. You most likely meant to write (without the parentheses):
正在声明一个调用的函数aux
,该函数不接受任何参数并返回一个Keyword
. 您很可能打算写(不带括号):
Keyword aux;
Which declares an objectof type Keyword
.
它声明了一个类型的对象Keyword
。
UPDATE:
更新:
Concerning the next error you are getting, this is because you have a declarationof 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.
关于您遇到的下一个错误,这是因为您有类的构造函数和析构函数的声明,但没有定义。事实上,您得到的错误来自链接器,而不是来自编译器。
To provide a trivial definition of your constructor and destructor, change this:
要提供构造函数和析构函数的简单定义,请更改以下内容:
Keyword();
~Keyword();
Into this:
进入这个:
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).
或者,只要这些成员函数什么都不做,就完全忽略它们——编译器会为你生成它们(除非你添加一些其他用户声明的构造函数,因为构造函数涉及什么)。
回答by john
Not this
不是这个
Keyword aux();
aux.put_tag(word);
aux.put_site(site);
but this
但是这个
Keyword aux;
aux.put_tag(word);
aux.put_site(site);
In your version Keyword aux();
is a function prototypenot a variable declaration.
在您的版本中Keyword aux();
是函数原型而不是变量声明。
回答by newComer
I had the same problem when I type the following code into my main function, I have a List.h and List.cpp files contains my List class.
当我在主函数中键入以下代码时遇到了同样的问题,我有一个 List.h 和 List.cpp 文件包含我的 List 类。
List<int,int> myList();
bool x=myList.isEmpty();
I get an error of "request for member 'isEmpty' in 'myList', which is of non-class type 'List()'"
我收到错误消息“请求‘myList’中的成员‘isEmpty’,这是非类类型‘List()’”
the error is because the compiler consider myList() as a function prototype
错误是因为编译器将 myList() 视为函数原型
when I correct the code to be
当我更正代码时
List<int,int> myList;
bool x=myList.isEmpty();
I got the error "undefined reference to `List::List()" and a couple of similar errors for the destructor.
我收到错误“对`List::List() 的未定义引用”以及析构函数的几个类似错误。
Further inspection of my code and answers in this page I found that I have to include my List.cpp file in the main.cpp however I included List.h in my List.cpp file but it seems that this information has to be told to the main file as well. further reading for this tutorialexplain why ,if I compile the project without including List.cpp it will compile fine because the List.h file has the prototype but it would fail in the linker stage because the linker would be unable to resolve the call to List() to a specific function.
进一步检查此页面中的代码和答案,我发现我必须将 List.cpp 文件包含在 main.cpp 中,但是我在 List.cpp 文件中包含了 List.h,但似乎必须将这些信息告知主文件也是如此。本教程的进一步阅读解释了为什么,如果我在不包含 List.cpp 的情况下编译项目,它将编译正常,因为 List.h 文件具有原型,但它会在链接器阶段失败,因为链接器将无法解析对List() 到一个特定的函数。