dimanche 12 mars 2017

Array of std::vector's doesn't work

I'd like to manage three std::vectors and thought about a solution like this:

#include <stdlib.h>
#include <vector>

int main() {
  std::vector<int> nums[3];
  nums[0].push_back(1);
  nums[0].push_back(3);
  nums[0].push_back(5);
  nums[1].push_back(10);
  nums[1].push_back(20);
  nums[1].push_back(30);
  nums[2].push_back(100);
  nums[2].push_back(300);
  nums[2].push_back(500);
  nums[2].push_back(700);
  int i;
  for (i = 0; i < nums[0].size(); i++) {
    printf("%d\n", nums[0].at(i));
  }
  for (i = 0; i < nums[1].size(); i++) {
    printf("%d\n", nums[2].at(i));
  }
  for (i = 0; i < nums[2].size(); i++) {
    printf("%d\n", nums[2].at(i));
  }
}

Why doesn't this work? I don't understand the compiler output. Any idea how I can fix this?

gcc:

Undefined symbols for architecture x86_64:
"std::__1::__vector_base_common::__throw_length_error() const", referenced from: void std::__1::vector >::__push_back_slow_path(int const&) in code-4a0a01.o "std::__1::__vector_base_common::__throw_out_of_range() const", referenced from: std::__1::vector >::at(unsigned long) in code-4a0a01.o "std::terminate()", referenced from: ___clang_call_terminate in code-4a0a01.o "operator delete(void*)", referenced from: std::__1::__vector_base >::~__vector_base() in code-4a0a01.o std::__1::__split_buffer&>::~__split_buffer() in code-4a0a01.o
"operator new(unsigned long)", referenced from: std::__1::__split_buffer&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator&) in code-4a0a01.o
"___cxa_begin_catch", referenced from: ___clang_call_terminate in code-4a0a01.o "___gxx_personality_v0", referenced from: _main in code-4a0a01.o void std::__1::vector >::__push_back_slow_path(int const&) in code-4a0a01.o Dwarf Exception Unwind Info (__eh_frame) in code-4a0a01.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Aucun commentaire:

Enregistrer un commentaire