request for member which is of non-class type

2020-07-10 07:22发布

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

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

};

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

3条回答
聊天终结者
2楼-- · 2020-07-10 07:33

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<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()'"

the error is because the compiler consider myList() as a function prototype

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.

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 tutorial explain 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.

查看更多
够拽才男人
3楼-- · 2020-07-10 07:57

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 prototype not a variable declaration.

查看更多
Evening l夕情丶
4楼-- · 2020-07-10 07:59

This line does not do what you think:

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

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.

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

查看更多
登录 后发表回答