简体   繁体   English

VS 2008-标头的某些部分不包括在内?

[英]VS 2008 - some parts of header not included?

I'm working under Visual Studio 2008, maybe that's important. 我正在Visual Studio 2008下工作,也许这很重要。 In a larger project I decided to split one of my .cpp files into two. 在一个较大的项目中,我决定将一个.cpp文件拆分为两个。 As I moved some of the functions to a new file, let's call it new.cpp , and tried to compile, I got errors that new.cpp doesn't know definitions of fstreams , setw() , etc. Now, at the very top of the new file I included my own header, let's call it main_header.h , which in turn includes all the necessary <iostream> , <iomanip> , etc. This works just fine all throughout older files used in this project, but for some reason doesn't in new.cpp . 当我将一些函数移到新文件中时,我们将其new.cpp并尝试进行编译,但出现了new.cpp不知道fstreamssetw()等定义的fstreams 。在新文件的顶部,我包含了自己的标头,我们称其为main_header.h ,它依次包含了所有必需的<iostream><iomanip>等。此项目在此项目中使用的所有较旧文件中都可以正常工作,但是对于某些原因不在new.cpp

If I add 如果我加

#include <fstream>
#include <iomanip>
// and all the rest

in new.cpp then all works just fine, but that's not how I want to solve it. 然后在new.cpp一切正常,但这不是我要解决的方式。 I thought maybe content of main_header.h doesn't get appended to new.cpp on compilation, but that's not true, I tried using in new.cpp an external variable declared in main_header.h and defined in yet different .cpp, and got no errors on compilation, linking, or running. 我想,也许内容main_header.h不会被追加到new.cpp上编译,但事实并非如此,我尝试使用new.cpp在声明的外部变量main_header.h在又不同的.cpp定义,并得到了编译,链接或运行时没有错误。 Yet it seems like <fstream> and <iomanip> included in main_header.h do not make it to new.cpp file. 但是,似乎main_header.h包含的<fstream><iomanip>并未进入new.cpp文件。

I'm relatively new to Visual Studio, so the solution to my problem is likely something silly I'm not aware of, but I spent a good while trying to figure this one out and to no avail. 我是Visual Studio的新手,因此解决我的问题的方法可能是我不知道的愚蠢之举,但是我花了很多时间试图弄清楚这一点,但无济于事。 The new file is definitely part of the project, since building projects attempts to compile it, plus once I include iostream and iomanip in this new.cpp I can call its routines in other parts of the project. 新文件绝对是项目的一部分,因为构建项目会尝试对其进行编译,此外,一旦在此new.cpp包含iostreamiomanip ,我就可以在项目的其他部分调用它的例程。 Any ideas what I might be doing wrong? 有什么想法我可能做错了吗?

main_header.h looks like that main_header.h像这样

#ifndef MAIN_HEADER
#define MAIN_HEADER

#include <iomanip>
#include <fstream>
// loads of other stuff

#endif  // for MAIN_HEADER



Update: Ok, so the day after I created a whole new project using the same files and now all works fine, I don't need to include iomanip nor anything else in new.cpp. 更新:好的,因此,在我使用相同的文件创建了一个全新的项目并且现在一切正常的第二天,我不需要在new.cpp中包含iomanip或其他任何内容。 It sure as heck was to do with some oddities of VS not code itself, but still beats me what exactly was the issue. 可以肯定的是,与VS的一些奇怪之处有关,而不是代码本身,但是仍然使我难过。

This could be caused by prefix headers or precompiled headers, which can be set across the whole project, or could be set just on your new.cpp file, which might explain why there's some difference. 这可能是由前缀标头或预编译标头引起的,它们可以在整个项目中设置,也可以仅在new.cpp文件中设置,这可能解释了为什么存在一些区别。 Here's a few things to try: 这里有一些尝试:

  • Properties -> C++ -> Precompiled headers: check the setting for the whole project and for the individual files 属性-> C ++->预编译头文件:检查整个项目和单个文件的设置
  • Properties -> C++ -> Advanced -> Force includes: check this is the same for both 属性-> C ++->高级->强制包含:检查两者是否相同
  • Open the vcproj file in a text editor and find the new.cpp node -- this is a quick way of finding if this individual file has different compiler settings 在文本编辑器中打开vcproj文件并找到new.cpp节点-这是查找此单个文件是否具有不同编译器设置的快速方法
  • Properties -> C++ -> Preprocessor -> Generate Preprocessed file: this will generate an intermediate new.i file with all #includes and macros resolved. 属性-> C ++->预处理程序->生成预处理文件:这将生成一个中间new.i文件,其中所有#includes和宏都已解析。 Compare the result of this for both files and look for the diffs -- this might show why one works and the other doesn't 比较这两个文件的结果并查找差异-这可能表明为什么一个起作用而另一个不起作用

Do you have another header somewhere that also has #define MAIN_HEADER ? 您是否在其他地方也有#define MAIN_HEADER标头?

It's an easy mistake to make when creating a new header by copying an old one, and leads to mysterious symptoms like this. 通过复制旧的标题来创建新的标题时,这是一个容易犯的错误,并导致诸如此类的神秘症状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM