指向绑定函数的指针只能用于调用该函数
A pointer to a bound function may only be used to call the function
我正在为我的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"
在CCD_ 3和CCD_?
我搞不明白这是怎么回事!
函数调用中缺少()
。应该是ptrAsa->getStyleId()
。
两个调用都缺少括号,应该是
ptrAsa->getStyleId()
调用函数。
ptrAsa->getStyleId
用于引用成员值/属性。
您需要调用函数,而不仅仅是引用它:
std::cout << "Style Id:t" << ptrAsa->getStyleId() << "n";
std::cout << "Style Name:t" << ptrAsa->getStyleName() << "n";
在使用箭头运算符调用函数(ptrAsa->getStyleId)时,忘记将()放在最后。
相关文章:
- (此函数用于计算 SMA).在 FA.exe 中0x00509159引发异常: 0xC0000005:访问冲突读取位置0
- cv2有什么区别.StereoSGBM_create() 和 cv2.StereoBM_create() 函数用于 Op
- 模板 ctor 类型推导不起作用(没有匹配的构造函数用于初始化 ...)与函数<>参数
- Qt中是否有QLabel::setScaledContent等效函数用于QGraphicsScene?
- 递归函数用于计算 n 个数字之和的意外输出
- 替换(或重新实现?)std::函数用于某些统计和测试
- 如何将成员函数用于同一类的不同对象
- 为什么lambda函数用于stl函数,例如sort(),c 中的max_element()函数以两个参数为输入
- 如何在 Cython 中将C++中的模板化函数用于两种类型
- 编译库的C 挂钩函数用于调试
- 在已发布的结构中添加构造函数(用于 memcpy)是否安全?
- 没有匹配的构造函数用于初始化模板化类的构造函数
- C 无匹配函数用于呼叫错误(默认为参考通过)
- 没有匹配函数用于调用 std::transform,未解析的重载函数类型
- 函数用于随机数(C++11)
- 是否有一个函数用于遍历POSIX环境变量
- “没有匹配的构造函数用于初始化”与矢量
- 函数用于C样式字符串和C++std::string
- 有选择地内联函数(用于调试目的)
- 两个模板函数用于两种标准类型