lundi 22 décembre 2014

error: read-only variable is not assignable for map in eclipse C++

I'm in a C++ class, and the teacher requires that we use Eclipse (I'm running it on a Macbook Pro). I've followed all of his instructions (including adding -std=c++11 to my c++ settings in eclipse) and compiled and ran a hello world program.


Then he wants us to test things from the std library. He gave us a file to use and said we should be able to compile and run it without any issues. This is the entire code:



//============================================================================
// 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;
}


The problem is I'm getting compiler errors, and after 2 hours of searching, I can't find anything that will fix them. I think it has to do with C++ 11 and eclipse, but I'm not 100% sure. Here is what it is saying:



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/http://ift.tt/1zUmJjJ:
In file included from /Applications/http://ift.tt/1AWGPHQ:
In file included from /Applications/http://ift.tt/1zUmHrS:
/Applications/http://ift.tt/1zUmHrW: error: read-only variable is not assignable
first = _VSTD::forward<first_type>(__p.first);
~~~~~ ^
/Applications/http://ift.tt/1AWGR2F: 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/http://ift.tt/1zUmHs4: note: in instantiation of member function 'std::__1::__value_type<const int, std::__1::basic_string<char> >::operator=' requested here
__cache->__value_ = *__first;
^
/Applications/http://ift.tt/1AWGRiX: 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)


What am I doing wrong?


Aucun commentaire:

Enregistrer un commentaire