在构造函数c ++中初始化向量

时间:2012-12-06 01:11:00

标签: c++ vector

我保证我已经四处寻找解决方案,但无济于事,但我没有C ++经验,所以也许我只是不知道要搜索什么。

我认为最简单的方法是先显示我的错误代码,以防它被识别。这是一堆,这就是为什么我无法理解它是什么错误。 (也就是为什么我认为这是出于我的理解,或者是我犯的一个非常恼人的错误!)

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

编辑: 道歉,我不完全理解我在做什么,但我希望这就是你所要求的。如果我的代码中至少有这么多(两个相关文件),那么错误仍会出现:

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

和contacts.cc:

#include "contacts.h"

using namespace std;


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

由于 -Ewan

1 个答案:

答案 0 :(得分:1)

为什么要将0传递给std :: vector&lt;&gt;构造函数?这听起来和你写的一样有效:

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