gpt4 book ai didi

c++ - 在构造函数中初始化 vector C++

转载 作者:行者123 更新时间:2023-11-28 07:51:15 26 4
gpt4 key购买 nike

我保证我已经四处寻找了很多年的解决方案,但无济于事,但是我没有使用 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

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

联系人.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) {}

谢谢-伊万

最佳答案

为什么要将 0 传递给 std::vector<> 构造函数?这听起来与您所写的效果相同:

联系人::联系人() : count(0) {}

关于c++ - 在构造函数中初始化 vector C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13735371/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com