Question

I have this simple 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();

But I am getting a lot of error? such as:

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

and so on.

I have these definitions at the start of file

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

I am using VS2012 on a console application.

edit1:

The complete code is as follow:

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();


 }

I also noted that I have these warnings:

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
Was it helpful?

Solution

Put header files after #include "stdafx.h"

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

stdafx.h must be the first included file, this is Microsoft specific rule.

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

OTHER TIPS

Your project is probably making use of precompiled headers. If so, the first line of each .cpp file should be:

#include "stdafx.h"

Or whatever the name of the header is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top