Menu

Initializing class variables - Error

CoryLehan
2007-12-06
2012-09-26
  • CoryLehan

    CoryLehan - 2007-12-06

    --This is an object-oriented related question that is probably pretty simple, but my instructor never taught us this.
    --I need to initialize two classes that don't have default constructors, and I can't use pointers.

    --I have a class Table:

    namespace tableNS
    {
    class Table
    {
    private:
    bst authorIdBst;
    bst isbnBst;
    DataType data;

    public:
        Table();
            // Initializes each tree to a reference to the actual data.
                ...Various other members with no problems...
    };
    

    }

    --And a class bst (Binary search tree):

    namespace tableNS
    {
    class bst
    {
    private:
    TreeNode root;
    DataType
    data;
    public:
    bst(DataType* pData);
    // Sets the root to NULL and data to pData.
    bst(const bst& tree);
    // Calls CopyTree() to make a copy of the current tree.
    ...Other members that only access variables, and can't change them,
    and because of this are unrelated
    };
    }

    --When I start up the program, I get these errors in Table.cpp:
    C2512: 'tableNS::bst' : no appropriate default constructor available
    C2512: 'tableNS::bst' : no appropriate default constructor available

    --The error points to the Table constructor in Table.cpp:
    namespace tableNS
    {
    Table::Table() { // Error points to this line

    }
    

    }

    --But I don't know how to initialize Table's two bst variables. If they were pointers I could use
    authorIdBst = new bst(data);

    --but these aren't pointers, and I haven't learned how to initialize non-pointer classes. They can't be pointers though, because Table.h was made by my instructor, and I need to make the Table.cpp.

    --I've tried:
    authorIdBst = bst(&data);
    isbnBst = bst(&data);
    --but that doesn't change the error.

    --Any help on how to initialize these two variables?

     
    • BiT

      BiT - 2007-12-06

      not sure how this can be offending but oh well

      "post your basic 3 <---- can be found in the please read me thread"

       
      • Anonymous

        Anonymous - 2007-12-07

        Perhaps, but "no appropriate default constructor available" and the code and explanation provided rather says it all.

        My suspicion given the ridiculous constraints is that this is homework. My suspicion also is that the OP has perhaps misunderstood the question.

         
      • Osito

        Osito - 2007-12-06

        You said the T word! I think I was the one who offended him. It just didn't make sense to post compiler errors from Microsoft - they have forums of their own for that. Oh well.

         
    • BiT

      BiT - 2007-12-06

      post your basic 3 <---- can be found in the please read me thread

       
    • CoryLehan

      CoryLehan - 2007-12-06

      Sorry if it looks otherwise, but I'm only asking a general question.

      I'm only asking how to initialize classes without a default constructor or pointer.
      The code is only there to give an example of the problem.

      I don't see how it matters here, but I'm using Visual Studio 2005 with Windows XP, and I gave the compile errors and where they were located.

       
      • Osito

        Osito - 2007-12-06

        It sure seems like it matters to me - you're asking questions about compiler errors in the Dev-Cpp forum, yet you're using the competitor's software. I understand you're trying to get general information, but maybe it would have been better to post the question itself rather than the results from VS2005.

         
        • CoryLehan

          CoryLehan - 2007-12-06

          Well I can see asking here is getting me nowhere.

          I've frequently come to this forum because it was fast, helpful, and friendly toward every problem I've had, when I used Dev-C++ and when I didn't.

          You don't answer my question because of Competitor's software!? When the problem has nothing to do with what software I'm using...
          I can see that this forum has changed a great deal since I first came here.

          I don't think I'll be back again. I wouldn't bother answering my question. I'll look for a different forum.

           
    • Anonymous

      Anonymous - 2007-12-06

      You have two choices:

      1) Declare the members as bst* and instantiate them with the 'new' operator in Table::Table()
      2) Add a default constructor to class bst.

      Those are your only two choices, and for some reason undisclosed you won't use pointers, so there's your answer. You cannot instantiate an object through a non-existent constructor. You may also want to add initialisation functions to bst so that you can initialise instances that were instantiated using the default constructor. The other two constructors should call these init functions to avoid code duplication.

      You have an instructor - then ask him - that is what he is paid for. Getting over-sensitive about responses on a public forum is not going to get you far - not everyone is nice, or suffers fools gladly. You choose to post here, but you cannot choose your responses or who responds. To go off in a childish sulk because just two of the many correspondents on the forum offended you is a bit weak to be frank.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.