Jump to content
  • Advertisement
Sign in to follow this  
Sagar_Indurkhya

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

This topic is 4422 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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. :(

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!