Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 静态方法可以';t返回结构类型?(C+;+;)_C++_Methods_Static_Struct_Return - Fatal编程技术网

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>。这是首选。