我正在使用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);
}
然后尝试按如下方式编译它:
compile.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 bug会进入模拟器的任何想法,实际上模拟器应该使用gcc-4.2.x,其中修复了这个bug?
答案 0 :(得分:4)
不确定这是否是正确的答案,但这可能解释了为什么在使用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标头集,至少在Snow Leopard上使用Xcode 3.2。
答案 1 :(得分:0)
我会仔细检查模拟器引用的库(STL)标题。
答案 2 :(得分:0)
答案 3 :(得分:0)
unordered_map的迭代器在gcc-4.0.x中没有默认构造函数存在问题
http://bytes.com/topic/c/answers/607427-error-tr1-unordered_map-iterator
声明时的初始化应该解决它:
for(dict::const_iterator it = c1.begin(); it != c1.end(); it++)
std::cout << it->first << std::endl;