Class has no member named

c++
153,187

Solution 1

I had a similar problem. It turned out, I was including an old header file of the same name from an old folder. I deleted the old file changed the #include directive to point to my new file and all was good.

Solution 2

I had similar problem. My header file which included the definition of the class wasn't working. I wasn't able to use the member functions of that class. So i simply copied my class to another header file. Now its working all ok.

Solution 3

Most of the time, the problem is due to some error on the human side. In my case, I was using some classes whose names are similar. I have added the empty() method under one class; however, my code was calling the empty() method from another class. At that moment, the mind was stuck. I was running make clean, and remake thinking that it was some older version of the header got used. After walking away for a moment, I found that problem right away. We programmers tends to blame others first. Maybe we should insist on ourselves to be wrong first.

Sometimes, I forget to write the latest update to disk and looking at the correct version of the code, but the compiler is seeing the wrong version of the code. This situation may be less a issue on IDE (I use vi to do coding).

Share:
153,187

Related videos on Youtube

Rapture686
Author by

Rapture686

Updated on August 24, 2020

Comments

  • Rapture686
    Rapture686 almost 4 years

    I have a problem with accessing a function from a class with the class object in my main function. I am just trying to make the object for the class and use that object to access the function inside that class's .cpp file. I keep getting an error and I even made the simplest program to test it and I still get an error.

    Main:

    #include <iostream>
    #include "Attack.h"
    
    using namespace std;
    
    int main()
    {
        Attack attackObj;
        attackObj.printShiz();
    }
    

    Class header:

    #ifndef ATTACK_H
    #define ATTACK_H
    
    class Attack
    {
        public:
            Attack();
            void printShiz();
        protected:
        private:
    };
    
    #endif // ATTACK_H
    

    Class .cpp:

    #include <iostream>
    #include "Attack.h"
    using namespace std;
    
    Attack::Attack() {
    
    }
    
    void Attack::printShiz() {
        cout << "Test" << endl;
    }
    

    How do I fix this error? Everytime I try to access the printShiz() function in the Attack class by using an object in my main function, I get an error and it doesn't think this function exists within this class.

    Error:

    error: 'class Attack' has no member named 'printShiz'

    • chris
      chris almost 11 years
      The code looks fine. Maybe it's trying to use an older version of the header.
    • johnchen902
      johnchen902 almost 11 years
      Sometimes a "Rebuild All" fix everything.
    • Rapture686
      Rapture686 almost 11 years
      Just tried it, getting the same error :/
    • juanchopanza
      juanchopanza almost 11 years
      You should post some code that reproduces the problem. The code you posted looks fine.
    • qwr
      qwr almost 11 years
      suggest 2 : copy printShiz() and replace all with copied. sometimes what it is written seems equal but when you change to ANsi on notepad++ you see that under code is different. happen when you switch much between keyboard layouts
    • Tony Delroy
      Tony Delroy almost 11 years
      The problem must be with your include path, directories/filenames etc.. Which compiler are you using? If e.g. GCC, then you can use "g++ -E main.cc <other-includes-etc>" to get proprocessor-stage output that will show if the header you expect is being incorporated properly in the translation unit.
    • Rapture686
      Rapture686 almost 11 years
      I'm using Codeblocks with the standard GNU GCC Compiler it came with. I just started coding so I don't know too much about what is wrong.
    • Boynux
      Boynux almost 11 years
      would you please give me exact command you are using to compile your code? I'm afraid that you are not linking class.o object with your code properly.
    • Rapture686
      Rapture686 almost 11 years
      @Boynux I'm really new to the whole coding thing so I dont know exactly what you mean by the command. I know that I'm using Codeblocks with the GNU GCC Compiler. When I go to the compiler settings under toolchain executables, it says the C++ compiler is mingw32-g++.exe
    • Boynux
      Boynux almost 11 years
      open a terminal and witch to your program folder and then issue this command: mingw32-g++.exe main.cpp class.cpp -o main.exe then try to run main.exe
    • Rapture686
      Rapture686 almost 11 years
      How do I do that? As I said I'm VERY new to coding. I just started a couple days ago. I just want to fix whatever is wrong so I can go back to learning how to code.
    • Boynux
      Boynux almost 11 years
      what windows version do you use?
    • Rapture686
      Rapture686 almost 11 years
      I am currently using Windows 7
    • Boynux
      Boynux almost 11 years
      well goto start -> run -> type cmd.exe and enter then follow my previous comment.
    • Rapture686
      Rapture686 almost 11 years
      I navigated to the program's folder and entered the command exactly and got a response: "'ming32-g++.exe' is not recognized as an internal or external command, operable program or batch file."
    • SpongeBobFan
      SpongeBobFan almost 11 years
      so find mingw32-g++.exe and use its absolute path to call (like c:\some\folder\mingw32-g++.exe main.cpp class.cpp -o main.exe)
    • Stefano Falasca
      Stefano Falasca almost 11 years
      Since you say you are new to programming let me give you a suggestion, always include your own headers first (see your main.cpp) so that you make sure that you are including everything you need in other files. Your example is just fine, this is just a suggestion!
    • Rapture686
      Rapture686 almost 11 years
      Thanks for the advice! My current problem is currently keeping me from programming and I have no idea why it's happening so I'm kinda bummed out. Hopefully this problem will be fixed soon
    • Boynux
      Boynux almost 11 years
      you may also consider testing your code here: compileonline.com/compile_cpp_online.php
    • Tim Post
      Tim Post almost 11 years
      Please move extended discussions to Stack Overflow Chat. Thank you.
  • Rapture686
    Rapture686 almost 11 years
    Yes that is there, I just accidentally left it out of the post.
  • Rapture686
    Rapture686 almost 11 years
    The toolchain I'm using is mingw32-g++.exe
  • M.M
    M.M almost 10 years
    If he "forgot to compile Class.cpp" that would give an undefined reference link error, not the compilation error shown
  • Yiannis Mpourkelis
    Yiannis Mpourkelis over 7 years
    This is the correct answer to the question. It happens sometimes. You copy a header file to another path and make changes to it, but because somewhere in your project you include the old header, changes to member functions, variables etc never appear and you get this kind of "strange" errors.