Question

std::vector< std::pair< const QTextCharFormat, std::vector< std::tr1::regex > > > foo;
std::vector< std::pair< const QTextCharFormat, std::vector< std::tr1::regex > > > bar;

Won't work on gcc 4.6.3 because I cannot call: bar.push_back( std::make_pair( foo.first, foo.second ) ); This compiles and runs fine on Visual Studio, but under gcc I get:

/usr/include/c++/4.6/bits/stl_pair.h:156:2: error: passing ‘const QTextCharFormat’ as ‘this’ argument of ‘QTextCharFormat& QTextCharFormat::operator=(const QTextCharFormat&)’ discards qualifiers [-fpermissive]

Is there an intermediate that Visual Studio is skipping that gets created under gcc?

Was it helpful?

Solution

From this answer:

Items in a vector must be assignable. const objects aren't assignable, so attempting to store them in a vector will fail (or at least can fail -- the code is invalid, but a compiler is free to accept it anyway, if it so chooses, though most programmers would generally prefer that invalid code be rejected).

OTHER TIPS

Well, bar is an std::vector<std::pair<...>>, therefore trying to assign an std::pair (from std::make_pair) to it is going to fail. What you probably want is to push back that new std::pair into bar.

foo is also an std::vector, therefore you need to select an element and only then call .first or .second:

bar.emplace_back(foo[i].first, foo[i].second);

But from the way you are using it you most likely mistakenly added an std::vector too much in the definitions of both bar and foo.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top