VS 2008 - 未包含标头的某些部分?

发布于 2024-11-07 18:40:55 字数 1447 浏览 0 评论 0原文

我正在 Visual Studio 2008 下工作,也许这很重要。 在一个较大的项目中,我决定将一个 .cpp 文件分成两个。当我将一些函数移动到一个新文件(我们将其命名为 new.cpp)并尝试编译时,我收到了 new.cpp 不知道定义的错误fstreamssetw() 等。现在,在新文件的最顶部,我包含了自己的标头,我们将其称为 main_header.h,这又包括所有必需的 等。这在该项目中使用的所有旧文件中都可以正常工作,但是由于某种原因不在 new.cpp 中。

如果我添加

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

new.cpp 那么一切都会正常工作,但这不是我想要解决的问题。 我认为也许 main_header.h 的内容在编译时不会附加到 new.cpp 中,但事实并非如此,我尝试在 new.cpp< 中使用/code> 在 main_header.h 中声明并在不同的 .cpp 中定义的外部变量,并且在编译、链接或运行时没有错误。然而,似乎 main_header.h 中包含的 并未进入 new.cpp 文件。

我对 Visual Studio 比较陌生,所以我的问题的解决方案可能是一些我不知道的愚蠢的事情,但我花了很长时间试图解决这个问题,但无济于事。新文件肯定是项目的一部分,因为构建项目会尝试编译它,而且一旦我在此 new.cppiostream 和 iomanip > 我可以在项目的其他部分调用它的例程。有什么想法我可能做错了什么吗?

main_header.h 看起来像这样

#ifndef MAIN_HEADER
#define MAIN_HEADER

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

#endif  // for MAIN_HEADER



更新:好的,所以在我使用相同的文件创建一个全新的项目并且现在一切正常之后的第二天,我不需要在 new.cpp 中包含 iomanip 或其他任何内容。这确实与 VS 的一些奇怪之处有关,而不是代码本身,但仍然让我困惑到底是什么问题。

I'm working under Visual Studio 2008, maybe that's important.
In a larger project I decided to split one of my .cpp files into two. 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.

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.
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. Yet it seems like <fstream> and <iomanip> included in main_header.h do not make it to new.cpp file.

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. 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. Any ideas what I might be doing wrong?

main_header.h looks like that

#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. It sure as heck was to do with some oddities of VS not code itself, but still beats me what exactly was the issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2024-11-14 18:40:55

这可能是由前缀标头或预编译标头引起的,这些标头可以在整个项目中设置,也可以仅在 new.cpp 文件上设置,这可能解释了为什么存在一些差异。这里有一些可以尝试的事情:

  • 属性-> C++->预编译头:检查整个项目和各个文件的设置
  • 属性 -> C++->高级->强制包含:检查两者是否相同
  • 的快速方法
  • 在文本编辑器中打开 vcproj 文件并找到 new.cpp 节点 - 这是查找此单个文件是否具有不同编译器设置 -> C++->预处理器->生成预处理文件:这将生成一个中间 new.i 文件,其中包含所有#include 和已解析的宏。比较两个文件的结果并查找差异 - 这可能会显示为什么一个有效而另一个无效

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. Here's a few things to try:

  • Properties -> C++ -> Precompiled headers: check the setting for the whole project and for the individual files
  • Properties -> C++ -> Advanced -> Force includes: check this is the same for both
  • 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
  • Properties -> C++ -> Preprocessor -> Generate Preprocessed file: this will generate an intermediate new.i file with all #includes and macros resolved. Compare the result of this for both files and look for the diffs -- this might show why one works and the other doesn't
别挽留 2024-11-14 18:40:55

您是否还有另一个标头也包含 #define MAIN_HEADER

通过复制旧标头来创建新标头时,这是一个很容易犯的错误,并会导致类似这样的神秘症状。

Do you have another header somewhere that also has #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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文