Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3226522
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:28:00+00:00 2026-05-17T16:28:00+00:00

I have the following class class node { public: node() { } node(const node&);

  • 0

I have the following class

class node {
public:
    node() { }
    node(const node&);
    node(luint inID) { ID = inID; }
    ~node() { neighbors.clear(); }

    node& operator=(const node&);
    void addNeighbor(luint);
    void addNeighbor(const std::vector<luint>& );
    void setID(luint inID) { ID = inID; }
    luint getID() const { return ID; }
    std::vector<luint> & getNeighbors() const { return neighbors; }
protected:
    luint ID;
    std::vector<luint> neighbors;
};
void node::addNeighbor(const std::vector<luint>& inID) {
    for(int i = 0; i < inID.size(); i++)
        neighbors.push_back( inID[i] );
}
// etc..

right now the error I get is

graph.h: In member function 'std::vector<long unsigned int, std::allocator<long unsigned int> >& node::getNeighbors() const': In file included from main.cpp:10:
graph.h:28: error: invalid initialization of reference of type 'std::vector<long unsigned int, std::allocator<long unsigned int> >&' from expression of type 'const std::vector<long unsigned int, std::allocator<long unsigned int> >'
make[2]: *** [build/Debug/GNU-MacOSX/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

on the other hand, if I remove “const” at the declaration of function “getNeighbors()” I get the error

graph.cpp: In member function 'void graph::addNode(const node*)':
graph.cpp:36: error: request for member 'addNeighbor' in '((graph*)this)->graph::nodeMap. std::map<_Key, _Tp, _Compare, _Alloc>::operator[] [with _Key = long unsigned int, _Tp = node*, _Compare = std::less<long unsigned int>, _Alloc = std::allocator<std::pair<const long unsigned int, node*> >](((const long unsigned int&)((const long unsigned int*)(& inNode-> node::getID()))))', which is of non-class type 'node*'
graph.cpp:36: error: passing 'const node' as 'this' argument of 'std::vector<long unsigned int, std::allocator<long unsigned int> >& node::getNeighbors()' discards qualifiers
make[2]: *** [build/Debug/GNU-MacOSX/graph.o] Error 1

Does anyone know how to fix this issue?

Thank you in advance,


Update:

class node {
public:
    node() { }
    node(const node&);
    node(luint inID) { ID = inID; }
    ~node() { neighbors.clear(); }

    node& operator=(const node&);
    void addNeighbor(luint);
    void addNeighbor(const std::vector<luint>& );
    void setID(luint inID) { ID = inID; }
    luint getID() const { return ID; }
    std::vector<luint> & getNeighbors() { return neighbors; }
    std::vector<luint> const & getNeighbors() const { return neighbors; }
protected:
    luint ID;
    std::vector<luint> neighbors;
};

class graph {
public:
    graph() { }
    ~graph() { }

    void addNode(const node*);
    void addNode(const node&);
protected:
    std::map<luint, node*> nodeMap;
};

void node::addNeighbor(luint inID) {
    neighbors.push_back(ID);
}

void node::addNeighbor(const std::vector<luint>& inID) {
    for(int i = 0; i < inID.size(); i++)
        neighbors.push_back( inID[i] );
}

void graph::addNode(const node* inNode) {
    nodeMap[inNode->getID()] = new node(inNode->getID());
    nodeMap[inNode->getID()].addNeighbor( inNode->getNeighbors() );
}

void graph::addNode(const node& inNode) {
    nodeMap[inNode.getID()] = new node(inNode.getID());
}

compilation error:

graph.cpp: In member function 'void graph::addNode(const node*)':
graph.cpp:36: error: request for member 'addNeighbor' in '((graph*)this)->graph::nodeMap. std::map<_Key, _Tp, _Compare, _Alloc>::operator[] [with _Key = long unsigned int, _Tp = node*, _Compare = std::less<long unsigned int>, _Alloc = std::allocator<std::pair<const long unsigned int, node*> >](((const long unsigned int&)((const long unsigned int*)(& inNode-> node::getID()))))', which is of non-class type 'node*'
make[2]: *** [build/Debug/GNU-MacOSX/graph.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:28:01+00:00Added an answer on May 17, 2026 at 4:28 pm

    I modified the code by explicit casting and it works

    class node {
        friend class graph;
        friend ostream& operator<< (ostream&, const node&);
    public:
        node() { }
        node(const node&);
        node(luint inID) { ID = inID; }
        ~node() { neighbors.clear(); }
    
        node& operator=(const node&);
        void addNeighbor(luint);
        void addNeighbor(const std::vector<luint>& );
        luint getID() const { return ID; }
    
    
    protected:
        luint ID;
        std::vector<luint> neighbors;
    };
    
    class graph {
        friend ostream& operator<< (ostream&, graph&);
    public:
        graph() { }
        ~graph() { }
    
        void addNode(const node& );
        void addNode(const node* );
        void readFile(const char * );
        void clearMap();
    protected:
        std::map<luint, node*> nodeMap;
    };
    
    void graph::addNode(const node& inNode) {
        nodeMap[ (const luint)inNode.ID ] = new node( (const luint) inNode.ID );
        nodeMap[ (const luint)inNode.ID ]->neighbors.resize( (const luint) inNode.neighbors.size() );
        *nodeMap[ (const luint)inNode.ID ] = inNode;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class class Node { int key; Node**Nptr; public: Node(int maxsize,int
I have the following class public class Car { public Name {get; set;} }
I have the following class: public abstract class AbstractParent { static String method() {
I have the following class [XmlRoot(ElementName= webSites)] //No capital w at the beginning public
I have the following class objects: public class VacancyCategory { public int ID {
Say I have the following class MyComponent : IMyComponent { public MyComponent(int start_at) {...}
Let's have the following class hierarchy: public class ParentClass implements SomeInterface { } public
I have the following class which uses BinaryReader internally and implements IDisposable. class DisposableClass
I have the following class structure FlowerDAO with the fields (mapped using Hibernate): id
I have the following class in my C# .NET 3.5 win forms app: class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.