C++ Constructor help. Please help.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • izzyk
    New Member
    • Oct 2008
    • 2

    C++ Constructor help. Please help.

    Can someone please show me how I would create these constructors in the source file...my compiler tells me I have errors. I know I set up my main, header, and source file correctly because I am getting no errors until I start implementing the constructors.

    In the HEADER file:(xArray.h)
    class xArray{
    public:
    //constructors
    xArray();
    xArray(const xArray& a);
    xArray(const int x);
    private:
    size_t len;
    size_t size;
    //data of int array
    int *data;
    };

    In the source file: (xArray.cpp)

    //this is the constructor I am unsure about
    xArray::xArray( const xArray& a){
    // ? ? ? ? ? ? ? ? ?
    }


    //these two constructors I believe are correct...my compiler does not return any
    //errors

    //constructor 1
    xArray::xArray( ){
    len = 0;
    size = 0;
    data = new int[size];
    }

    //constructor 3
    xArray::xArray( const int x){
    len = 0;
    size = x;
    data = new int[x];
    }

    All help is greatly appreciated.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    What error you are getting?

    Raghu

    Comment

    • izzyk
      New Member
      • Oct 2008
      • 2

      #3
      Originally posted by gpraghuram
      What error you are getting?

      Raghu
      Ok, I think I've created all three of my constructors correctly now: But when I try to create a new xArray in my MAIN file (xArray a;), I am getting an error: (the error is posted below after the constructors which are directly below this.)

      //constructor 1
      xArray::xArray( ){
      len = 0;
      size = 0;
      data = new int[size];
      }

      //constructor 2
      xArray::xArray( const xArray& a){
      len = a.len;
      size = len*2;
      data = new int[size];
      }

      //constructor 3
      xArray::xArray( const int x){
      len = 0;
      size = x;
      data = new int[x];
      }



      //MAIN.cpp

      cout << "Testing the constructors." << endl;
      xArray a; //trying to create an xArray

      HERE IS THE ERROR I AM GETTING: (USING VISUAL BASIC C++):
      MAIN.obj : error LNK2019: unresolved external symbol "public: _thiscall xArray::~xArray (void)" (??1xArray@@QAE @XZ) referenced in function_main
      MAIN.exe : fatal error LNK1120: 1 unresolved externals

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Have you declared the destructor in header and not defined the body for it?

        Raghu

        Comment

        Working...