简体   繁体   English

将我的结构体作为C ++,OPENGL中的数组返回

[英]Return my struct as an array in C++, OPENGL

I have a problem with my function. 我的功能有问题。 I can't seem to make it return an array of a struct. 我似乎无法使其返回结构数组。

Here is the MyApp.h header file: 这是MyApp.h头文件:

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

class CMyApp
{

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

    Vertex[] DrawCircle(int cx, int cy);
...

It underlines the DrawCircle and "expects a ';'". 它强调DrawCircle并“期望';'”。

Here is the MyApp.cpp (Of course, header included): 这是MyApp.cpp(当然,包括标头):

Vertex[] CMyApp::DrawCircle(int cx, int cy) {
    Vertex result[42];

    result[0] = { glm::vec3((float)cx, (float)cy, 0.0), glm::normalize(glm::vec3(0, 0, 1)) };

    for (int ii = 1; ii < 21; ii++) {
        float theta = 2.0f * 3.1415926f * float(ii) / float(20);
        float x = 0.5 * cosf(theta);
        float y = 0.5 * sinf(theta);
        result[ii].p = glm::vec3(x, y, 0.0);
        result[ii].c = glm::normalize(result[ii].p);
    }

    result[21] = { glm::vec3((float)cx, (float)cy, 2.0), glm::normalize(glm::vec3(0, 0, 1.0)) };

    for (int ii = 22; ii < 42; ii++) {
        float theta = 2.0f * 3.1415926f * float(ii) / float(20);
        float x = 0.5 * cosf(theta);
        float y = 0.5 * sinf(theta);
        result[ii].p = glm::vec3(x, y, 2.0);
        result[ii].c = glm::normalize(result[ii].p);
    }

    return result;
}

Same underline here under the function name's DrawCircle for expected ";". 在函数名称的DrawCircle下的相同下划线表示预期的“;”。

If I remove the array marks then the only error is the return statement. 如果删除数组标记,则唯一的错误就是return语句。 I want to return as an array tho. 我想作为数组返回。

Thanks for help in advance. 预先感谢您的帮助。

You cannot return a local array. 您不能返回本地数组。 Such an array is allocated on the stack; 这样的数组被分配在堆栈上。 when the function returns, all its content is available for other stack variables. 函数返回时,其所有内容可用于其他堆栈变量。 If you use it after the call, its content is likely to be corrupted. 如果您在通话后使用它,则其内容可能已损坏。

So 所以

Vertex[] CMyApp::DrawCircle(int cx, int cy) {
    Vertex result[42];
    return result;
}

is undefined behavior for the compiler. 是编译器的未定义行为。

You should use a vector instead. 您应该改用向量。 Its move constructor makes it efficient for returning many results organized as an array. 它的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;
}

Note that if you declare 请注意,如果您声明

class CMyApp
{

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

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

You obtain the error message: 您收到错误信息:

error: 'DrawCircle' declared as function returning an array 错误:“ DrawCircle”声明为函数,返回数组

ArrayOfVertices DrawCircle(int cx, int cy); ArrayOfVertices DrawCircle(int cx,int cy);

I want to return as an array tho. 我想作为数组返回。

You can't. 你不能 C and C++ don't allow it. C和C ++不允许这样做。 What you can do in C++ is returning a std::vector which you should use instead of plain arrays anyway. 在C ++中可以执行的操作是返回一个std::vector ,无论如何应使用它而不是普通数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM