繁体   English   中英

为什么我在C ++代码中遇到此错误

[英]Why I am getting this error in c++ code

我有这个简单的代码:

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 ++不会在源文件中的#include“ stdafx.h”之前进行任何编译,除非未选中编译选项/Yu'stdafx.h'(默认情况下) 1

您的项目可能正在使用预编译的头文件。 如果是这样,则每个.cpp文件的第一行应为:

#include "stdafx.h"

或无论标题的名称是什么。

暂无
暂无

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

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