2013-05-23 3 views
0

에서이 오류를 얻고있다 이유 :나는이 간단한 코드가 C의 ++ 코드

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 

등등이 있습니다.

은 내가 콘솔 응용 프로그램에 VS2012를 사용하고

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

파일의 시작에서 이러한 정의가 있습니다.

EDIT1 : 미리 컴파일 된 헤더의

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 
+1

'std :: ifstream ifs;'의 직전에 코드를 게시 할 수 있습니까? 아마도 어딘가에 이전에 세미콜론을 놓친 것일 수 있습니다. –

+0

std :: 포괄적으로 사용할 때 "using namespace std"줄을 버리십시오. – Bathsheba

+0

[코드 작동 원리] (http://eval.in/31344). 함수에서 그 코드를 랩핑 했습니까? –

답변

3

넣고 헤더 파일 #include "stdafx.h"

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

stdafx.h이어야 후,이 마이크로 소프트 특정 규칙이다.

컴파일 옵션 /Yu'stdafx.h가 '선택 해제하지 않는 한 C++는, 에서 소스 파일을 #INCLUDE "에 stdafx.h"전 아무것도 컴파일되지 않습니다 비주얼 1

(기본적으로)
+0

고마워요,하지만 왜 작동합니까? – mans

+0

오, 전 이미 같은 질문에 답했습니다. [this] (http://stackoverflow.com/a/16040876/952747)를 읽어보십시오. – deepmax

2

프로젝트가 아마 만들고 사용 : 나는 또한 내가 이러한 경고가 주목

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


} 

:

전체 코드는 다음과 같다. 이 경우, 각 .cpp 파일의 첫 번째 행은 다음과 같습니다

#include "stdafx.h" 

또는 어떤 헤더의 이름입니다. 첫 번째 파일을 포함