I'm working on a homework assignment for my C++ class and have ran across a problem that I cannot figure out what I am doing wrong.
Just to note, the separation of the files is necessary and I realize this would be much easier if I just made a structure AttackStyles
inside the main
and forgo the additional class file altogether.
The base of my problem is that I cannot seem to be able to loop through an array of classes and pull out base data. Here is the code:
// AttackStyles.h#ifndef ATTACKSTYLES_H#define ATTACKSTYLES_H#include <iostream>#include <string>using namespace std;class AttackStyles{private:int styleId;string styleName;public:// ConstructorsAttackStyles(); // defaultAttackStyles(int, string);// Destructor~AttackStyles();// Mutatorsvoid setStyleId(int);void setStyleName(string); // Accessorsint getStyleId();string getStyleName(); // Functions};#endif/////////////////////////////////////////////////////////// AttackStyles.cpp#include <iostream>#include <string>#include "AttackStyles.h"using namespace std;// Default ConstructorAttackStyles::AttackStyles() {}// Overloaded ConstructorAttackStyles::AttackStyles(int i, string n){setStyleId(i);setStyleName(n);}// DestructorAttackStyles::~AttackStyles() {}// Mutatorvoid AttackStyles::setStyleId(int i){styleId = i;}void AttackStyles::setStyleName(string n){styleName = n;}// Accessorsint AttackStyles::getStyleId(){return styleId;}string AttackStyles::getStyleName(){return styleName;}//////////////////////////////////////////////// main.cpp#include <cstdlib>#include <iostream>#include <string>#include "attackStyles.h"using namespace std;int main(){const int STYLE_COUNT = 3;AttackStyles asa[STYLE_COUNT] = {AttackStyles(1, "First"), AttackStyles(2, "Second"), AttackStyles(3, "Third")};// Pointer for the arrayAttackStyles *ptrAsa = asa;for (int i = 0; i <= 2; i++){cout << "Style Id:\t" << ptrAsa->getStyleId << endl;cout << "Style Name:\t" << ptrAsa->getStyleName << endl;ptrAsa++;}system("PAUSE");return EXIT_SUCCESS;}
My question is why do I get the error:
"a pointer to a bound function may only be used to call the function"
on both ptrAsa->getStyleId
and ptrAsa->getStyleName
?
I cannot figure out what is wrong with this!
Best Answer
You are missing ()
around the function calls. It should be ptrAsa->getStyleId()
.
You are missing parenthesis on both calls, it should be
ptrAsa->getStyleId()
to call the function.
ptrAsa->getStyleId
is used to refer to a member value / attribute.
You need to invoke the function, not merely reference it:
std::cout << "Style Id:\t" << ptrAsa->getStyleId() << "\n";std::cout << "Style Name:\t" << ptrAsa->getStyleName() << "\n";
You are Forgot to put () in last in Your Function(ptrAsa->getStyleId ) Calling with arrow operator.