错误:eclipse C ++中的map不能分配只读变量

时间:2014-12-22 08:31:04

标签: c++ eclipse c++11 compiler-errors

我在C ++课程中,老师要求我们使用Eclipse(我在Macbook Pro上运行它)。我已经按照他的所有说明(包括在eclipse中添加-std = c ++ 11到我的c ++设置)并编译并运行了一个hello world程序。

然后他希望我们从std库中测试一下。他给了我们一个文件使用,并说我们应该能够编译并运行它没有任何问题。这是整个代码:

//============================================================================
// Name        : stltest.cpp
// Version     : Winter 2014
// Description : Simple test of STL (really of MingGW in Eclipse)
//============================================================================

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

int main() {
   std::map<const int,std::string> m;
    m = {{1,"one"},{2,"two"},{3,"three"}};
  m[4] = "four";
  m[5] = "five";

  for (std::pair<int,std::string> elem : m)
    std::cout << elem.first << ' ' << elem.second << std::endl;
  std::cout << std::endl;

  for (auto elem : m)
    std::cout << elem.first << ' ' << elem.second << std::endl;
  std::cout << std::endl;

  for (auto elem = m.begin(); elem != m.end(); elem++)
    std::cout << elem->first << ' ' << elem->second << std::endl;
  std::cout << std::endl;

  return 0;
}

问题是我遇到编译器错误,经过2个小时的搜索后,我找不到任何能解决问题的方法。我认为它与C ++ 11和eclipse有关,但我并非100%肯定。这是它的意思:

23:47:59 **** Build of configuration Debug for project Test0 ****
make all 
Building file: ../src/stltest.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/stltest.d" -MT"src/stltest.d" -o "src/stltest.o" "../src/stltest.cpp"
In file included from ../src/stltest.cpp:8:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:423:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__tree:16:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:598:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:336:15: error: read-only variable is not assignable
        first = _VSTD::forward<first_type>(__p.first);
        ~~~~~ ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:614:15: note: in instantiation of member function 'std::__1::pair<const int, std::__1::basic_string<char> >::operator=' requested here
        {__nc = std::move(__v.__nc); return *this;}
              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__tree:1223:35: note: in instantiation of member function 'std::__1::__value_type<const int, std::__1::basic_string<char> >::operator=' requested here
                __cache->__value_ = *__first;
                                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:941:21: note: in instantiation of function template specialization 'std::__1::__tree<std::__1::__value_type<const int, std::__1::basic_string<char> >, std::__1::__map_value_compare<const int, std::__1::__value_type<const int, std::__1::basic_string<char> >, std::__1::less<const int>, true>, std::__1::allocator<std::__1::__value_type<const int, std::__1::basic_string<char> > > >::__assign_unique<const std::__1::pair<const int, std::__1::basic_string<char> > *>' requested here
            __tree_.__assign_unique(__il.begin(), __il.end());
                    ^
../src/stltest.cpp:16:4: note: in instantiation of member function 'std::__1::map<const int, std::__1::basic_string<char>, std::__1::less<const int>, std::__1::allocator<std::__1::pair<const int, std::__1::basic_string<char> > > >::operator=' requested here
        m = { {1,"one"},{2,"two"},{3,"three"}};
          ^
1 error generated.
make: *** [src/stltest.o] Error 1

23:48:00 Build Finished (took 534ms)

我做错了什么?

0 个答案:

没有答案