The class does not have a member named

I have a problem accessing a function from a class with a class object in my main function. I am just trying to create an object for a class and use this object to access the function inside this class .cpp file. I keep getting the error message, and I even created the simplest program to test it, and still get the error message.

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 

.Cpp class:

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

How can I fix this error? Every time I try to access the printShiz() function in the Attack class using an object in my main function, I get an error message and she does not think that this function exists in this class.

Error:

error: there is no member named printShiz in the Attack class

+10
source share
8 answers

I had a similar problem. It turned out I included the old header file with the same name from the old folder. I deleted the old file by changing the #include directive to point to my new file, and everything was fine.

+15
source

In most cases, the problem is due to a mistake on the part of the person. In my case, I used several classes whose names are similar. I added the empty () method to a single class; however, my code called the empty () method from another class. At that moment, the mind is stuck. I ran make clean and redid it, thinking it was some kind of an older version of the header. Leaving for a moment, I immediately discovered this problem. We programmers tend to blame others first. Maybe we should insist that we make mistakes first.

Sometimes I forget to write the latest update to disk and see the correct version of the code, but the compiler sees the wrong version of the code. This situation may be less important for the IDE (I use vi for coding).

+1
source

I could not find the answer to the question, so I tried and was not mistaken.

Compiled as follows:

g ++ -c class.cpp

g ++ -c main.cpp

g ++ -o main class.o main.o

There are no errors, it works well. The gcc version is used for compilation. gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)

0
source

When I had this problem, I had a previously created .o file in the same directory. I deleted this file and the compilation worked fine. Reading that you are trying to create an object may mean that you have a link to an older .o file, and this leads to its failure.

0
source

Did you remember to include the final figure in the main?

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

Try defining functions right in the header

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

and compile. If the compiler does not complain about duplicate definitions, it means that you forgot to compile the Class.cpp file, then you just need to do this (add it to your Makefile / project / solution file ... what tool chain are you using?)

-1
source

I know this is the year, but I ran into this same problem. My problem was that I did not have a constructor in my implementation file. I think the problem here may be the comment mark at the end of the header file after #endif ...

-1
source

Do you have a typo in your .h? I once came across this error when I had a method correctly named in my main, but with a typo in .h / .cpp ("g" versus "q" in the method name, which made it curious). It falls into the category of "copy / paste error".

-1
source

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


All Articles