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

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

## 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>

using namespace std;

class Node
{
public:

double oldValue;
double value;

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;
}

};


#pragma once

class Node;

{
public:

Node* A;
Node* B;

double (*inputFunc)(double);

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

{
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 on other sites
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.

class x
class y

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

Kuphryn

##### 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 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!