-1

I'm a noob. Using C++ in Clion

I'm building a graph of N random nodes on a Cartesian plane

I have a simple type, node (just a point) (int x, int y)
node pt(x,y)

I have a vector of N randomly generated unique points (would this be considered ordered points btw?) vector NodeList(N);

I have a class Graph (Incomplete) which has a function GenNodelist which I have tested as a standalone program. I had a hell of a time just getting the constructor to build without compile error.

    #include <iostream>
    #include <vector>
    #ifndef DIJKSTRA_GRAPH_H
    #define DIJKSTRA_GRAPH_H

    using namespace std;

    class Graph {

        private:
            int x;
            int y;
            vector<int> NodeList;
            int *np;
     public:

     //constructor
   x(x),y(y),NodeList(),np(){}


    void GenNodeList(vector<int> NL(), int &np) {x,y,NodeList, &np; }

    void GenNodeList(vector<int> *NL, int *p);

    };


    #endif //DIJKSTRA_GRAPH_H


void Graph::GenNodeList(vector<int>* NL, int* p) {
                             .
                             .
 }                            .
...  code that builds and has been quasi tested 

So everything builds and there's a "hello world" main program in the project. The 2 classes, (node & Graph: 2 headers and 2 cpp files) along with the main "hello world" build and run. Now from main() I wan to call the call the GenNode function from main. I just want to pass a pointer and have the list generate and sit in memory UN-mutable. right now. I'll build the graph off of this later. When I try to call the function nothing works. How can I build this list and access it from main() and Graph()?

main(){
    vector<int> NL(N);
    int *np;

    Graph::GenNodeList( NL, np);
}

Can't seem to figure this out.

  • 1
    The code you provided is nowhere near close to compiling. Please create an actual [mcve] – UnholySheep Mar 21 '18 at 22:30
  • If it's minimal it won't compile. Of course this doesn't compile. – Pasqualino31 Mar 21 '18 at 22:39
  • 2
    I don't think you understand the meaning of [mcve] - without one we cannot help you. Also the code you provided fails to compile due to many more reasons than being "minimal" – UnholySheep Mar 21 '18 at 22:42
  • My code compiles just fine with the exception of the function call in main() – Pasqualino31 Mar 21 '18 at 22:58
  • I can run everything in this project up to the function call in main. There is no minimal, complete verifiable example so I guess you can't help me. – Pasqualino31 Mar 21 '18 at 23:02
  • I'm going to try to work with smart pointers of which I know nothing, but the definition seems to fit and it seems like a good direction in which to look. It would have been a good suggestion. I built this whole project out of complete minimal and verifiable pieces. I don't need anyone to verify my minimal, complete examples. There is more than sufficient material out there to do it myself. – Pasqualino31 Mar 21 '18 at 23:37
  • It is direction in the context of the greater project where difficulty lies. I need to create and then access this vector of immutable objects from several places. HTF am I to present this problem in a minimal , complete and verifiable example of code? – Pasqualino31 Mar 21 '18 at 23:38
  • 1
    @Pasqualino31 This is "HTF" you can present your problem in a minimal, complete and verifiable example of code: `void addOne(vector* v) { v.push_back(1); } int main() { vector vec; addOne(vec); cout << vec[0]; }` – Max Vollmer Mar 21 '18 at 23:55
  • Possible duplicate of [How to pass objects to functions in C++?](https://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c) – Max Vollmer Mar 21 '18 at 23:59
  • `main()` is not legal C++. Please write `int main()` and also use compiler flags to enable common warnings (`-Wall -Werrror -pedantic` is a good set). – n. m. could be an AI Mar 22 '18 at 10:06
  • I'm also learning how to use GitHub and made a repository for this project. Right now is one big source file Which generates the N, random and unique nodes and then I make a graph by assigning random connections and use the distance the connected nodes as the edge weight. The assignment requires several classes with some particular functions, so I need to break up the big sloppy mess into an elegant C++ project. This is where I'm running into problems. Anyone who wants to laugh at my stuff can go here. https://github.com/Pasqualino31/Dijkstra – Pasqualino31 Mar 22 '18 at 14:55
  • I have no clue how to set compiler flags. – Pasqualino31 Mar 22 '18 at 19:46

2 Answers2

0

This incomplete piece of code

Graph::GenNodeList( NL, np);

is ill-formed because that method is not static. You have to access instance of class Graph for non-static members. Nothing about oop here, just language's rules.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
0
Graph():   x(x),y(y),NodeList(),np(){}

static void GenNodeList(vector<node>* NL, node* np);

void Graph::GenNodeList(vector<node>* NL, node* p) {

int main() {
    vector<node> NL(N);
    vector<node>* NList;
    node *np = nullptr;

    NList = &NL;

    Graph::GenNodeList(NList,np);

return 0;

https://github.com/Pasqualino31/Dijkstra/tree/Pasqualino31-patch-2