【问题标题】:std::vector<std::vector<Vertex>> vertex_matrix; was not declared in this scopestd::vector<std::vector<Vertex>> vertex_matrix;在这方面没有申明
【发布时间】:2013-09-18 05:02:28
【问题描述】:

在一个 .cpp 文件中,我声明并实现了一个“顶点”类。然后我声明并实现第二类“三维对象”。三维对象内部 ,它有一个公共成员 std::vector> vertex_matrix;

我确实导入了 .该项目在 xCode IDE 上运行良好,g++ 提示我“错误:'vertex_matrix' 未在此范围内声明”。

我该如何解决?

#include <vector>
class Vertex : public std::vector<float>
{
   //implementation
};

class ThreeDimensionObject
{
    //the center position
public:  
    //num_stack * num_stack * 4
    std::vector<std::vector<Vertex>> vertex_matrix;
};

【问题讨论】:

  • 作为一个不相关的旁注,std::vector 是在 3 空间中表示顶点的一种非常糟糕的方式,因为它使用动态分配的内存块来存储 3 或 4 个浮点数(并且该块占用的额外开销与这 4 个浮点本身一样多,不要忘记 std::vector 的自动存储本身,它也与实际顶点数据本身一样大)。如果你真的想要一个标准的顶点库容器,那么至少满足于std::array

标签: c++ vector g++ std


【解决方案1】:

当编译为 时,代码在IDEONE 上编译良好。在没有 C++.11 标志的情况下编译时,代码会发出以下错误:

prog.cpp:12:35: error: ‘>>’ should be ‘> >’ within a nested template argument list
     std::vector<std::vector<Vertex>> vertex_matrix;

此错误可能发生在您的错误列表顶部附近,您可能没有看到它。您可以将代码编译为 C++ 11(通过在 g++ 命令行中添加 -std=gnu++11-std=c++11),也可以添加所需的空间。

    std::vector<std::vector<Vertex> > vertex_matrix;

【讨论】:

  • +1 假设 OP(或其他任何人)使用来自 Apple 的罐装 clang,以确保在 Xcode 中符合 C++11,(1)。在解决方案中选择项目,(2) 在设置窗口中选择 All+Combined,(3) 滚动到 Build Options 并为编译器选择“Apple LLVM ...”,(4) 向下滚动到“Apple LLVM 4.2编译器 - 语言”设置并将 C++ 语言方言设置设置为“C++11 (-std=c++11)” 应该这样做。 g++ 设置略有不同,但在 Xcode 的相同设置位置。
【解决方案2】:

'vertex_matrix'的定义应该是,

std::vector<std::vector<Vertex> > vertex_matrix;

您的代码使用 c++11 标志编译,但没有 c++11 标志它需要额外的空间。

【讨论】:

  • 谢谢!这行得通。我在学校工作,不允许使用 c++ 11。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-01
  • 2012-03-07
  • 2020-01-26
  • 2020-09-22
相关资源
最近更新 更多