C++ 成员函数
编译时,我在“void operation”行遇到一个错误,因为我还没有定义Gate_ptr。我想在函数def中将“Gate_ptr”替换为“Gate*”。然而,有没有办法保持我目前的风格C++ 成员函数,c++,class,compiler-errors,member-functions,C++,Class,Compiler Errors,Member Functions,编译时,我在“void operation”行遇到一个错误,因为我还没有定义Gate_ptr。我想在函数def中将“Gate_ptr”替换为“Gate*”。然而,有没有办法保持我目前的风格 class Gate { public: Gate(); void operation(Gate_ptr &gate_tail, string type, int input1, int input2=NULL);
class Gate
{
public:
Gate();
void operation(Gate_ptr &gate_tail, string type, int input1, int input2=NULL);
private:
int cnt2;
int input_val1, input_val2;
int output, gate_number;
int input_source1, input_source2;
int fanout[8];
Gate* g_next;
string type;
};
typedef Gate* Gate_ptr;
我更喜欢这个顺序:
//forward decleration
class Gate;
//typedef based on forward dec.
typedef Gate* Gate_ptr;
//class definition
class Gate
{
public:
//...
};
Forwared declare,执行typedef,然后定义类:
class Gate;
typedef Gate* Gate_ptr;
class Gate
{
public:
Gate();
void operation(Gate_ptr &gate_tail, string type, int input1, int input2=NULL);
private:
int cnt2;
int input_val1, input_val2;
int output, gate_number;
int input_source1, input_source2;
int fanout[8];
Gate* g_next;
string type;
};
不,编译器在编译代码之前必须知道类型。它没有提前阅读。为什么不先声明类型呢?
intinput=NULL
(在操作
方法的参数列表中)只是wierd(尽管合法)。可能是int*input=NULL
或int-input=0
?这是在我访问函数时使用的,我决定不将参数传递到“input2”NULL
定义为0,但当需要将整数变量赋给0时,应该使用0
而不是NULL
NULL
应用于指针。在某些平台/编译器中,NULL
可能有void*
类型代码>是一个转发声明。当您有两个彼此包含的标题时,这是一个非常有用的特性。