Displonker
Displonker

Reputation: 13

Initialising vectors in constructors c++

I promise I've looked around for ages to find a solution to this, but to no avail, however I'm not experienced in C++ so maybe I just didn't know what to search for.

I suppose the easiest thing to do is to show you my error code first, in case it can be recognised. It's a bit of a heap, which is why I can't get my head around it to figure out what's wrong. (Also why I think it's something either out of my understanding, or a really annoying mistake I made!)

g++ -Wall -pedantic -ansi -std=c++0x -g   -c -o contacts.o contacts.cc
In file included from /usr/include/c++/4.6/vector:63:0,
             from contacts.h:5,
             from contacts.cc:2:
/usr/include/c++/4.6/bits/stl_construct.h: In function ‘void std::_Construct(_T1*,
 _Args&& ...) [with _T1 = person, _Args = {}]’:
/usr/include/c++/4.6/bits/stl_uninitialized.h:481:3:   instantiated from ‘static void
std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, 
_Size) [with _ForwardIterator = person*, _Size = unsigned int, bool _TrivialValueType = 
false]’
/usr/include/c++/4.6/bits/stl_uninitialized.h:529:7:   instantiated from ‘void 
std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = 
person*, _Size = unsigned int]’
/usr/include/c++/4.6/bits/stl_uninitialized.h:604:7:   instantiated from ‘void 
std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with 
_ForwardIterator = person*, _Size = unsigned int, _Tp = person]’
/usr/include/c++/4.6/bits/stl_vector.h:1134:2:   instantiated from ‘void 
std::vector<_Tp, _Alloc>::_M_default_initialize(std::vector<_Tp, _Alloc>::size_type) 
[with _Tp = person, _Alloc = std::allocator<person>, std::vector<_Tp, 
_Alloc>::size_type = unsigned int]’
/usr/include/c++/4.6/bits/stl_vector.h:239:9:   instantiated from ‘std::vector<_Tp, 
_Alloc>::vector(std::vector<_Tp, _Alloc>::size_type) [with _Tp = person, _Alloc = 
std::allocator<person>, std::vector<_Tp, _Alloc>::size_type = unsigned int]’
contacts.cc:7:38:   instantiated from here

Edit: Apologies, I don't completely understand what I'm doing, but I hope this is what you asked for. The error still appears if I have at the very least this much in my code (The two relevant files):

Contacts.h:

#ifndef _CONTACTS_H
#define _CONTACTS_H

#include <iostream>
#include <vector>

#include "person.h"


class contacts{
  private:
  int element;
  int count;
  vector<person> pv;

  public:
  contacts();
};
#endif

and contacts.cc:

#include "contacts.h"

using namespace std;


contacts::contacts() : count(0), pv(0) {}

Thanks -Ewan

Upvotes: 0

Views: 521

Answers (1)

wjl
wjl

Reputation: 7755

Why are you passing 0 to the std::vector<> constructor? This sounds like it has the same effect from what you've written:

contacts::contacts() : count(0) {}

Upvotes: 1

Related Questions