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’
0xCursor
2,2424 gold badges15 silver badges33 bronze badges
asked Jul 5, 2013 at 6:02
20
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.
answered Sep 23, 2014 at 19:50
asdeasde
2312 silver badges3 bronze badges
1
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).
Adrian Mole
49.5k155 gold badges49 silver badges79 bronze badges
answered Aug 23, 2018 at 22:01
Kemin ZhouKemin Zhou
6,0751 gold badge47 silver badges55 bronze badges
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.
answered Aug 20, 2020 at 15:37
I had a similar problem in VSCode after copy/pasting *.h and *.cpp files from a base folder into a subfolder.
The issue was that one file in the base folder was referencing one *.h file, that has been moved into the subfolder.
I could resolve the issue by explicitly writing the relative path w.r.t. the base folder in each include statement, e.g.:
- for a file in the SUBfolder that includes
base_header.h
from the BASE folder:#include "../base_header.h"
- for a file in the BASE folder that includes
sub_header.h
from the SUBfolder:#include "subfolder/sub_header.h"
I did this for each header, and had no issues afterwards.
Another issue was strange behaviour due to a duplicate import, so #pragma once
in the header file might also help.
answered Mar 10 at 14:00
1
Did you remember to include the closing brace in main?
#include <iostream>
#include "Attack.h"
using namespace std;
int main()
{
Attack attackObj;
attackObj.printShiz();
}
answered Jul 5, 2013 at 6:08
NorwæNorwæ
1,5458 silver badges18 bronze badges
1
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?)
answered Jul 5, 2013 at 8:22
Stefano FalascaStefano Falasca
8,7772 gold badges17 silver badges24 bronze badges
2
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…
answered Nov 24, 2014 at 22:40
normypnormyp
1741 silver badge11 bronze badges
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.
answered Nov 20, 2015 at 12:07
vespertovesperto
7641 gold badge5 silver badges23 bronze badges
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.
answered Feb 27, 2020 at 7:36
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.
answered Aug 24, 2020 at 9:30
minTwinminTwin
1,1212 gold badges19 silver badges33 bronze badges
Loading
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
00 : 29
‘class hardwareserial’ has no member named ‘printin’; did you mean ‘println’ ?Arduino programming
00 : 42
‘class TimeAlarm class’ has no member named ‘alarm repeat’; did you mean ‘alarmRepeat’ ? Arduino
01 : 12
iOS : XCode 6 Framework issue: Module [framework] has no member named [class]
15 : 44
Class Usb has no member named ctrl_Req usb HOST Fix Bypass A5 devices
03 : 07
‘class MPU6050’ has no member named ‘begin’ error and solution!
03 : 06
class TMRpcm’ has no member named ‘startRecording’ error, arduino uno, nano and lower
Comments
-
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’
-
The code looks fine. Maybe it’s trying to use an older version of the header.
-
Sometimes a «Rebuild All» fix everything.
-
Just tried it, getting the same error :/
-
You should post some code that reproduces the problem. The code you posted looks fine.
-
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
-
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.
-
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.
-
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.
-
@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
-
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 runmain.exe
-
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.
-
what windows version do you use?
-
I am currently using Windows 7
-
well goto start -> run -> type cmd.exe and enter then follow my previous comment.
-
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.»
-
so find
mingw32-g++.exe
and use its absolute path to call (likec:somefoldermingw32-g++.exe main.cpp class.cpp -o main.exe
) -
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!
-
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
-
-
Yes that is there, I just accidentally left it out of the post.
-
The toolchain I’m using is mingw32-g++.exe
-
If he «forgot to compile Class.cpp» that would give an undefined reference link error, not the compilation error shown
-
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.
Recents
Related
- Forum
- General C++ Programming
- Class has no member named function
Class has no member named function
Hi guys,
This error is really annoying me but its late and i’ve been programming all day so I might be missing something stupid.
I keep getting an error saying ui.h:30: error: ‘class BTree<Word>’ has no member named ‘prntInOrder’
here is my ui.h
|
|
ui.cpp
|
|
and part of my btree.h
|
|
As you can see the printInOrder() function is there so would it not see it?
tree.printInOrder();
(no parameters)
Your function is:
void printInOrder(Node<T>* node)
(1 parameter)
You need to give it a parameter.
EDIT: crap, just saw the other overload, nevermind…
Last edited on
You should show the whole error message and the place in your code where the function is called.
ui.h: In member function ‘void UI::go(std::string)’:
ui.h:30: error: ‘class BTree<Word>’ has no member named ‘printInOrder’
ui.h:30 doesnt exist but the place that im calling printInOrder is line 14 in ui.cpp
What is tree in this code?
void UI::go(string filename)
{
if (!load(filename))
return;
tree.printInOrder();
}
BTree<Word> tree;
as shown in line 15 of ui.h
I do not see the definition of tree. So your presented code is not adequate to the proiblem.
> ui.h: In member function ‘void UI::go(std::string)’:
> ui.h:30: error: ‘class BTree<Word>’ has no member named ‘printInOrder’
> ui.h:30 doesnt exist …
Do uou have more than one file ui.h?
Are you using an IDE of some kind, and compiling the code from within the IDE?
If so, perhaps the files that you think are being compiled are not the files that are actually compiled.
Im compiling with g++ on the console using a makefile and I only have 1 ui.h
Makefile
|
|
The line 30 (which doesn’t exist) may come from your template class, the compiler generates the class after the template and in this case the generated class is probably placed into your ui.h file (since there is where you use the template, line 15). Have you tested the template separately from ui.h?
Can we also see your node specification?
node.h
|
|
btree.h line 13. I don’t think that’s the issue, however there is a ; after the { } of the constructor.
Topic archived. No new replies allowed.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
- Pick a username
- Email Address
- Password
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account