预编译头策略 - 使用继承和来自(未知)类的本地成员

时间:2013-11-21 11:00:13

标签: c++ visual-c++ visual-studio-2012 precompiled-headers

我是新来的,抱歉,但有许多类似的话题,但他们没有正确回答这个问题。

(另一方面,很多人都说应该放置#include“stdafx.h” 在每个.h文件和.cpp文件中,我在每个.h文件中添加它们,一切正常。 我知道当#include“stdafx.h”仅包含在.cpp文件中时会发生什么 如果我只将它们放在.cpp文件中,我想有些类或枚举没有定义, 我希望它们成为一个类中的成员或从它们继承......这是文本的墙:)

这是我在“问题”问题中找到的主题,但是 没有人回答“正确”的问题,只有pchs背后的“基础”。

include stdafx.h in header or source file?

我有一个类似的问题,我知道pch以及它们是如何工作的,但是 Marnix问题没有完全解决。

他的问题是:

stdafx包含freeglut。 我的类头文件的属性为GLenum。

我应该将stdafx包含在班级的.h中吗?

我知道你可以用B类声明一个“未定义的类”作为例子, 但是当你想从这个类派生时,你在做什么 或者将其用作局部变量,而不是头文件中的指针。

示例:

//Where B is in the precompiled header file, lets say its a big class from <freeglut.h> 

class B1;

class c : public B // B is unknown and wont "compile" here
{
public:
    void test2();
        B1 tes; // Wont work because of undefined class..., I know why
        B1* tes2; // Works fine, I know why
};

头文件不正确,因为B尚未知晓。这是否包括在这里是一个好方法 “stdafx.h”文件 - 甚至包括freeglut.h?

我想不是,但是我们可以说理想的B类在命名空间中,某处隐藏......

因此,标题由于错误而不会“编译”。 而且我不想将头文件添加到“stdafx.h”中,因为我可能会更改它...

我知道pch是什么以及它们是如何工作的。

你知道解决方案吗?

  • 这是Marnix未解决的问题(包括我的问题),请看链接。

更新

比尔蜥蜴删除了我对问题的描述 - 它 应该更好地描述我的问题。谢谢你。

问题尚未解决。

这两个答案都是主要问题,并没有解决。 只是测试它,它不会工作。 我甚至问我的教授一个答案 - 他不知道答案。 所以:

这是用简单的词语描述的问题:

我有一个庞大的A班。这个班很大,不会改变,所以我 将它们包含在预编译的头文件中。

现在我有一个继承自A类的B类,并且拥有A类的本地成员。

问题是A级不懂B级。 A类声明;没有帮助,因为我想继承它并想要一个本地人 B类中的A类成员。

每个人都说预编译头应该是.cpp文件中的“ONLY” - 因此它将包括所有内容(当预处理器处于活动状态时)并且一切都应该工作,因为这是预处理器的第一行。无义。

所以 - 想一想。它无法处理它,因为头文件现在不是B类, 即使.cpp文件通过预编译头文件具有B类的定义。

示例代码:

  // Class A is inside the precompiled header - with #include "classA.h"
class A
{
    //BIG BIG BIG class
};


//This class doesn't know class A because I didn't include the precompiled header
//And it doesn't make sense to include the "big" header file from class A

//class A; // fake class A
class B : public A // A is unknown and wont compile even when "fake class A" is declared
{
    A nonUseableVariable; // A is unknown unless you include the header or precompiled header for it;
    A* ptr; // Only member works when "fake class A" is declared
};

//Solution from everybody how doesnt read and understand the real problem
//This is the .cpp file from class b.cpp
#include "stdafx.h" // dont care, the header cant be compiled

假设类B的标题经常更改,或者类A在命名空间中的某个位置,没有解决此问题的方法,您必须在此处添加预编译标题。

所以我希望你现在了解我。 我猜没有其他的解决方案,因为继承和本地成员必须为类B知道类A,当成员是指针时,成员才能工作。

但请查看代码。

我希望有人可以在这里帮助我,而且没有管理员会再次删除它。

更新:已解决

在重启整个系统后,VS发疯了,一切正常。 再次感谢你:)

1 个答案:

答案 0 :(得分:1)

使用预编译头时,它们必须是任何实现(.cpp)文件的第一个包含 - 否则编译器会抱怨。

Ergo没有必要将它们包含在头文件中,因为它们总是作为所有实现文件的第一个包含。

修改

正如Joachim Pileborg所指出的,它必须是源文件中的第一个非评论!

修改2

作为概念证明,我创建了一个项目:

<强> stdafx.h中

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>



// TODO: reference additional headers your program requires here
#include "a.h"

<强> A.H

#pragma once

class A
{
public:
    A();
};

<强> a.cpp

#include "stdafx.h"

A::A()
{
}

<强> b.h

#pragma once

class B : public A
{
public:
    B();
}

<强> b.cpp

#include "stdafx.h"
#include "b.h"

B::B()
{
}

输出:

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

<强>注意事项

Intellisense不喜欢它,但这并不奇怪 - 关键是, 工作。