9

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
sniperu
  • 91
  • 1
  • 1
  • 3
  • 4
    Read about [the most vexing parse](http://en.wikipedia.org/wiki/Most_vexing_parse). – Some programmer dude May 03 '13 at 17:01
  • @JoachimPileborg Not really the same thing is it? The OP mistake was simpler than the one in the article linked to. – john May 03 '13 at 17:03
  • 1
    @john: Indeed. Technically, this is not MVP, because there is no ambiguity. This just cannot be interpreted as the declaration of an object. – Andy Prowl May 03 '13 at 17:06
  • Thanks @Joachim Pileborg, as this was the error I was trying to fix in my case. The question here didn't help, your hint to the article did! – Gunnar Nov 07 '13 at 13:17

3 Answers3

22

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

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • by removing the constructor and destructor or by replacing with your answer i still get the same error – sniperu May 03 '13 at 17:19
  • @sniperu: You probably get the same error for something else than the constructor and destructor. There is likely some other function you forgot to define – Andy Prowl May 03 '13 at 17:20
  • @sniperu: In fact, it looks like you are missing the definition of `Stack::push()`. Did you put that in a header file? If not, see [this Q&A](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – Andy Prowl May 03 '13 at 17:22
  • 1
    I have a stack.h and a stack.cpp, and it seems to work after i have included this #include "stack.cpp" after #include "stack.h" – sniperu May 03 '13 at 17:28
  • but before, i was linking the stack.cpp in my makefile – sniperu May 03 '13 at 17:29
  • @sniperu: Don't do that. `.cpp` files are not meant to be `#include`d. Just move the definition of `Stack`'s member functions to `stack.h` – Andy Prowl May 03 '13 at 17:29
  • @sniperu: Linking `stack.cpp` does not help, because member functions of a class template are instantiated only when a call is found to those functions, and in `stack.cpp` you are not invoking them, while on other translation units where you are invoking them their definition is not visible, so the compiler can't emit code for them. Result: undefined reference. Just put those definitions in `stack.h` – Andy Prowl May 03 '13 at 17:30
  • I have done that and no errors were founded. Thank you very, very much! – sniperu May 03 '13 at 17:36
  • @sniperu: You're welcome. Please, consider marking the answer as accepted if this solves your problem :) – Andy Prowl May 03 '13 at 17:37
4

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.

john
  • 85,011
  • 4
  • 57
  • 81
  • What if you are trying to have that semantic form: you would `like` to write `Keyword aux()` instead of `auto aux = Keyword();` but you `cant` because of some detail. Do you know what that detail would be? – Chris Jul 02 '20 at 13:14
0

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.

sh.e.salh
  • 508
  • 2
  • 5
  • 16