我使用C ++开发的iPhone应用程序的算法的一部分,并且我遇到奇怪的错误。我有代码,编译罚款用gcc-4.2都在Linux,Mac上,和iPhone设备上,只是没有在模拟器上,这使得调试和测试非常困难。

从尝试消息编译为模拟器误差类似于在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 

Eventhough我使用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标头集合了两个版本,至少在雪豹和Xcode 3.2。

其他提示

我将仔细检查该模拟器引用库(STL)报头。

有时,在Xcode编译器的问题,也许你有类似于这里所描述的一个问题。

UIKit的SDK错误

在此情况下,必须特别指定编译器用于设备和模拟器。 我知道,这样做没有任何意义,但它固定我的问题。

有与不具有在GCC-4.0.x的默认构造unordered_map的迭代

的问题

http://bytes.com/topic/c/答案/ 607427-错误-TR1-unordered_map迭代器

在声明初始化应该修复它:

for(dict::const_iterator it = c1.begin(); it != c1.end(); it++)
    std::cout << it->first << std::endl;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top