【问题标题】:does not name a type using push_back不使用 push_back 命名类型
【发布时间】:2012-03-02 10:34:45
【问题描述】:

我是使用 c++ 的新手

当我执行以下代码时

我知道它现在不应该绘制任何东西我只是想从使用数组作为顶点位置更改为使用向量,因为我希望能够计算点然后使用 push_back 来附加它们。

这个最小的例子不会编译:

#include <vector>

std::vector<float> vertexPositions;

const float triangle = 0.75f;

vertexPositions.push_back(triangle);

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

我明白了:

error: ‘vertexPositions’ does not name a type

【问题讨论】:

  • 请展示一个完整的例子;该代码看起来不错。
  • 嗯,你需要在 main 中有那个代码,至少是对 push_back 的调用。

标签: c++


【解决方案1】:

vertexPositions.push_back(triangle); 是一个声明。它必须放在函数定义中。不能这样放在全局范围内。

将该行移至例如main,应该没问题。

【讨论】:

  • 声明/定义 a) std::vector vertexPositions; b) 常量浮点三角形 = 0.75f;要么是陈述。在全球范围内可行的陈述是否有特殊含义?干杯
  • 好的,函数调用是一个表达式(见stackoverflow.com/questions/38180381/…
【解决方案2】:

你添加#include &lt;vector&gt;了吗?

【讨论】:

  • +1。鉴于他/她发布的代码,这是唯一可能的错误。
【解决方案3】:

我看到了您的问题 - 以下行必须出现在您的 main 函数中:

vertexPositions.push_back(triangle);

我创建了一个控制台应用程序作为示例:

    // 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;
}

您可能缺少一些简单的东西吗?

【讨论】:

    猜你喜欢
    • 2020-09-19
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2019-07-13
    • 1970-01-01
    相关资源
    最近更新 更多