Reputation: 165
I am trying do initialize a vector of self-defined type dataCard
:
vector<dataCard> dataSet;
dataSet.reserve(200);
However the compiler doesn't allow me to do that indicating an error at the second line (where I try to reserve space for the container):
required from here
However when I change the declaration to the following
vector<dataCard*> dataSet;
it starts working.
What could be the reason for the compiler not allowing me to use my type as it is, but letting me to use a pointer to the type? I actually need a vector of objects, not pointers to them.
EDIT
Header file of dataCard
#include <card.h>
class dataCard {
char* name; // name according to the filename
char Class; // Classes: 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A
double distanceToQuery;
public:
void resetDistance();
int numBlackTotal (bitmap* bmp);
bitmap image;
int numOfBlackPixels;
char* getName();
void setName(char* n);
void setClass (char* c);
char* getClass();
void setDistance(double distance);
double getDistance ();
dataCard(const char* fname);
dataCard();
virtual ~dataCard();
};
Here's an error message from compiler
/usr/include/c++/4.7/bits/stl_uninitialized.h:77:3: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = dataCard*; _ForwardIterator = dataCard*; bool _TrivialValueTypes = false]’
/usr/include/c++/4.7/bits/stl_uninitialized.h:119:41: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = dataCard*; _ForwardIterator = dataCard*]’
/usr/include/c++/4.7/bits/stl_uninitialized.h:260:63: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = dataCard*; _ForwardIterator = dataCard*; _Tp = dataCard]’
/usr/include/c++/4.7/bits/stl_vector.h:1112:8: required from ‘std::vector<_Tp, _Alloc>::pointer std::vector<_Tp, _Alloc>::_M_allocate_and_copy(std::vector<_Tp, _Alloc>::size_type, _ForwardIterator, _ForwardIterator) [with _ForwardIterator = dataCard*; _Tp = dataCard; _Alloc = std::allocator<dataCard>; std::vector<_Tp, _Alloc>::pointer = dataCard*; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/4.7/bits/vector.tcc:76:70: required from ‘void std::vector<_Tp, _Alloc>::reserve(std::vector<_Tp, _Alloc>::size_type) [with _Tp = dataCard; _Alloc = std::allocator<dataCard>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/home/ppsadm01/Documents/vgv/src/main.cc:145:22: required from here
/usr/include/c++/4.7/bits/stl_construct.h:85:7: error: no matching function for call to ‘dataCard::dataCard(const dataCard&)’
Upvotes: 2
Views: 97
Reputation: 4813
As the error message suggests you need to implement the copy constructor dataCard::dataCard(const dataCard&)
. You need that because for example vector<dataCard>::push_back()
will copy dataCard
instances.
When you write the copy constructor, be careful that all member variables get copied - in the right way. So an example implementation could be
dataCard::dataCard(const dataCard &rhs)
{
strcpy(name, rhs.name);
Class = rhs.Class;
distanceToQuery = rhs.distanceToQuery;
image = rhs.image; // make sure that this is OK!
}
Upvotes: 2
Reputation: 416
If the compiler does not know the size of a dataCard
object, it cannot reserve space for it. A pointer however is always a known size so it can reserve space for that.
My guess is that the compiler does not know the exact size of a dataCard
object. If for example the bitmap
type object is of variable size, then the compiler would not know what size dataCard
would be.
Upvotes: 2