2013-12-23 5 views
1

줄 짜기 기능을 클래스로 옮기려고하는데, 몇 가지 오류가 발생했습니다. 어떻게 작동하는지 모릅니다.C++ 클래스를 사용하여 파일의 줄 수를 계산합니다.

class lines { 
     string name; 
     int number_of_lines; 
     string line; 
    public: 
     void set_value (string n); 
     ifstream myfile(name); //C2061: syntax error : identifier 'name' 
     while (getline(myfile, line)) //Multiple markers at this line - C2059: syntax error : 'while', - Syntax error 
     {       // C2334: unexpected token(s) preceding '{'; skipping apparent function body 
      ++number_of_lines; 
     } 
     int row() {return number_of_lines;} 
    }; 

void lines::set_value (string n) { 
number_of_lines=0; 
    name = n; 
} 

표시되는 행에 주석을 추가했습니다.

+1

. 먼저 함수 선언에서 유형이 아닌 변수 이름을 사용하고 있습니다. 둘째, 멤버 함수를 선언하고 클래스 정의에서 멤버 함수 정의로 사용하려는 코드 블록을 수행합니다. – Bandrami

+0

간단하게하십시오. 버퍼 된 입력을 사용하여 바이트 단위로 2 바이트 버퍼로 읽고 CR/LF/CRLF 시퀀스를 확인하십시오. – Dariusz

답변

1

변경 코드에

 string line; 
public: 
     void set_value (string n); 
     ifstream myfile(name); //C2061: syntax error : identifier 'name' 
     while (getline(myfile, line)) //Multiple markers at this line - C2059: syntax error : 'while', - Syntax error 
     {       // C2334: unexpected token(s) preceding '{'; skipping apparent function body 
      ++number_of_lines; 
     } 

:

많은 문제를 가지고
public: 
    void set_value (string name) 
    { 
     ifstream myfile(name); 
     string line; 
     while (getline(myfile, line)) 
     {    
      ++number_of_lines; 
     } 
     myfile.close(); 
    } 
+0

이것은 while 및 name 오류를 수정 한 것으로 보이지만 첫 번째 "{"대괄호에서 구문 오류가 많습니다. - 구문 오류 \t - C2059 : 구문 오류 : '{' \t - C2334 : 예기치 않은 토큰) 앞에 '{'; 명백한 함수 본문을 건너 뜁니다. – user2031585

+0

@ user2031585가 수정되었습니다. – herohuyongtao

0

C++에서 수행하는 연산/계산이 어떤 함수 내부에 있어야합니다. 여기에서는 클래스 내부에서 함수 내에 있어야하는 아래 명령문을 사용하고 있습니다.

ifstream myfile(name); //C2061: syntax error : identifier 'name' 
     while (getline(myfile, line)) //Multiple markers at this line - C2059: syntax error : 'while', - Syntax error 
     {       // C2334: unexpected token(s) preceding '{'; skipping apparent function body 
      ++number_of_lines; 
     } 
관련 문제