Kar*_*sen 7 c++ arrays class object
我正在为我的C++课做一个家庭作业,遇到了一个我无法弄清楚我做错了什么的问题.
需要注意的是,文件的分离是必要的,我意识到如果我只是在AttackStyles里面创建了一个结构main并完全放弃了附加的类文件,这会更容易.
我的问题的基础是我似乎无法循环遍历类数组并提取基础数据.这是代码:
// 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;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么我会收到错误:
"a pointer to a bound function may only be used to call the function"
Run Code Online (Sandbox Code Playgroud)
两个ptrAsa->getStyleId和ptrAsa->getStyleName?
我无法弄清楚这有什么问题!
两个电话都缺少括号,应该是
ptrAsa->getStyleId()
Run Code Online (Sandbox Code Playgroud)
调用该函数.
ptrAsa->getStyleId
Run Code Online (Sandbox Code Playgroud)
用于引用成员值/属性.