【问题标题】:A pointer to a bound function may only be used to call the function指向绑定函数的指针只能用于调用函数
【发布时间】:2012-03-09 17:34:23
【问题描述】:

我正在为我的 C++ 课程做家庭作业,但遇到了一个我无法弄清楚自己做错了什么的问题。

请注意,文件的分离是必要的,我意识到如果我只是在 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-&gt;getStyleIdptrAsa-&gt;getStyleName?

我不知道这有什么问题!

【问题讨论】:

  • 这是旧的,但不要使用“using namespace std;”在头文件中。它弄乱了它之后包含的每个标头和包含文件的名称空间。

标签: c++ arrays class object


【解决方案1】:

你忘了把 () 放在你的函数(ptrAsa->getStyleId)的最后,用箭头运算符调用。

【讨论】:

    【解决方案2】:

    您需要调用该函数,而不仅仅是引用它:

        std::cout << "Style Id:\t" << ptrAsa->getStyleId() << "\n";
        std::cout << "Style Name:\t" << ptrAsa->getStyleName() << "\n";
    

    【讨论】:

      【解决方案3】:

      两次调用都缺少括号,应该是

      ptrAsa->getStyleId() 
      

      调用函数。

      ptrAsa->getStyleId 
      

      用于引用成员值/属性。

      【讨论】:

      • 天啊!我现在只想爬回我的洞里。非常抱歉这个愚蠢的问题哈哈。
      • @Kardsen 请将最佳答案标记为已接受。
      【解决方案4】:

      您在函数调用周围缺少()。应该是ptrAsa-&gt;getStyleId()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-16
        • 2013-01-18
        • 1970-01-01
        相关资源
        最近更新 更多