Question

The code:

#include <vector>

int main()
{
    std::vector<int> v1 = {12, 34};
    std::vector<int> v2 = {56, 78};

    //Doesn't work.
    v1.push_back(v2[0]);

    //Works.
    int i = v2[0];
    v1.push_back(i);

    return 0;
}

For some reason, the first push_back doesn't work, while the second does. Eclipse gives for that line the error:

Invalid arguments ' Candidates are: void push_back(const int &) void push_back(int &&) '

Could someone explain what is happening there? Thanks!

EDIT:

The code actually compiles fine. For some reason, Eclipse doesn't agree that this is valid code.

Was it helpful?

Solution

If I compile the code with g++ 4.7.3 with

g++ test.cpp --std=c++0x

It compiles correctly and if I try to print v1[2];, I get the correct result.

std::cout << v1[2]; // 56

The Eclipse code analyzer tool (CODAN) may just not be right in this situation.

Rely on the output of a C++ (in this case C++11 compatible) compiler.

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