Request for a member that is not in a class

I got this error and I can not solve it 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 that gives me this error,

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

I should mention that the word and site are char * type

Now my keyword class definition is as follows:

 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

Changing

 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 
+6
source share
3 answers

This line does not do what you think:

 Keyword aux(); 

Declares a function called aux that takes no arguments and returns a Keyword . You most likely wanted to write (without parentheses):

 Keyword aux; 

Declaring an object of type Keyword .

UPDATE:

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

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

 Keyword(); ~Keyword(); 

In it:

 Keyword() { } ~Keyword() { } 

Or, while these member functions do nothing, just omit them altogether - the compiler will generate them for you (unless you add any other constructor declared by the user as for the constructor).

+15
source

Not this one

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

but this one

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

In your version of Keyword aux(); it is a function prototype, not a variable declaration.

+3
source

I had the same problem when I injected the following code into my main function, I have List.h and List.cpp files containing my List class.

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

I get an error "request for member" isEmpty "in" myList ", which is not of type" List () ""

the error is due to the compiler treating myList () as a function prototype

when i fix the code

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

I got the error "undefined link to" List :: List () "and a couple of similar errors for the destructor.

Further checking my code and answers on this page I found that I need to include the List.cpp file in the main.cpp file, but I included List.h in my List.cpp file, but it seems that this information should also be reported to the main file . further reading for this tutorial explains why if I compile the project without including List.cpp, it will compile fine, because there is a prototype in the List.h file, but it will not work at the linker stage, because the linker cannot resolve the List call () a specific function.

0
source

Source: https://habr.com/ru/post/944234/


All Articles