我有一個雞和雞蛋的問題。我想將一個數據結構傳遞給一個包含指向需要該結構的例程的指針的例程。例程指針結構struct作爲參數
我做了一個非常簡單的例子。
我需要在定義之前使用CalcDataStruct,如果我在結構之後添加它,FuntctionPrototype沒有定義。
我運行的問題只在前兩行,其餘的可能包含一些語法錯誤,因爲我沒有在編譯器中檢查過這個問題。
typedef void(*FunctionPrototype)(CalcDataStruct *Ptr);
struct CalcDataStruct
{
int A, B, C, D;
int Values;
char SignA, Sign B;
int Result;
FunctionPrototype Routine;
}
struct ScanStruct
{
char Sign;
int Values;
FunctionPrototype Routine;
};
const ScanStruct ExampleList[] =
{
{ '+', 2, AddTwo },
{ '+', 3, AddThree }
};
void AddTwo(CalcDataStruct *Ptr)
{
// use the data and if needed put it back
}
void AddThree(CalcDataStruct *Ptr)
{
// use the data and if needed put it back
}
void GetFunction(CalcDataStuct *Ptr, ScanStruct *List)
{
// Very simple return based on nothing
Ptr->Routine = *List[(1)].Routine;
}
void main()
{
CalcDataStruct A;
// struct is filled
// Fill in the routine pointer based on data
GetFunction(A, ExampleList)
// Execute the routine fetched with all the data
A->Routine(A)
}
這是一個C或C++的問題?請最多選一個。 – fuz
刪除了C標籤。但解決方案對於兩者都有效。 – BWonder
這很可能,但不能提前說。有些事情在C中起作用,在C++中不起作用,反之亦然。這就是爲什麼您一次詢問一種語言的政策。 – fuz