我正在使用 C++ 开发 iPhone 应用程序的算法部分,但遇到了一个奇怪的错误。我拥有的代码在 Linux、Mac 和 iPhone 设备上都可以使用 gcc-4.2 编译,只是不能在模拟器上编译,这使得调试和测试非常困难。
尝试为模拟器编译的错误消息类似于 4.0.x 中的一个已知错误,尽管这不是很清楚为什么,因为我已明确将 gcc-4.2 设置为默认编译器。
为了演示这个错误,我准备了以下小代码片段:
错误.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?