参考错误:使用前向声明? [英] Error in reference : use forward declaration ?

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

问题描述





我的问题是这样的



Main.cpp

Hi,

My problem is like this

Main.cpp

static CommandList* commandList = 0;
static Networker* networker = 0;
...

void f1()
{
 commandList->RunCommands();
}

void f2()
{
 networker ->Listen();
}




类neter类中的
我想调用commandList函数并且在CommandList中,我想调用网络功能



怎么办?

我试图找到谷歌的解决方案。我看到了前方声明。它可能是解决方案,但我不知道要改变什么



非常感谢您的帮助



in the class Networker class I want to call "commandList" functions and in the CommandList, I want to call "networker" functions

how to do this ?
I tried to find a solution with Google. I saw the forward declaration. It''s maybe the solution but I don"t know what to change

Many thanks for your help

推荐答案

在C ++中以正确的顺序获取内容的秘诀在于考虑翻译单元而不是文件。

翻译单元是一个cpp文件,就像你的main.cpp一样包含标题粘贴,就像你将每个文件的内容复制并粘贴到你有#include的确切位置,然后用生成的代码再次完成,直到整个文件是一个巨大的文件。在编译真正开始之前,这种疯狂正是C预处理器对您的代码所做的第一步。

这个粘贴在一起的超级文件是实际被编译的东西,它的内容顺序在那里这很重要。

我的帖子中没有足够的代码告诉我确切的错误但是一个非常通用的解决方案通常最终会看到一些问题g这样。

The secret to getting things in the right order in C++ is to think in terms of translation units rather than files.
A translation unit is a cpp file like your main.cpp with all the included headers pasted in, just as if you''d copied and pasted the content of each file into the exact place where you have a #include for it and then done that again with the resulting code until the whole thing is one huge file. This craziness is exactly what the C preprocessor does with your code as a first step before compiling really gets going.
This pasted together super file is the thing that actually gets compiled and its the order of things in there that matters.
There''s not enough of your code in your post for me to tell exactly whats wrong but a very general solution often ends up looking something like this.
//file A.h

class B;//This is a forward declaration of B

class A
{
...
private:
  B* m_pb;/*pointers and references only to B can be declared, no actual code that uses them though*/
};

//file B.h

class A;/*The compiler has already seen this but it doesn''t matter as long as it matches the declaration already seen*/

class B
{
...
private:
  A* m_pa;/*We could use an actual A here but if inclusion order changes things will break so we don''t*/
};

//file A.cpp

int A::somefunc(...)
{
  m_pb->DoABOperation();
}





这是A.cpp的编译单位。正如你所看到的,B.cpp的那个将是相似的,假设每个键入的A.cpp和B.cpp文件都以:



That''s the compilation unit for A.cpp. As you can see the one for B.cpp will be similar assuming that the A.cpp and B.cpp files as typed each begin with:

#include "A.h"
#include "B.h"
...





前瞻声明和包含工作的方式很老套一旦你掌握了各种各样的东西,它就会变得非常强大而又非常强大。从C#中删除它是我倾向于坚持使用C ++的原因之一。可能是因为我老了,也很老实。



Forward declaration and the way inclusion works is a very old fashioned and cranky but very powerful tool for doing all sorts of things once you get a handle on it. Removing it from C# was one of the reasons I tend to stick with C++. Probably because I''m old fashioned and cranky too.


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

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