C++ 静态方法可以';t返回结构类型?(C+;+;)
我想知道这里哪里做错了:C++ 静态方法可以';t返回结构类型?(C+;+;),c++,methods,static,struct,return,C++,Methods,Static,Struct,Return,我想知道这里哪里做错了: class Grasp { typedef struct { int unique; int intersection; int sets; float alpha; int *covered; int *choosen; }best; static best findSolution(); } 关于.cpp: best Grasp::fin
class Grasp
{
typedef struct
{
int unique;
int intersection;
int sets;
float alpha;
int *covered;
int *choosen;
}best;
static best findSolution();
}
关于.cpp:
best Grasp::findSolution()
{
//it doesn't matter
}
那一行有一个错误:bestGrasp::findSolution()
“best”不命名类型
为什么?best
是嵌套类型,因为它是Grasp
的成员。因此,您需要将退货类型限定为:
Grasp::best Grasp::findSolution()
{
//your code
}
注意返回类型::-)
best
是包含在Grasp
中的typedef。除非它是全局的,否则编译器无法知道它属于那个类。改用grass::best
:
Grasp::best Grasp::findSolution()
{
// ..
}
您不在
Grasp
类的范围内,因此编译器无法看到名为best
的符号。是否尝试将其更改为Grasp::best
?作为旁注,您不需要使用typedef
。您可以在C++中编写<代码>结构>最佳{…} /COD>。这是首选。