Thread: object array problems (some openGL but its pretty irrelevant to the problem)

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    11

    object array problems (some openGL but its pretty irrelevant to the problem)

    I have this project im working on. It will eventually be an implementation of Dijkstra's shortest path algorithm using some c++ classes and openGL to visualize it.

    My problem is creating a data structure to hold an array of nodes that I can draw to the screen. Im sure there is something im missing, ive researched online for hours but I couldnt find anything that could help me with my problem. Here is the code thats giving me problems:

    Node is declared in a header file. Ill go ahead and include that whole file.
    "Node.h":
    Code:
    #include <GL/glut.h>
    #include <GL/glu.h>
    #include <GL/gl.h>
    
    class Node {
    	private: 
    		GLfloat x, y, z;
    		int numLinks;
    		Node *links[];
    	public: 
    		//Node(GLfloat x, GLfloat y, ...);
    		Node();
    		Node(GLfloat x, GLfloat y);
    		~Node();
    		GLfloat getNodeX();
    		GLfloat getNodeY();
    		GLfloat getNodeZ() {return 0.0f;}
    		GLvoid setNodeX(GLfloat x);
    		GLvoid setNodeY(GLfloat y);
    } Node;
    
    Node::Node() {
    	this->x = 0.0f;
    	this->y = 0.0f;
    	this->z = 0.0f;
    }
    
    Node::Node(GLfloat x, GLfloat y) {
    	this->x = x;
    	this->y = y;
    	this->z = 0.0f;	
    }
    
    /*Node::Node(GLfloat x, GLfloat y, int numLinks, ...) {
    	va_list arguments;
    	va_start(arguments, numLinks);
    	for (int i = 0; i < numLinks; i++) {
    		this.links[i] = va_arg(arguments, *Node);
    	}
    	this.x = x;
    	this.y = y;
    	this.z = 0.0f;	
    }*/
    
    GLfloat Node::getNodeX() {
    	return this->x;
    }
    
    GLfloat Node::getNodeY() {
    	return this->y;
    }
    
    GLvoid Node::setNodeX(GLfloat x) {
    	this->x = x;
    }
    
    GLvoid Node::setNodeY(GLfloat y) {
    	this->y = y;
    }
    
    Node::~Node() {
    }
    Global array:
    Code:
    Node *nodes[NUM_NODES];
    Code:
    int makeNodes() {
    	nodes[0] = new Node(0.0f, 0.0f);
    }
    g++ gives me these errors which correspond to the 4 lines above. The first error being the global variable.
    DijkstraShortestPath.cpp:11:1: error: ‘Node’ does not name a type
    DijkstraShortestPath.cpp: In function ‘int makeNodes()’:
    DijkstraShortestPath.cpp:58:2: error: ‘nodes’ was not declared in this scope
    DijkstraShortestPath.cpp:58:17: error: expected type-specifier before ‘Node’
    DijkstraShortestPath.cpp:58:17: error: expected ‘;’ before ‘Node’

    I would appreciate some suggestions on how to fix this, or even suggestions on a better way to keep track of my nodes.
    Last edited by wassat676; 07-12-2011 at 10:56 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to include "Node.h" if you want to use the stuff inside it.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    11
    I know, and i did.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also, don't do this:
    Code:
    class Node {
    	private: 
    		GLfloat x, y, z;
    		int numLinks;
    		Node *links[];
    	public: 
    		//Node(GLfloat x, GLfloat y, ...);
    		Node();
    		Node(GLfloat x, GLfloat y);
    		~Node();
    		GLfloat getNodeX();
    		GLfloat getNodeY();
    		GLfloat getNodeZ() {return 0.0f;}
    		GLvoid setNodeX(GLfloat x);
    		GLvoid setNodeY(GLfloat y);
    } Node;
    You've got a variable now masking your type and that's a shame. The Node at the end isn't needed.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    11
    Thats a totally legal move, and it has nothing to do with my problem.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by wassat676
    Thats a totally legal move, and it has nothing to do with my problem.
    Considering this error:
    Code:
    DijkstraShortestPath.cpp:11:1: error: ‘Node’ does not name a type
    I think it has a great deal to do with your problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    11
    I stand corrected. thanks.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Also, with regard to it being a legal move: it isn't because your putting a global variable definition in a header file, without extern or static. This will lead to an error if you include that header in two different files.

    Not that you should fix the problem by making the variable static or extern...
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-17-2006, 08:03 AM
  2. Clicking an Object in OpenGL.
    By gpr1me in forum Game Programming
    Replies: 5
    Last Post: 03-26-2006, 10:12 PM
  3. How to eliminate irrelevant conditionals?
    By cdave in forum C Programming
    Replies: 9
    Last Post: 12-10-2005, 04:39 PM
  4. Array of Pointers + Deleting An Object = Problems
    By Nereus in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2004, 12:16 PM
  5. Irrelevant question?
    By f0ul in forum Linux Programming
    Replies: 25
    Last Post: 05-25-2002, 12:16 AM