Skip to main content
gamedev.net
🔒 Locked

Function Pointers and Forward Declaration... Damn, the third time too!

Started by Sagar_Indurkhya 3 replies 950+ views
Original Post
Sagar_Indurkhya
Sagar_Indurkhya
Yes, the two parts of C++ that I have the most trouble with, put together! I was wondering if someone could help me with this. I have a graph structure, with nodes and links(sorry, I should have used Verticies and Edges). Link depends on Node, and Node depends on Link, so I remember I'm suppose to do a forward declaration. However it's not working. Also, I think my function pointer in Node class is screwy(as my VS 2005 compiler tells me). Node.h

#pragma once
#include <list>
#include "Link.h"

using namespace std;

class Node
{
public:

	double oldValue;
	double value;

	list<Link*> inputs;

	double (*inputFunc)((list<double>) &); // Compiler says something is wrong here!

	Node(void)
	{
	}

	~Node(void)
	{
	}

	double getValue()
	{
		return oldValue;
	}

	void ComputeValue()
	{
		list<double> inputValues;

		for(list<Link*>::iterator iter = inputs.begin(); iter != inputs.end(); iter++)
			inputValues->push_back((*iter)->getValue());

		value = (*inputFunc)(inputValues);
	}

	void CleanUp()
	{
		oldValue = value;
		value = 0.0;
	}

};

Link.h

#pragma once

class Node;

class Link
{
public:

	Node* A;
	Node* B;

	int linkType;

	double (*inputFunc)(double);

	Link(Node* Alpha, Node* Beta, double (*inputFunction)(double))
	{
		if(Alpha)
			A = Alpha;
		if(Beta)
			B = Beta;
		if(inputFunction)
			inputFunc = inputFunction;
	}

	~Link(void)
	{
		A = B = NULL;
	}

	double getValue()
	{
		return (*inputFunc)(A->getValue());
	}
};

Can anyone see if anything is popping up as wrong? I don't see where I am going wrong. :(
Alpha_ProgDes
Alpha_ProgDes
i'm not sure about the forward declaration but i remember the function pointer for classes having a different syntax then what you have there.

i believe you forgot a: class Link; in your Node.h file.
Beginner in Game Development?  Read here. And read here.  
kuphryn
kuphryn
class x
class y

void (x::*px)()
void (y::*py)()

Kuphryn
Omega147
Omega147
Your forward declaration of the classes seems right. As for the function pointer, not sure... I haven't used function pointers much.

What error is the compiler giving you?
ZMaster
ZMaster
In your file link.h you forward declared Node but, which is okay, but then in your method definitions of Link you're referring to members of Node. You can't access members of a forward declared class since at the time of compilation there is no information about the members of that forward declared class available.
So a suggestion would be that you move the definitions of all methods which require access to the class members of Node (getValue this would be in this particular case) to a file named link.cpp. In this file also include node.h... that should work!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.