Class has no member named
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).
Related videos on Youtube
Rapture686
Updated on August 24, 2020Comments
-
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 theAttack
class by using an object in mymain
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 almost 11 yearsThe code looks fine. Maybe it's trying to use an older version of the header.
-
johnchen902 almost 11 yearsSometimes a "Rebuild All" fix everything.
-
Rapture686 almost 11 yearsJust tried it, getting the same error :/
-
juanchopanza almost 11 yearsYou should post some code that reproduces the problem. The code you posted looks fine.
-
qwr almost 11 yearssuggest 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 almost 11 yearsThe 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 almost 11 yearsI'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 almost 11 yearswould 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 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 almost 11 yearsopen 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 runmain.exe
-
Rapture686 almost 11 yearsHow 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 almost 11 yearswhat windows version do you use?
-
Rapture686 almost 11 yearsI am currently using Windows 7
-
Boynux almost 11 yearswell goto start -> run -> type cmd.exe and enter then follow my previous comment.
-
Rapture686 almost 11 yearsI 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 almost 11 yearsso find
mingw32-g++.exe
and use its absolute path to call (likec:\some\folder\mingw32-g++.exe main.cpp class.cpp -o main.exe
) -
Stefano Falasca almost 11 yearsSince 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 almost 11 yearsThanks 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 almost 11 yearsyou may also consider testing your code here: compileonline.com/compile_cpp_online.php
-
Tim Post almost 11 yearsPlease move extended discussions to Stack Overflow Chat. Thank you.
-
-
Rapture686 almost 11 yearsYes that is there, I just accidentally left it out of the post.
-
Rapture686 almost 11 yearsThe toolchain I'm using is mingw32-g++.exe
-
M.M almost 10 yearsIf he "forgot to compile Class.cpp" that would give an undefined reference link error, not the compilation error shown
-
Yiannis Mpourkelis over 7 yearsThis 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.