你如何在 XCode 中 std::vector + C++?
由于各种原因(我向你保证它们是有效的,所以请不要“使用 Cocoa”),我必须使用 XCode、C++、OpenGL、OpenCL(侧面有点过剩)在 Mac 上重建一些图形演示(来自XP + Visual Studio 2005开发)。该项目是使用“c++ stdc++”构建为命令行工具的。
我的 Program.h 文件将我的着色器对象连接在一起,进行编译、链接或以其他方式准备它们以用作 OpenGL 着色器程序。该文件中包含以下相关代码行:
#include <vector>
using std::vector;
在类的私有部分中:
vector<int> shaderHandles;
添加着色器句柄时:
shaderHandles.push_back(shaderHandle);
最后,使用推送的着色器句柄时:
for (int s = 0; s < (int) shaderHandles.size(); s++)
{
glAttachShader(handle, shaderHandles[s]);
}
根据我的所有经验和研究,这些行没有任何问题在 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'
另外,链接到stl_bvector.h和vector.tcc的文件是:
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/vector
到目前为止,多次谷歌搜索一无所获。所有这些代码在 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);
}
程序将按预期工作。
但我们不能将这完全归咎于矢量实现,因为以下程序:
#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;
}
工作没有问题。
我很乐意根据需要提供更多信息。
请不要告诉我应该使用 Cocoa/Objective-C;现在这确实不是一个选择。虽然是的,我可以使用列表来完成此功能,但我的演示的其他部分并不那么容易返工。
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++".
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:
#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_DEBUG
problem), the following 4 errors are generated:
/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:
/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:
#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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很抱歉大家。发布此文几分钟后,我决定继续尽我所能,将此问题留到以后再讨论。我发现 fstream 也出现类似的问题。有了这些新信息,Google 搜索就出现了此主题,并最终找到了解决方案。
我在完全不相关的矢量数学文件中定义了自己的最小和最大宏。解决方案是删除我的宏并将 std:: 放在 min 和 max 调用前面。
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.
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.
你检查过Mac SDK文档吗,他们为向量类声明了哪些函数?也许您设置了一些编译器标志?
您还可以对向量执行与列表相同的操作,使用迭代器扫描向量。
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.