[c++] Class has no member named

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'

This question is related to c++

The answer is


Maybe I am 6.5 years late. But I'm answering because others maybe searching still now. I faced the same problem and was searching everywhere. But then I realized I had written my code in an empty file. If you create a project and write your code in there, there won't be this error.


I know this is a year old but I just came across it with the same problem. My problem was that I didn't have a constructor in my implementation file. I think the problem here could be the comment marks at the end of the header file after the #endif...


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.


Do you have a typo in your .h? I once came across this error when i had the method properly called in my main, but with a typo in the .h/.cpp (a "g" vs a "q" in the method name, which made it kinda difficult to spot). It falls under the "copy/paste error" category.


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.


Try to define the functions right into the header

 #ifndef ATTACK_H
 #define ATTACK_H

 class Attack {
     public:
         Attack(){};
         void printShiz(){};
     protected:
     private: };

 #endif // ATTACK_H

and to compile. If the compiler doesn't complain about duplicate definitions it means you forgot to compile the Class.cpp file, then you simply need to do it (add it to your Makefile/project/solution... which toolchain are you using?)


The reason that the error is occuring is because all the files are not being recognized as being in the same project directory. The easiest way to fix this is to simply create a new project.

File -> Project -> Console application -> Next -> select C or C++ -> Name the project and select the folder to create the project in -> then click finish.

Then to create the class and header files by clicking New -> Class. Give the class a name and uncheck "Use relative path." Make sure you are creating the class and header file in the same project folder.

After these steps, the left side of the IDE will display the Sources and Headers folders, with main.cpp, theclassname.cpp, and theclassname.h all conviently arranged.


Did you remember to include the closing brace in main?

#include <iostream>
#include "Attack.h"

using namespace std;

int main()
{
  Attack attackObj;
  attackObj.printShiz();
}

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