c++ - 指向绑定(bind)函数的指针只能用于调用该函数

标签 c++ arrays class object

我正在为我的 C++ 类(class)布置家庭作业,遇到了一个问题,我无法弄清楚我做错了什么。

请注意,文件的分离是必要的,我意识到如果我只是在 main 中创建一个结构 AttackStyles 并放弃额外的,这会容易得多类文件。

我的问题的根源在于我似乎无法遍历类数组并提取基础数据。这是代码:

// 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;
}

我的问题是为什么会出现错误:

  "a pointer to a bound function may only be used to call the function"

ptrAsa->getStyleIdptrAsa->getStyleName 上?

我不知道这有什么问题!

最佳答案

函数调用周围缺少 ()。它应该是 ptrAsa->getStyleId()

关于c++ - 指向绑定(bind)函数的指针只能用于调用该函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9343148/

相关文章:

Java 设计问题 - 向固定类添加功能

jQuery .css 不适用于 p 和 p.class

c++ - 从 64 位应用程序加载 32 位共享库?

c++ - 虚类的多重继承

c++ - 使用 ALUT 时出现错误 LNK2019

c - 在循环中使用 fgets() 获取输入

java - 初始化类

c# - 应用程序域用于什么?

c - 在 C 函数中使用 EOF 读取用户输入并修改数组?

javascript - 在数组json中的单个对象中转换多个相同的键对象