Friday, May 6, 2011

C++ constructor problem

#include <iostream>
using namespace std;

// This first class contains a vector and a scalar representing the size of the vector.
typedef class Structure1
{
 int N;
 double* vec;
public: 
 // Constructor and copy constructor:
 Structure1(int Nin);
 Structure1(const Structure1& structurein);
 // Accessor functions:
 int get_N() { return N; }
 double* get_vec() { return vec; }
 // Destructor:
 ~Structure1() { delete []vec; }
} Structure1;

Structure1::Structure1(int Nin)
{
 N = Nin;
 vec = new double [N];
 for (int i = 0; i < N; i++)
  {
   vec[i] = i;
  };
}

Structure1::Structure1(const Structure1& structurein)
{
 vec = new double[structurein.N];
 for(int i = 0; i < structurein.N; i++)
 {
  vec[i] = structurein.vec[i];
 };
}

// This class just contains the first structure.
typedef class Structure2
 {
  Structure1 structure;
 public:  
  // Constructor:
  Structure2(const Structure1& structurein) { structure = structurein; }

The error occurs here:

Line Location main.cpp:47: error: no matching function for call to 'Structure1::Structure1()'

  // Accessor Function:
  Structure1 get_structure() { return structure; }
  // Destructor:
  ~Structure2() {}
 } Structure2;

int main (int argc, char * const argv[])
{
 const int N = 100;
 Structure1 structure1(N);
 Structure2 structure2(structure1);

 return 0;
}

If anyone knows what's wrong, your help would be greatly appreciated. Thanks!

From stackoverflow
  • You need to use a member initializer in your Structure2 ctor.

    Structure2(const Structure1& structurein) : structure(structurein) {}
    

    It's trying to use a default constructor for your Structure1 member, and there isn't one.

  • Since you have declared a constructor for Structure2, the compiler refuses to create a default constructor for it which is needed in order to access members of the class like you do in this constructor:

      Structure2(const Structure1& structurein) { structure = structurein; }
    

    You should probably do:

    Structure2(const Structure1& structurein) : structure(structurein) {}
    
  • By the way, I think you need to fix the copy constructor:

    Structure1::Structure1(const Structure1& structurein)
    {
     ///!!!! Initialize N
     N = structurein.N;
    
     vec = new double[structurein.N];
     for(int i = 0; i < structurein.N; i++)
     {
      vec[i] = structurein.vec[i];
     };
    }
    
    ypsu : At the beginning you should make sure you are constructing the object from an another object, like if (this == &structurein) return; Otherwise your copy won't work, and also a memory leak we'll be there as well.
    ypsu : Ah, sorry,ignore my previous commentt, I mixed this this with the = operator! :)
    ypsu : Sorry for spamming, but you can still construct the object from the same object: new (&b) A(b); Calling placement new with the same address where as the object from which A is constructed is rare, maybe you can ignore this.

0 comments:

Post a Comment