Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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++ 将我的结构作为数组返回到C++;,OPENGL_C++_Arrays_Function_Struct - Fatal编程技术网

C++ 将我的结构作为数组返回到C++;,OPENGL

C++ 将我的结构作为数组返回到C++;,OPENGL,c++,arrays,function,struct,C++,Arrays,Function,Struct,我的功能有问题。我似乎无法让它返回结构的数组 以下是MyApp.h头文件: struct Vertex { glm::vec3 p; glm::vec3 c; }; class CMyApp { public: CMyApp(void); ~CMyApp(void); Vertex[] DrawCircle(int cx, int cy); ... 它在画圈下面加下划线,并“预期为“;” 以下是MyApp.cpp(当然,包括标题): Ver

我的功能有问题。我似乎无法让它返回结构的数组

以下是MyApp.h头文件:

    struct Vertex
{
    glm::vec3 p;
    glm::vec3 c;
};

class CMyApp
{

public:
    CMyApp(void);
    ~CMyApp(void);

    Vertex[] DrawCircle(int cx, int cy);
...
它在画圈下面加下划线,并“预期为“;”

以下是MyApp.cpp(当然,包括标题):

Vertex[]CMyApp::DrawCircle(int-cx,int-cy){
顶点结果[42];
结果[0]={glm::vec3((float)cx,(float)cy,0.0),glm::normalize(glm::vec3(0,0,1))};
对于(int ii=1;ii<21;ii++){
浮点数θ=2.0f*3.1415926f*浮点数(ii)/浮点数(20);
浮动x=0.5*cosf(θ);
浮动y=0.5*sinf(θ);
结果[ii].p=glm::vec3(x,y,0.0);
结果[ii].c=glm::归一化(结果[ii].p);
}
结果[21]={glm::vec3((float)cx,(float)cy,2.0),glm::normalize(glm::vec3(0,0,1.0))};
对于(int ii=22;ii<42;ii++){
浮点数θ=2.0f*3.1415926f*浮点数(ii)/浮点数(20);
浮动x=0.5*cosf(θ);
浮动y=0.5*sinf(θ);
结果[ii].p=glm::vec3(x,y,2.0);
结果[ii].c=glm::归一化(结果[ii].p);
}
返回结果;
}
在函数名的DrawCircle下,此处的下划线与预期“;”相同

如果我删除了数组标记,那么唯一的错误就是return语句。我想以数组tho的形式返回


提前感谢您的帮助。

您无法返回本地数组。在堆栈上分配这样一个数组;当函数返回时,其所有内容可用于其他堆栈变量。如果您在呼叫后使用它,则其内容可能已损坏

所以

是编译器的未定义行为

你应该改用向量。它的move构造函数使它能够高效地返回许多以数组形式组织的结果

std::vector<Vertex> CMyApp::DrawCircle(int cx, int cy) {
    std::vector<Vertex> result;
    result.reserve(42);
    // same content than your original code.
    ...
    return result;
}
您将获得错误消息:

错误:“DrawCircle”声明为返回数组的函数

阵列自由度画圈(int-cx,int-cy)


不能返回本地数组。在堆栈上分配这样一个数组;当函数返回时,其所有内容可用于其他堆栈变量。如果您在呼叫后使用它,则其内容可能已损坏

所以

是编译器的未定义行为

你应该改用向量。它的move构造函数使它能够高效地返回许多以数组形式组织的结果

std::vector<Vertex> CMyApp::DrawCircle(int cx, int cy) {
    std::vector<Vertex> result;
    result.reserve(42);
    // same content than your original code.
    ...
    return result;
}
您将获得错误消息:

错误:“DrawCircle”声明为返回数组的函数

阵列自由度画圈(int-cx,int-cy)

我想以数组tho的形式返回

你不能。C和C++不允许这样做。您可以在C++中做的是返回<代码> STD::vector < /C> >,您应该使用它而不是普通数组。

我想以数组tho的形式返回


你不能。C和C++不允许这样做。您可以在C++中做的是返回一个<代码>:ST::vector < /C> >,您应该使用它代替普通数组。

使用指针并在参数<代码>顶点**函数(int和长度)< /c>中引用引用的长度,或者使用答案中描述的std::vector。使用指针并在参数
顶点*函数(int&length)
中通过引用反馈长度,或者使用答案中描述的std::vector。
std::vector<Vertex> CMyApp::DrawCircle(int cx, int cy) {
    std::vector<Vertex> result;
    result.reserve(42);
    // same content than your original code.
    ...
    return result;
}
class CMyApp
{

public:
    CMyApp(void);
    ~CMyApp(void);

    typedef Vertex ArrayOfVertices[];
    ArrayOfVertices DrawCircle(int cx, int cy);
};