-3
vector<vector<int>> v;
vector<int> v0;
for(int i = 0; i < 5; i++){
    v.push_back(v0);
}
for(int i = 0; i < v.size(); i++){
    cout << static_cast<void const *>(&(v[i])) << endl;
}

I try to treat vector as an object and cout its address like above but failed, then how can I cout the vector's address.

cxs1031
  • 150
  • 2
  • 9

2 Answers2

2

Instead of using gcc, you might want to try to compile your code with

g++ -std=c++14 

Then it should work: https://ideone.com/y0DwdA.

The linker error, which I could reproduce by using gcc, is most probably related to the differences between the compilers described here.


As pointed out by @πάνταῥεῖ, the compiler flag -std=c++11 will work as well.

Community
  • 1
  • 1
RHertel
  • 23,412
  • 5
  • 38
  • 64
  • I can't see any relevance for `-std=c++14 ` here? – πάντα ῥεῖ Mar 03 '16 at 19:03
  • @πάνταῥεῖ If you insert a space betwen `>>` in the first line, at the end of the nested vector declaration, there is none. – RHertel Mar 03 '16 at 19:05
  • 1
    `-std==c++11` or no specific flag will solve this already, unless an older `cx03` compiler is used. – πάντα ῥεῖ Mar 03 '16 at 19:07
  • 1
    @πάνταῥεῖ I agree that -std=c++11 is sufficient, but no complier flag won't work, at least not generally. I'm using g++ version 5.2.1 and the >> is a syntax error without the mentioned flags. – RHertel Mar 03 '16 at 19:08
1

Compile with g++ -std=c++11 -Wall main.cc the code:

#include <iostream>
#include <vector>

int
main()
{
    using namespace std;

    vector<vector<int>> v;
    vector<int> v0;
    for(int i = 0; i < 5; i++){
        v.push_back(v0);
    }
    for(size_t i = 0; i < v.size(); i++){
        cout << reinterpret_cast<size_t>(&(v[i])) << endl;
    }
}
Michael Lehn
  • 2,934
  • 2
  • 17
  • 19