Pracuję nad zadaniem domowym dla mojej klasy C++ i natknąłem się na problem, że nie mogę zrozumieć, co robię źle.Wskaźnik do powiązanej funkcji może być używany tylko do wywoływania funkcji
Wystarczy zauważyć, że oddzielenie plików jest konieczne i zdaję sobie sprawę, że byłoby to o wiele łatwiejsze, gdybym właśnie utworzył strukturę AttackStyles
wewnątrz main
i zrezygnowałem z dodatkowego pliku klasy.
Podstawą mojego problemu jest to, że nie mogę przechodzić przez tablicę klas i wyciągać danych podstawowych. Oto kod:
// AttackStyles.h
#ifndef ATTACKSTYLES_H
#define ATTACKSTYLES_H
#include <iostream>
#include <string>
using namespace std;
class AttackStyles
{
private:
int styleId;
string styleName;
public:
// Constructors
AttackStyles(); // default
AttackStyles(int, string);
// Destructor
~AttackStyles();
// Mutators
void setStyleId(int);
void setStyleName(string);
// Accessors
int getStyleId();
string getStyleName();
// Functions
};
#endif
/////////////////////////////////////////////////////////
// AttackStyles.cpp
#include <iostream>
#include <string>
#include "AttackStyles.h"
using namespace std;
// Default Constructor
AttackStyles::AttackStyles()
{}
// Overloaded Constructor
AttackStyles::AttackStyles(int i, string n)
{
setStyleId(i);
setStyleName(n);
}
// Destructor
AttackStyles::~AttackStyles()
{}
// Mutator
void AttackStyles::setStyleId(int i)
{
styleId = i;
}
void AttackStyles::setStyleName(string n)
{
styleName = n;
}
// Accessors
int 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 array
AttackStyles *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;
}
Moje pytanie brzmi, dlaczego pojawia się błąd:
"a pointer to a bound function may only be used to call the function"
zarówno ptrAsa->getStyleId
i ptrAsa->getStyleName
?
Nie mogę zrozumieć, co jest z tym nie tak!
OMG! Teraz czuję się naprawdę głupio. Dzięki chłopaki! – Kardsen