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'
It is just called "stateless". Nothing really special about it. Show activity on this post. There is nothing wrong with a class that has no members; controllers do this very frequently.
1 Answer. Friend function is not a member of the class.
A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object.
No, because the object would be infinitely large (because every Node has as members two other Node objects, which each have as members two other Node objects, which each... well, you get the point).
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With