我是C ++类和对象的初学者。我想定义函子,并根据“ corv”类的构造值的类型将其定义为具有不同的功能。 ab
函子可用于int
,ch
函子可用于char
,re
函子可用于string
。
下面是我的代码:
#include <iostream>
using std::string;
using std::cout;
// "ab" functor
class ab {
private:
int val;
public:
ab(int v) : val(v) {}
void operator()() const;
};
// "ch" functor
class ch {
private:
char val;
public:
ch(char v) : val(v) {}
void operator()() const;
};
// "re" functor
class re {
private:
string val;
public:
re(string v) : val(v) {}
void operator()() const;
};
// corv class
class corv {
private:
string t;
int intV;
char charV;
string stringV;
public:
// constructor for "int" value
corv(int v) {
// only "ab" functor available
intV = v;
t = "int";
void ab::operator()() const {
cout << "ab: " << val << endl;
}
void ch::operator()() const {
return;
}
void re::operator()() const {
return;
}
}
// constructor for "char" value
corv(char v) {
// only "ch" functor available
charV = v;
t = "char";
void ab::operator()() const {
return;
}
void ch::operator()() const {
cout << "ch: " << val << endl;
}
void re::operator()() const {
return;
}
}
// constructor for "string" value
corv(string v) {
// only "re" functor available
stringV = v;
t = "string";
void ab::operator()() const {
return;
}
void ch::operator()() const {
return;
}
void re::operator() const {
cout << "re: " << val << endl;
}
}
};
这些错误
test.cpp: In constructor 'corv::corv(int)':
test.cpp:39:28: error: qualified-id in declaration before '(' token
void ab::operator()() const {
^
test.cpp:42:28: error: qualified-id in declaration before '(' token
void ch::operator()() const {
^
test.cpp:45:28: error: qualified-id in declaration before '(' token
void re::operator()() const {
^
test.cpp: In constructor 'corv::corv(char)':
test.cpp:52:28: error: qualified-id in declaration before '(' token
void ab::operator()() const {
^
test.cpp:55:28: error: qualified-id in declaration before '(' token
void ch::operator()() const {
^
test.cpp:58:28: error: qualified-id in declaration before '(' token
void re::operator()() const {
^
test.cpp: In constructor 'corv::corv(std::__cxx11::string)':
test.cpp:65:28: error: qualified-id in declaration before '(' token
void ab::operator()() const {
^
test.cpp:68:28: error: qualified-id in declaration before '(' token
void ch::operator()() const {
^
test.cpp:71:29: error: qualified-id in declaration before 'const'
void re::operator() const {
^~~~~
发生过。
为什么?以及如何解决这个问题?