#pragma once和#include问题 [英] #pragma once and #include issues

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

问题描述

我有一个循环引用问题(即A.h和B.h #including each),有些人建议我使用#pragma一次来防止这种情况。但是,这个解决方案似乎仍然不能正常工作。

I had an issue of circular references (i.e. A.h and B.h #including each other) and some people advised me to use #pragma once to prevent that. However, this solution appears to be not working still.

发生的事情是A类以外的任何文件中都不再识别A类(即使在A.cpp中也是如此) B类也是如此。

What's happening is that class A no longer becomes recognized in any file other than A.h (not even in A.cpp) and the same happens for class B.

让我告诉你代码:

#pragma once
#include "B.h"

class A {
public: B* b;

};

Bh​​

#pragma once
#include "A.h"

class B {
    public: A* a;
};

A.cpp

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

B.cpp A.cpp

错误跟踪如下:


1> B.cpp 1> c:\ usersrs \ user \\ \\documents\visual studio
2010 \projects\envmodel\test\bh(5):错误C2143:语法错误:
缺少';'在''1> c之前:\ usersrs \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\注意:C ++不支持default-int
1> c:\ usersrs \ user\documents\visual studio
2010 \projects\envmodel\test\bh(5) :错误C4430:缺少类型
说明符 - 假定为int。注意:C ++不支持default-int 1>
A.cpp 1> c:\users\user\documents\visual studio
2010\projects\envmodel\ test \\ bh(5):错误C2143:语法错误:
缺少';'之前'
'1> c:\ usersrs \ user\documents\visual studio
2010 \\ n \\ projects\envmodel\test\bh(5):错误C4430:缺少类型
说明符 - 假定为int。注意:C ++不支持default-int
1> c:\ usersrs \ user\documents\visual studio
2010 \projects\envmodel\test\bh(5) :错误C4430:缺少类型
说明符 - 假定为int。注意:C ++不支持default-int

1> B.cpp 1>c:\users\user\documents\visual studio 2010\projects\envmodel\test\b.h(5): error C2143: syntax error : missing ';' before '' 1>c:\users\user\documents\visual studio 2010\projects\envmodel\test\b.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\users\user\documents\visual studio 2010\projects\envmodel\test\b.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1> A.cpp 1>c:\users\user\documents\visual studio 2010\projects\envmodel\test\b.h(5): error C2143: syntax error : missing ';' before '' 1>c:\users\user\documents\visual studio 2010\projects\envmodel\test\b.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\users\user\documents\visual studio 2010\projects\envmodel\test\b.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

还有两件事我想澄清:


  1. 在Visual Studio 2010中如何以及何时使用#includestdafx.h(我真的很讨厌这个IDE,但它似乎是唯一一个适用于C ++ - Netbeans是这种语言的完全垃圾)

  1. How and when to use #include "stdafx.h" in Visual Studio 2010 (I really hate this IDE, but it appears to be the only one that works well with C++ - Netbeans is total trash with this language)

如何正确使用#pragma?我假设我只是把它放在每个文件的第一行(好吧,显然这不能解决我的问题!)。是否应该在#includestdafx.h之前或之后放置?注意:我没有在stdafx.h中输入#pragma一次

How to use #pragma once correctly? I'd assume I'd just put it at the first line of each file (well, obviously that doesn't solve my problems!). Also, should it be placed before or after #include "stdafx.h"? Note: I didn't put #pragma once in stdafx.h

谢谢。

编辑:我忘记了分号,因此原始错误跟踪变得臃肿。
Edit2:我忘了使用指针。我的实际程序确实使用了指针而不是普通的对象值,我忽略了我急于创建一个小例子。

I forgot the semi-colons, thus the original error trace was bloated. I forgot to use pointers. My actual program did use pointers instead of plain object values and I neglected that in my haste to create a small example.

推荐答案

你不能有循环依赖。

这样想。如果您实例化A类型的对象;那有多大?答案是无限大的。因此,无论如何都无法创建像这样的圆形对象。

Think of it like this. If you instanciate an object of type A; then how big is it? The answer is infinitely large. So you can't create objects that are circular like that anyway.

您需要使用可选值(指针)来中断循环。

SO如果您更改B以保持指向A的指针并使用前向引用,则它可以正常工作。

You need to break the cycle with an optional value (a pointer).
SO If you change B to hold a pointer to A and use forward references then it works.

#pragma once
class A; // Forward reference.

class B {
    public: A* a; // Break cycle with a pointer. (In real life use a smart pointer)
}

注意:你仍然应该使用头文件中的#pragma一次

Note: You should still use the #pragma once in your header files.

这篇关于#pragma once和#include问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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