gpt4 book ai didi

c++ - 为什么我在 C++ 代码中收到此错误

转载 作者:太空宇宙 更新时间:2023-11-04 16:21:11 24 4
gpt4 key购买 nike

我有这个简单的代码:

std::ifstream ifs;
ifs.open ("test.txt", std::ifstream::in);
char c = ifs.get();
while (ifs.good()) {
std::cout << c;
c = ifs.get();
}
ifs.close();

但是我得到了很多错误?比如:

Error   9   error C3083: 'ifstream': the symbol to the left of a '::' must be a type    test.cpp    
Error 8 error C2228: left of '.open' must have class/struct/union test.cpp

等等。

我在文件开头有这些定义

#include <iostream>
#include <fstream>
#include "stdafx.h"
using namespace std;

我在控制台应用程序上使用 VS2012。

编辑1:

完整代码如下:

void ReadRawImages::Read(int frameNumber)
{
std::ifstream ifs;

ifs.open ("test.txt", std::ifstream::in);

char c = ifs.get();

while (ifs.good()) {
std::cout << c;
c = ifs.get();
}

ifs.close();


}

我还注意到我有这些警告:

Warning 1   warning C4627: '#include <iostream>': skipped when looking for precompiled header use   test.cpp
Warning 2 warning C4627: '#include <fstream>': skipped when looking for precompiled header use test.cpp

最佳答案

将头文件放在#include "stdafx.h"之后

#include "stdafx.h"
#include <iostream>
#include <fstream>

stdafx.h 必须是第一个包含的文件,这是 Microsoft 的特定规则。

Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default) 1

关于c++ - 为什么我在 C++ 代码中收到此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16712729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com