0

Hi in my program I have two constructors;

BinaryImage();

BinaryImage(int MM, int NN, double* input_data, double thresh);

In my main;

BinaryImage BinaryImageObj();

This compiles fine but when I invoke the second construtor;

BinaryImage BinaryImageObj2(MM,NN,data,1);

This however brings an error;

main.cpp||undefined reference to `BinaryImage::BinaryImage(int, int, double*, double)'

Why does it do this?, am I missing something simple?

Thanks

Mike

This matrix class constructor creates object

Matrix::Matrix(int MM, int NN, double* input_data){

                      M = MM;
                      N = NN;
                      data = new double[M * N];
                      for ( int i =0; i < M; i++)
                      {
                          for (int j = 0; j < N; j++)
                          {
                              data[i* N+j] = input_data[i*N+j];
                           //   cout << data[i*N+j] <<"\t";
                          }
                      //    cout <<"\n";
                      }

           cout << "This is the Matrix Constructor being invoked" << endl ;
}

binaryimage class (inherts from matrix)

BinaryImage::BinaryImage(int MM, int NN, double* input_data, double thresh ):Matrix(MM, NN, input_data)
{
                      M = MM;
                      N = NN;
                      data = new double[M * N];
                      for ( int i =0; i < M; i++)
                      {
                          for (int j = 0; j < N; j++)
                          {
                              treshData(tresh);

                          }

                      }

}

Main

Matrix MatrixObj1(MM,NN,data2);

BinaryImage BinaryImageObj;

edited to show whats going on.

nullVoid
  • 199
  • 1
  • 6
  • 24

1 Answers1

6

You're not implementing the constructors, therefore no symbols are generated for them. You can implement them inside the class definition:

BinaryImage() { };
BinaryImage(int MM, int NN, double* input_data, double thresh) { };

or in an implementation file (recommended).

Your first example:

 BinaryImage BinaryImageObj();

compiles because you're declaring a function called BinaryImageObj that takes no parameters and returns a BinaryImage, and not actually creating a BinaryImage object as you'd think.

The correct syntax is

 BinaryImage BinaryImageObj;
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • BinaryImage::BinaryImage(int MM, int NN, double* input_data, double thresh ):Matrix(MM, NN, input_data) this is within my binaryimage class it was to my belief that the matrix class created the object, i think ive completly missed something :( – nullVoid Apr 25 '12 at 11:02