2012-04-03 101 views
-1

崩溃我有一个字符串映射在它的两个条目: "Chicago"--> ChicagoObj"NewYork"-->NewYorkObj,其中ChicagoObjNewYorkObj是指针的MyClass对象。的std ::地图适用于Windows 32位,但在64位

以下代码在32位编译并运行正常,它在64位编译,但在打印芝加哥条目后总是在64位崩溃。任何帮助表示赞赏!

typedef std::map<std::string, MyClass*> MyStringMap; 
MyStringMap my_map; 

std::string key1="Chicago"; 
MyClass *ChicagoObj = new MyClass; 
my_map[key1] = ChicagoObj; 

std::string key2="NewYork"; 
MyClass *NewYorkObj = new MyClass; 
my_map[key2] = NewYorkObj ; 

MyStringMap::iterator iObjMap = my_map.begin(); 

while (iObjMap != my_map.end()) 
{ 
    std::string key = iObjMap->first; 

    std::cout<<"name of the key in the map: " << key <<std::endl; 
    iObjMap++; 
} 
+1

我看到“字符串”和“std :: string”的混合使用,我假设一个错字,否则可能“字符串”根本可能不是一个“std :: string”。 – SirDarius 2012-04-03 16:22:31

+3

即使在这种情况下它看起来无害,你也不应该使用像'std :: string'这样的对象类型的'C'类型转换。 – Chad 2012-04-03 16:27:49

+0

这段代码是否实际编译?它似乎缺少一些东西,例如什么是“my_map”? – josephthomas 2012-04-03 16:27:58

回答

0

确定您已经编译了最新的代码,并且没有运行带有以前的错误的程序吗?我已经在Windows 7 64上的Visual Studio 2010中编译了代码,并且没有任何问题。

虽然代码中有编译错误,但是std::out应该是std::cout。除此之外,甚至没有来自/ Wall的警告。

+0

“甚至没有来自/ Wall的警告。”这不可能是真的。标准库头文件(比如'')不会干净地用/ Wall编译。你的意思是/ W4? – 2012-04-03 17:17:22

+0

我是指他的代码中的/ Wall,而不是其他包含的。 – josephthomas 2012-04-03 17:23:41

0

既然你显然不会发布一个完整的,可编译的代码来说明问题,下面是它如何工作的一个简短演示:

#include <map> 
#include <iostream> 
#include <string> 
#include <iterator> 

class City { 
    unsigned int population; 
    // probably more stuff here, but this should be enough for now. 
public: 
    City(int pop=0) : population(pop) {} 

    friend std::ostream &operator<<(std::ostream &os, City const &c) { 
     return os << c.population; 
    } 

}; 


std::ostream &operator<<(std::ostream &os, 
         std::pair<std::string, City> const &d) 
{ 
    return os << d.first << ": " << d.second; 
} 

int main() { 
    std::map<std::string, City> cities; 
    typedef std::pair<std::string, City> ct; 

    cities["Chicago"] = City(3456789); 
    cities["New York"] = City(8765432); 

    std::copy(cities.begin(), cities.end(), 
     std::ostream_iterator<ct>(std::cout, "\n")); 

    return 0; 
} 

我编译和测试,这对64位Windows,它似乎工作得很好。这似乎(至少对我来说)map在64位Windows上可以正常工作。确切地说,你已经完成的工作很难被猜出,但是扩展工作代码来做你想做的事可能更容易,而不是找出那些无法工作的代码中损坏的东西。

相关问题