Thread: Reference from a class to take a sub class

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    Reference from a class to take a sub class

    Sorry, my title may not be very descriptive. Perhaps I can do better here

    I have a class called Node. I am subclassing it to make a class called CustomNode.
    Another class takes a reference to a vector of Node pointers:

    Code:
    void getNeighbouringNodes(Node *n, std::vector<Node*> &nodes);
    I am calling it like this:

    Code:
      std::vector<CustomNode*> *nodes;
      nodes = new (std::vector<CustomNode*>);
      graph->getNeighbouringNodes(n, *nodes);
    I get the error message:

    error: non-const lvalue reference to type 'vector<class graphDB::Node *>' cannot bind to a value of unrelated
    type 'vector<class CustomNode *>'
    I understand the error I think: the method getNeighbouringNodes is expecting a refernce to a vector of Node pointers, but I am passing it a reference to a vector of CustomNode pointers. CustomNode pointers is a subclass of Node so I expect somehow I can pass a reference vector of CustomNode pointers into something expecting a reference vector of Node pointers?????

    Thanks
    Alex

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your CustomNode is derived from Node. However, a vector<CustomNode*> is NOT derived from vector<Node *>.

    A vector<Node *> can have CustomNode *'s as elements though, which is probably what you need to do.

    It is also not necessary for nodes to be a pointer, nor to dynamically allocate it. It can simply be an instance of a vector<Node *>.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can also use static polymorphism:

    template<typename Node_t>
    void GetNeighbouringNodes(Node_t* n, std::vector<Node_t*>& nodes);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    You can also use static polymorphism:

    template<typename Node_t>
    void GetNeighbouringNodes(Node_t* n, std::vector<Node_t*>& nodes);
    Maybe, although - from info the OP has provided - I doubt it is warranted in this case.

    For example, it's a fair bet that the GetNeighbouringNodes() member function attempts to copy additional pointers to Node into the array passed - which won't work if a vector<CustomNode *> is passed. Static polymorphism does not help with that (except in a few very specific circumstances).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class reference members
    By nimitzhunter in forum C++ Programming
    Replies: 3
    Last Post: 11-24-2010, 05:29 AM
  2. Initializing a class reference inside another class...
    By Raigne in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2008, 12:36 AM
  3. Can I not reference another class within a class?
    By cpudaman in forum C++ Programming
    Replies: 17
    Last Post: 01-07-2008, 02:26 AM
  4. returning reference from class
    By l2u in forum C++ Programming
    Replies: 17
    Last Post: 12-16-2007, 11:21 AM
  5. C++ class reference
    By whitenoise in forum Linux Programming
    Replies: 2
    Last Post: 05-15-2002, 05:14 PM