你如何在 XCode + C++ 中使用 std::vector?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1611526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:55:09  来源:igfitidea点击:

How do you std::vector in XCode + C++?

c++xcodevectorstdstdvector

提问by Dwight

For various reasons (and I assure you they are valid, so no "use Cocoa" talk please), I must work with XCode, C++, OpenGL, OpenCL (with a little GLUT on the side) to rebuild a few graphics demos on Mac (coming from XP + Visual Studio 2005 development). The project was built as a Command Line Tool using "c++ stdc++".

出于各种原因(我向你保证它们是有效的,所以请不要谈论“使用 Cocoa”),我必须使用 XCode、C++、OpenGL、OpenCL(侧面有一点 GLUT)在 Mac 上重建一些图形演示(来自 XP + Visual Studio 2005 开发)。该项目是使用“c++ stdc++”作为命令行工具构建的。

My Program.h file connects my shader objects together, compiles, links, and otherwise prepares them for use as OpenGL shader programs. Contained within this file are the following relevant lines of code:

我的 Program.h 文件将我的着色器对象连接在一起、编译、链接并以其他方式准备它们以用作 OpenGL 着色器程序。此文件中包含以下相关代码行:

#include <vector>
using std::vector;

and within the private section of the class:

并在班级的私人部分中:

vector<int> shaderHandles;

and when adding shader handles:

添加着色器句柄时:

shaderHandles.push_back(shaderHandle);

and finally, when using the pushed shader handles:

最后,当使用推送的着色器句柄时:

for (int s = 0; s < (int) shaderHandles.size(); s++)
{
    glAttachShader(handle, shaderHandles[s]);
}

In all my experience and research, there's nothing wrong with these lines within C++. However, when compiling (whether debug or release, so it's not related to the _GLIBCXX_DEBUGproblem), the following 4 errors are generated:

根据我的所有经验和研究,C++ 中的这些行没有任何问题。但是编译的时候(无论是debug还是release,所以与_GLIBCXX_DEBUG问题无关),会产生以下4个错误:

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_bvector.h:916: error: 'size' is not a member of 'std'
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_bvector.h:961: error: 'size' is not a member of 'std'
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/vector.tcc:350: error: '__old_size' is not a member of 'std'
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/vector.tcc:453: error: '__old_size' is not a member of 'std'

Also, the file that links to stl_bvector.h and vector.tcc is:

此外,链接到 stl_bvector.h 和 vector.tcc 的文件是:

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/vector

Thus far, numerous Google searches have turned up nothing. All this code works flawlessly on Windows. Worse, if I replace the above code with the list equivalents:

到目前为止,无数的谷歌搜索一无所获。所有这些代码都可以在 Windows 上完美运行。更糟糕的是,如果我用列表等价物替换上面的代码:

#include <list>
using std::list;

and,

和,

list<int> shaderHandles;

and,

和,

for (list<int>::iterator s = shaderHandles.begin(); s != shaderHandles.end(); s++)
{
    glAttachShader(handle, *s);
}

The program works as expected.

该程序按预期工作。

But one can't blame this ENTIRELY on the vector implementation, because the following program:

但是不能将这完全归咎于向量实现,因为以下程序:

#include <iostream>
#include <vector>
using std::vector;

int main (int argc, char * const argv[])
{
    vector<int> test;

    test.push_back(1);
    test.push_back(2);
    test.push_back(3);

    test.clear();
    return 0;
}

Works with no problems.

工作没有问题。

I'll be happy to provide more information as necessary.

我很乐意在必要时提供更多信息。

Please don't tell me I should use Cocoa/Objective-C; it's not really an option right now. And while yes, I can use lists to accomplish this functionality, other parts of my demo are not so easy to rework.

请不要告诉我我应该使用 Cocoa/Objective-C;现在这不是一个真正的选择。虽然是的,我可以使用列表来完成此功能,但我的演示的其他部分并不那么容易返工。

采纳答案by Dwight

I'm SO sorry everyone. Mere minutes after posting this, I decided to go on with what I could, saving this issue for later. I found a similar problem occurring with fstream. With this new information available, a Google search brought up this topic, and ultimately the solution.

我很抱歉大家。发布后仅几分钟,我决定继续我能做的,把这个问题留到以后再说。我发现 fstream 发生了类似的问题。有了这些新信息,谷歌搜索就提出了这个话题,最终找到了解决方案。

I had defined my own min and max macros in my completely unrelated vector math files. The solution was to remove my macros and put std:: in front of the min and max calls.

我在完全不相关的矢量数学文件中定义了自己的最小和最大宏。解决方案是删除我的宏并将 std:: 放在 min 和 max 调用之前。

回答by caahab

Did you check the Mac SDK documentation, what functions do they declare for the vector class? Maybe you have set some compiler flags? Also you can do the same for the vector as you did for the list, using iterators to scan thru your vector.

您是否查看了 Mac SDK 文档,他们为 vector 类声明了哪些函数?也许您设置了一些编译器标志?您也可以对向量执行与列表相同的操作,使用迭代器扫描向量。