0

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

3
  • Post the minimum code that reproduces the issue. Narrow it down. Cut it up to the smallest piece. Commented Dec 6, 2012 at 1:15
  • Yeah, put at least your contacts class header here. Commented Dec 6, 2012 at 1:19
  • The issue is probably with the person class; please post that header; you'll want to have accessible default and copy constructors defined for person. Commented Dec 6, 2012 at 1:35

1 Answer 1

1

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) {}

Sign up to request clarification or add additional context in comments.

5 Comments

Note: my logic may be flawed, but here it is: I thought I need to initialise my vector to use it throughout the rest of the code, but I cannot initialise it prior to my constructor or I receive a different error. (At least I think it needs to be initialised??)
@Displonker: vector has a default constructor... it will be properly constructed with no contained elements. The same is true for all Standard containers (including std::string), typical smart pointer classes, but not true for types like int, char, double and [] arrays of them.
@Displonker Your vector will automatically be initialized (via it's default constructor) before your constructor body starts executing. It will be empty. If you want to actually FILL the vector, you can do that in your constructor if you wish.
Hah.. Sorry everyone for wasting your time! I've been working on this project for 8 hours today, and I suppose I lost track of vectors. Should I look to delete the question? thanks for the help.
@Displonker: I don't think you need worry about deleting the question... there is some clean up process where less-useful questions are purged periodically, and you never know - other people may find this helpful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.