C ++中的前向声明不起作用 [英] Forward declaration in C++ not working

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

问题描述

myProgram.cpp:

展开 | 选择 | Wrap | 行号

解决方案

当你转发声明一个类时您只是告诉编译器具有该名称的类将存在。你可以从前向声明中声明指针和引用,但你不能声明类的实例,因为类型不完整,编译器只知道它的名字而不是它的结构。


你可以只有在类完全声明后才声明类的实例。


你在哪里声明这个

展开 | 选择 | Wrap | 行号

展开 | 选择 | Wrap | 行号


OK !!这真的很有帮助,我假设我正在通过一个指针,就像在Java中一样。您可能已经猜到我发布的代码只是一个概念版本,在实际版本中,父级(粒子系统)包含一个子级(粒子)数组 - 我只是希望每个子级都有一个指向其父级的指针,因此它可以问它在重生时需要看起来像什么。


所以,如果我做这样的事情:

展开 | 选择 | 换行 | 行号


myProgram.cpp:

Expand|Select|Wrap|Line Numbers

解决方案

When you forward declare a class you are just telling the compiler that a class with that name is going to exist. You can declare pointers and references from a forward declaration but you can not declare an instance of the class because the type is incomplete, the compiler only knows its name not its structure.

You can only declare an instance of the class once the class has been fully declared.

Where you declare this

Expand|Select|Wrap|Line Numbers


Expand|Select|Wrap|Line Numbers


OK!! That''s really helpful, I was assuming I was passing through a pointer just as in Java. As you may have guessed the code I posted was just a conceptual version, in the actual version the parent (a particle system) contains an array of children (particles) - I just want each child to have a pointer to its parent so it can ask it what it needs to look like when it respawns.

So, if I do something like this:

Expand|Select|Wrap|Line Numbers


这篇关于C ++中的前向声明不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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