Thread: Class Problems

  1. #1
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Class Problems

    Hey,

    Anyone mind pointing out what I'm doing wrong?

    I've got objects of two different classes in main(). The two classes both work with these two objects, so both require the reference to eachothers objects. e.g. class1 requires class2's object on construction, and visa versa.

    Code:
    int main()
    {
      CVector* Vectors;
      CPlane* Water;
      Vectors = new CVector(Water);
      Water = new CPlane(Vectors);
    }
    
    /*
    They will both be linked lists eventually. CVector is simply x, y, z
     points, in a linked list fashion. CPlane is a 2d plane which uses
     CVector to plot the points, but also has a function which can be
     used to 'reflect properties' (like a mirror) of the vectors in front of
     it (since this is Water (on no angle, facing up), it reflects vectors
     at the y axis). 
    */
    My problem is that the constructor is requiring I initialize the variable holding the object information, or else it returns an error: 25 C:\Programming\Projects\Game\v0003\Vector.h uninitialized reference member `CVector::m_Registrar'. The below code is what I'm forced to do.

    Code:
    class CVector {
    private:
      CPlane*& m_Registrar;
    
    public:
      CVector(CPlane*& p_Registrar) : m_Registrar(p_Registrar) {}
    };
    I would prefer not having to do that since there are some cases I wouldnt want to keep passing an object to m_Registrar, when I don't plan on using the m_Registrar variable. Anyone know the way around this?

    I've also got a part of code that sends the points its searching for in the linked list to the vector object, using Search(), then when it finds the object match, it returns the object, which is the object I want m_Vectors to become.

    m_Vectors->Search() returns a CVector

    Code:
    CVector CVector::Search(long p_id) {}
    ...
    
      CVector*& m_Vectors;
    
      m_Vectors = m_Vectors->Search();
    
    returns error:
    145 C:\Programming\Projects\Game\v0003\Vector.cpp cannot convert ` CVector' to `CVector*' in assignment
    Is overloading the operator= and doing all the converting manually the only way? or cant I return a pointer to the CVector object, and set m_Vector to that, since is its a reference to a pointer. I forget.. now I'm just trying to please the compiler, then I'll go back and fix it after

    I might just stop and read, I've obviously lost all concept of C++ pointers and references, and some rules.

    Dae
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Why are you using pointers to references? Why won't regular pointers work? Also you can't declare a reference to something and not intialize it. Like this line here should give you an error.

    Code:
    CVector*& m_Vectors;
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by MrWizard
    Why are you using pointers to references? Why won't regular pointers work? Also you can't declare a reference to something and not intialize it. Like this line here should give you an error.

    Code:
    CVector*& m_Vectors;
    Yeah I said it was giving me an error. I was using pointers and references because it was giving me errors, which I figured I'd just fix later. I just went over it and removed all the references, so it no longer requires initializing m_Vectors, thanks for that.

    Now I can use *m_Vectors = Search();, and its working. Now all I have is some logic problem thats causing my program to stall and crash after 20 seconds.

    //Fixed.
    Last edited by Dae; 09-18-2005 at 04:45 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. class Template problems
    By Mr_roboto in forum C++ Programming
    Replies: 8
    Last Post: 02-16-2006, 10:21 PM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM