前向参考 [英] Forward References

查看:57
本文介绍了前向参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到前向推荐问题。


例如,


班级日期;

class MYCLASS;


class MYCLASS

{

public:

int n;

日期日期;

};


班级日期

{

public:

int day;

int month;

int year;

};


int main(int argc,char * argv)

{

返回0;

}

结果

g ++ -c -DTARGET_PC -Wall -g -I /。 -I / mingw / include / -I / devstudio / vc / include -o ./pc/impmain.o-Wno-deprecated impmain.cpp

impmain.cpp:8:field`date''有不完整的类型

make:*** [pc / impmain.o]错误1


编译退出异常,代码2在星期六十月16 12:54 :27

为什么?我已经在文件开头声明了前向引用。是

这只是g ++的愚蠢??

-

%Randy Yates%"看着所有的日子过去......

%% Fuquay-Varina,NC%你是谁,我是谁?

%%% 919-577-9882%''使命(世界纪录) )'',

%%%%< ya *** @ ieee.org> %*新的世界纪录*,ELO
http://home.earthlink。 net /~yatescr

解决方案

Randy Yates< ya *** @ ieee.org>写道:

我遇到了前向引用的问题。

例如,

类DATE;
MYCLASS课程;

MYCLASS课程
{
公开:
int n;
DATE date;
};
<课程日期
{
公开:
int day;
int month;
int year;
};
int main(int argc,char * argv)
{
返回0;
}

结果

g ++ -c - DTARGET_PC -Wall -g -I /。 -I / mingw / include / -I / devstudio / vc / include -o ./pc/impmain.o-Wno-deprecated impmain.cpp
impmain.cpp:8:field`date''的类型不完整< br:> make:*** [pc / impmain.o]错误1

编译退出异常,代码2在10月16日星期六12:54:27

为什么?我已经在文件开头声明了前向引用。这只是g ++的愚蠢吗?




我忘了添加,如果我切换类声明的顺序

错误消失了。我不应该订购类声明

,它在我的文件中有正向引用,就像我没有必要

订购子程序,如果它们有原型定义。

-

%Randy Yates%梦想家,未知的傻瓜 -

%% Fuquay-Varina,NC%梦想,没有痛苦会吻着眉头......

%%% 919-577-9882%

%%%%< ya *** @ ieee。有机> %''Eldorado Overture'',* Eldorado *,ELO
http:// home.earthlink.net/~yatescr


Randy Yates发布:

我遇到了问题有正向参考。

例如,课程日期;
课程MYCLASS;

课程MYCLASS
{
public:
int n;
DATE date;
};

class DATE
{
public:
int一天;
int month;
int year;
};

int main(int argc,char * argv)
返回{
0;
}



重新排列:


(加上我更正了一个错字,它本来应该是'argv []")

class DATE

{

public:

int int;

int month;

int year;

};


class MYCLASS

{

公开:

int n;

日期日期;

};

int main(int argc,char * argv [])

{

返回0;

}

-JKop


Randy Yates写道:

为什么?我已经在文件开头声明了前向引用。这只是g ++的愚蠢吗?




前向声明只允许声明指针或引用,以声明

an该类对象需要完整的声明。


-

Salu2


I''m having a problem with forward references.

For example,

class DATE;
class MYCLASS;

class MYCLASS
{
public:
int n;
DATE date;
};

class DATE
{
public:
int day;
int month;
int year;
};

int main(int argc, char* argv)
{
return 0;
}
results in
g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I /devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
impmain.cpp:8: field `date'' has incomplete type
make: *** [pc/impmain.o] Error 1

Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27
Why? I''ve declared the forward reference at the beginning of the file. Is
this just a stupidity of g++??
--
% Randy Yates % "Watching all the days go by...
%% Fuquay-Varina, NC % Who are you and who am I?"
%%% 919-577-9882 % ''Mission (A World Record)'',
%%%% <ya***@ieee.org> % *A New World Record*, ELO
http://home.earthlink.net/~yatescr

解决方案

Randy Yates <ya***@ieee.org> writes:

I''m having a problem with forward references.

For example,

class DATE;
class MYCLASS;

class MYCLASS
{
public:
int n;
DATE date;
};

class DATE
{
public:
int day;
int month;
int year;
};

int main(int argc, char* argv)
{
return 0;
}
results in
g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I /devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
impmain.cpp:8: field `date'' has incomplete type
make: *** [pc/impmain.o] Error 1

Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27
Why? I''ve declared the forward reference at the beginning of the file. Is
this just a stupidity of g++??



I forgot to add that if I switch the order of the class declarations
the error goes away. I shouldn''t have to order class declarations
which have forward references in my files, just as I don''t have to
order subroutines if they have prototype definitions.
--
% Randy Yates % "The dreamer, the unwoken fool -
%% Fuquay-Varina, NC % in dreams, no pain will kiss the brow..."
%%% 919-577-9882 %
%%%% <ya***@ieee.org> % ''Eldorado Overture'', *Eldorado*, ELO
http://home.earthlink.net/~yatescr


Randy Yates posted:

I''m having a problem with forward references.

For example,

class DATE;
class MYCLASS;

class MYCLASS
{
public:
int n;
DATE date;
};

class DATE
{
public:
int day;
int month;
int year;
};

int main(int argc, char* argv)
{
return 0;
}


Rearrange it:

(Plus I corrected a typo, it should''ve been "argv[]")
class DATE
{
public:
int day;
int month;
int year;
};

class MYCLASS
{
public:
int n;
DATE date;
};
int main(int argc, char* argv[])
{
return 0;
}
-JKop


Randy Yates wrote:

Why? I''ve declared the forward reference at the beginning of the file. Is
this just a stupidity of g++??



Forward declaration allow only to declare pointer or references, to declare
an object of the class the complete declaration is required.

--
Salu2


这篇关于前向参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆