0

I am trying to set size before using vector, but got error info:

error c2512: 'EItem:EItem' : no appropriate default constructor available

// file.h
vector<EItem > *eL;

// file.cpp
eL = new vector<EItem>(100);

What do I need to provide in my own defined object EItem?

iammilind
  • 68,093
  • 33
  • 169
  • 336
user1558064
  • 867
  • 3
  • 12
  • 28

4 Answers4

2

As your compiler is saying, you have to define a default constructor for your EItem class :

in .h :

class EItem {
public:
    EItem(); // constructor

    // ...
};

in .cpp :

EItem::EItem() {}

Or all in one (in .h) :

class EItem {
public:
    EItem() {} // constructor

    // ...
};

It is the minimum. You can add some arguments to your class and initialize some attributes of your class in the constructor.

In C++, default constructors are significant because they are automatically invoked in certain circumstances:

  • When an object value is declared with no argument list, e.g. MyClass x; or allocated dynamically with no argument list, e.g. new MyClass or new MyClass(); the default constructor is used to initialize the object
  • When an array of objects is declared, e.g. MyClass x[10]; or allocated dynamically, e.g. new MyClass [10]; the default constructor is used to initialize all the elements
  • When a derived class constructor does not explicitly call the base class constructor in its initializer list, the default constructor for the base class is called
  • When a class constructor does not explicitly call the constructor of one of its object-valued fields in its initializer list, the default constructor for the field's class is called
  • In the standard library, certain containers "fill in" values using the default constructor when the value is not given explicitly (ex : vector<MyClass>(10);), initializes the vector with 10 elements, which are filled with the default-constructed value of our type.

In the above circumstances, it is an error if the class does not have a default constructor.

There is some good answers on this thread too : When do we need to have a default constructor?

Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
1

The compiler is pointing you to the right direction: your class EItem does not have a default constructor - hence you should supply one.

Quoted from here:

What happens if we do not declare a default constructor and then instantiate our class? The answer is that C++ will allocate space for our class instance, but will not initialize the members of the class (similar to what happens when you declare an int, double, or other basic data type).

Without knowing the definition of your EItem class, a minimal example of implementing a default constructor is:

class EItem {
...
public:
    ...
    //this is a minimal default constructor
    EItem() {
        ...
        //initialize and set the values for any data members of the class here
        ...
    }
...
}

For more reading on this and more importantly why you should always supply one in your classes, see the following:

http://en.wikipedia.org/wiki/Default_constructor

jrd1
  • 10,358
  • 4
  • 34
  • 51
1

The vector needs to know how much size an EItem takes up, since vector uses an array. A default constructor will be provided, if none other exist in the EItem class. Make sure you have the class defined and the constructor is public.

class EItem
{
 public:
   EItem();
};
john.dennis
  • 56
  • 2
  • 7
0

Your class need to have a default constructor without parameters for it to be able to use a vector like that. If you can't have a default constructor then you need to make a vector of pointer to your class.

mooviies
  • 45
  • 5