C++ 指向绑定函数的指针只能用于调用该函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9343148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A pointer to a bound function may only be used to call the function
提问by Kardsen
I'm working on a homework assignment for my C++class and have ran across a problem that I cannot figure out what I am doing wrong.
我正在为我的C++课程做家庭作业,但遇到了一个问题,我无法弄清楚我做错了什么。
Just to note, the separation of the files is necessary and I realize this would be much easier if I just made a structure AttackStyles
inside the main
and forgo the additional class file altogether.
需要注意的是,文件的分离是必要的,我意识到如果我只是在AttackStyles
里面创建一个结构main
并完全放弃额外的类文件,这会容易得多。
The base of my problem is that I cannot seem to be able to loop through an array of classes and pull out base data. Here is the code:
我的问题的根源是我似乎无法遍历类数组并提取基本数据。这是代码:
// 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;
}
My question is why do I get the error:
我的问题是为什么会出现错误:
"a pointer to a bound function may only be used to call the function"
on both ptrAsa->getStyleId
and ptrAsa->getStyleName
?
两个ptrAsa->getStyleId
和ptrAsa->getStyleName
?
I cannot figure out what is wrong with this!
我无法弄清楚这有什么问题!
回答by Naveen
You are missing ()
around the function calls. It should be ptrAsa->getStyleId()
.
您缺少()
函数调用。应该是ptrAsa->getStyleId()
。
回答by Captain Giraffe
You are missing parenthesis on both calls, it should be
您在两个调用中都缺少括号,应该是
ptrAsa->getStyleId()
to call the function.
来调用函数。
ptrAsa->getStyleId
is used to refer to a member value / attribute.
用于引用成员值/属性。
回答by Rob?
You need to invoke the function, not merely reference it:
您需要调用该函数,而不仅仅是引用它:
std::cout << "Style Id:\t" << ptrAsa->getStyleId() << "\n";
std::cout << "Style Name:\t" << ptrAsa->getStyleName() << "\n";