简体   繁体   English

没有使用push_back命名类型

[英]does not name a type using push_back

i am new to using c++ 我是使用c ++的新手

When I execute the following code 当我执行以下代码

i am aware it shouldn't draw anything at the moment i am just trying to change from using an array for vertex position to using a vector as i want to be able to calculate points then use a push_back to append them. 我知道它暂时不应该画任何东西,因为我想能够计算点然后使用push_back附加它们,所以我现在正试图从将数组用于顶点位置更改为使用向量。

This minimal example won't compile: 这个最小的示例将无法编译:

#include <vector>

std::vector<float> vertexPositions;

const float triangle = 0.75f;

vertexPositions.push_back(triangle);

int main(int argc, char** argv)
{
    return 0;
}

I get: 我得到:

error: ‘vertexPositions’ does not name a type

vertexPositions.push_back(triangle); is a statement. 是一个声明。 It must be placed inside a function definition. 必须将其放在函数定义中。 It can not be placed in the global scope like that. 不能将它放在这样的全局范围内。

Move that line into for example main , and you should be fine. 将该行移至例如main ,就可以了。

是否添加了#include <vector>

I see your problem - the following line must appear in your main function : 我看到了您的问题-以下行必须出现在您的主要功能中:

vertexPositions.push_back(triangle);

I created a console application as an example: 我创建了一个控制台应用程序作为示例:

    // test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <vector>

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<float> vertexPositions;

    const float triangle = 0.75f;

    vertexPositions.push_back(triangle);
    return 0;
}

调试会话

Is there something simple you might be missing? 有什么简单的东西可能会丢失吗?

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

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