Reputation: 23
I am currently facing a problem with structures in C++. Follow the code below and then I can explain the problem.
void TestFunction(testStructure *tempStructValue)
{
cout << tempStructValue->data << endl;
return &tempStructValue->data;
}
typedef struct testStructure
{
int data;
void *TestFunction(testStructure *tempStructValue)
}temp;
The function is defined before the struct. This way, the function has a parameter as the struct but the struct is defined after the definition of function. This obviously is an error.
Now, think the other way round. Define the struct before the function. Doing this would still throw an error because the function would be defined later in the code and the struct would search for the function.
Is there a solution to this chicken and egg problem?
Upvotes: 1
Views: 1219
Reputation: 24249
You can "forward declare" a class so that it can be referred to by either a pointer or a reference ahead of it's complete definition.
class MyClass; // forward declaration
void fn1(MyClass* ptr); // legal
void fn2(MyClass* ptr) // legal
{
ptr->something; // illegal, definition of MyClass unknown
}
class A {
MyClass mc; // illegal, definition of MyClass unknown
};
class MyClass {
// ...
};
However, in your example, you wrote the following:
typedef struct testStructure
{
int data;
void *TestFunction(testStructure *tempStructValue)
} temp;
this declares a class, testStructure
, which as an integer member data
and a member function TestFunction
which returns a void*
pointer. This member function has absolutely no connection to the previous, free function TestFunction
. It is unclear what your intent is.
Example:
#include <iostream>
struct TestStructure;
void TestFunction(TestStructure* /*unused*/) {
std::cout << "Free TestFunction\n";
}
struct TestStructure {
int i;
void TestFunction(TestStructure* /*unused*/) {
std::cout << "TestStructure TestFunction\n";
}
void foo() {
std::cout << "From foo\n";
TestFunction(this);
}
};
int main() {
std::cout << "From main:\n";
TestFunction(nullptr);
TestStructure t;
t.foo();
}
The same is true if they are declared the other way around: http://ideone.com/VDcJxe
This is because names are resolved based on scope. Member functions of the class/struct will consult the class/struct's own scope before looking else where. If we wanted to call the free function from inside a member function, we could use the '::' scope operator as such:
void foo() {
::TestFunction(); // resolves from the global scope
}
See: http://ideone.com/B51J4C (note I chose to forward declare the function which in turn requires that I forward declare the class for it's argument).
Upvotes: 1
Reputation: 559
If it is a c code. Seperating file into 3 files may solve the problem. Making one header file for struct and one for the function will may be solution. Include function heder in struct file. Then include struct heder in function's c file. But if your code is written in c++ (cout makes me think it is more probable) you should try some OOA, OOD and may be design patterns techniques for the solution for your needs.
Meanwhile return value of the function must be a pointer type.
Upvotes: 0