我正在使用 C++ 开发 iPhone 应用程序的算法部分,我遇到了一个奇怪的错误。我拥有的代码可以在 Linux、Mac 和 iPhone 设备上使用 gcc-4.2 进行良好编译,只是不能在模拟器上进行编译,这使得调试和测试非常困难。
尝试为模拟器编译的错误消息类似于 4.0.x 中的一个已知错误,尽管这不是很清楚原因,因为我已明确将 gcc-4.2 设置为默认编译器。
为了演示错误,我准备了以下小代码片段:
bug.cpp
#include <tr1/unordered_map>
#include <iostream>
/* a hash key for the visitedTrip table */
struct X {
int x;
X() : x(0){};
X(int x) : x(x){};
};
typedef std::tr1::unordered_map<int,X> dict;
int main()
{
dict c1;
X a(0);
X b(1);
X c(2);
c1[0] = a;
c1[1] = b;
c1[2] = c;
dict::const_iterator it;
for(it = c1.begin(); it != c1.end(); it++)
std::cout << it->first << std::endl;
return (0);
}
然后尝试编译如下:
编译.sh
#!/bin/bash
#
# Compiling for the simulator and compiling under gcc-4.0 return the same error message
#
#SUCCESS
c++-4.2 -arch i386 bug.cpp
#FAIL
c++-4.0 -arch i386 bug.cpp
#SUCCESS
gcc-4.2 -arch i386 -c bug.cpp
#FAIL
gcc-4.0 -arch i386 -c bug.cpp
#FAIL
gcc-4.2 -arch i386 -c bug.cpp -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk
尽管我使用 gcc-4.2 为模拟器编译,但我得到的错误消息与我在 gcc-4.0 下编译时得到的错误消息相同,即:
bug.cpp:27: error: no matching function for call to ‘Internal::hashtable_iterator<std::pair<const int, X>, false, false>::hashtable_iterator()’
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk/usr/include/c++/4.2.1/tr1/hashtable:236: note: candidates are: Internal::hashtable_iterator<Value, is_const, cache>::hashtable_iterator(const Internal::hashtable_iterator<Value, true, cache>&) [with Value = std::pair<const int, X>, bool is_const = false, bool cache = false]
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk/usr/include/c++/4.2.1/tr1/hashtable:234: note: Internal::hashtable_iterator<Value, is_const, cache>::hashtable_iterator(Internal::hash_node<Value, cache>**) [with Value = std::pair<const int, X>, bool is_const = false, bool cache = false]
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk/usr/include/c++/4.2.1/tr1/hashtable:232: note: Internal::hashtable_iterator<Value, is_const, cache>::hashtable_iterator(Internal::hash_node<Value, cache>*, Internal::hash_node<Value, cache>**) [with Value = std::pair<const int, X>, bool is_const = false, bool cache = false]
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk/usr/include/c++/4.2.1/tr1/hashtable:225: note: Internal::hashtable_iterator<std::pair<const int, X>, false, false>::hashtable_iterator(const Internal::hashtable_iterator<std::pair<const int, X>, false, false>&)
关于为什么这个 gcc-4.0.x 错误会进入模拟器,而实际上模拟器应该使用 gcc-4.2.x,但这个错误已被修复,有什么想法吗?
最佳答案
不确定这是否是正确的答案,但这可能可以解释为什么您在使用 4.2 时会看到 4.0 的行为:
> pwd
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk/usr/include/c++
> ls -l
total 4
drwxr-xr-x 10 root wheel 2278 10 Sep 09:32 4.0.0/
lrwxr-xr-x 1 root wheel 5 10 Sep 09:30 4.2.1@ -> 4.0.0
看起来他们正在尝试为两个版本使用 4.0 header 集,至少在带有 Xcode 3.2 的 Snow Leopard 上是这样。
关于c++ - 为 iPhone 设备编译但不为模拟器编译的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1450133/