request for member which is of non-class type

62,254

Solution 1

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

Solution 2

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.

Share:
62,254
sniperu
Author by

sniperu

Updated on December 03, 2020

Comments

  • sniperu
    sniperu over 3 years

    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
    
    • Some programmer dude
      Some programmer dude about 11 years
      Read about the most vexing parse.
    • john
      john about 11 years
      @JoachimPileborg Not really the same thing is it? The OP mistake was simpler than the one in the article linked to.
    • Andy Prowl
      Andy Prowl about 11 years
      @john: Indeed. Technically, this is not MVP, because there is no ambiguity. This just cannot be interpreted as the declaration of an object.
    • Gunnar
      Gunnar over 10 years
      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!
  • sniperu
    sniperu about 11 years
    by removing the constructor and destructor or by replacing with your answer i still get the same error
  • Andy Prowl
    Andy Prowl about 11 years
    @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
    Andy Prowl about 11 years
    @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.
  • sniperu
    sniperu about 11 years
    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
    sniperu about 11 years
    but before, i was linking the stack.cpp in my makefile
  • Andy Prowl
    Andy Prowl about 11 years
    @sniperu: Don't do that. .cpp files are not meant to be #included. Just move the definition of Stack's member functions to stack.h
  • Andy Prowl
    Andy Prowl about 11 years
    @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
  • sniperu
    sniperu about 11 years
    I have done that and no errors were founded. Thank you very, very much!
  • Andy Prowl
    Andy Prowl about 11 years
    @sniperu: You're welcome. Please, consider marking the answer as accepted if this solves your problem :)
  • Chris
    Chris almost 4 years
    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?