2013-06-15 3 views
-1

이 방법을 테스트하기 위해 메인에 배열을 인쇄했습니다. 그러나 다른 수업에서 같은 것을하려고 할 때 나는 실수를합니다.eof가 fstream의 클래스 유형이 아니라는 오류가 나타나는 이유는 무엇입니까?

#include <iostream> 
#include <stdio.h> 
#include <string.h> 
#include <fstream> 
#include "struct.h" 
#include "phoneBook.h" 

using namespace std; 

int main() 
{ 
    struct contacts info[256]; 
    phoneTools manipulate; 
    fstream phoneBook ("phoneBook.txt"); 

    if(!phoneBook.is_open()) 
    { 
     cout<< "The file can not be opened"; 
     cout<< endl; 
     cout<< "Terminating!"; 

     exit(1); 
    } 

    else 
    { 
     //populate array of structs 
     int i = 0; 
     while(!phoneBook.eof()) 
     { 
      phoneBook>> info[i].firstName; 
      phoneBook>> info[i].surName; 
      phoneBook>> info[i].phoneNumber; 
      phoneBook>> info[i].email; 
      phoneBook>> info[i].relationship; 
      i++; 
     } 
    } 

    manipulate.addContact(info, phoneBook); 
} 

이것은 오류가 발생하는 다른 클래스입니다. 오류 메시지와 함께 아래에 헤더를 넣을 것입니다. 또한 내가 사용하지 않는 일부 라이브러리를 가져올 수도 있음을 알고 있지만, 그렇게 할 것입니다. 나는 모든 기능을 완성하지 못했습니다. 여기

#include <iostream> 
#include <stdlib.h> 
#include <string.h> 
#include <fstream> 
#include "struct.h" 

using namespace std; 

void writeStructToDatabase(struct contacts writeContact[], fstream *phoneBook) 
{ 
    //We are passed new array of contacts 
    //write this new array to the phonebook file 
    int i = 0; 
    while(!phoneBook->eof()) 
    { 
     phoneBook<< writeContact[i].firstName; 
     phoneBook<< writeContact[i].surName; 
     phoneBook<< writeContact[i].phoneNumber; 
     phoneBook<< writeContact[i].email; 
     phoneBook<< writeContact[i].relationship; 
     i++; 
    } 
} 

void showPhoneBook(struct contacts print[]) 
{ 
    int num = (sizeof(print)/sizeof(*print)); 

    cout<< endl; 
    cout<< endl; 
    for(int i = 0; i < num; i++) 
    { 
     cout<< print[i].firstName; 
     cout<< endl; 

     cout<< print[i].surName; 
     cout<< endl; 

     cout<< print[i].phoneNumber; 
     cout<< endl; 

     cout<< print[i].email; 
     cout<< endl; 

     cout<< print[i].relationship; 
     cout<< endl; 
    } 
    cout<< endl; 
} 

void addContact(struct contacts newContact[], fstream *phoneBook) 
{ 
    int num = (sizeof(newContact)/sizeof(*newContact)); 

    cout<< "First Name: "; 
    cin>> newContact[num].firstName; 
    cout<< endl; 

    cout<< "Last Name: "; 
    cin>> newContact[num].surName; 
    cout<< endl; 

    cout<< "Phone Number: "; 
    cin>> newContact[num].phoneNumber; 
    cout<< endl; 

    cout<< "Email: "; 
    cin>> newContact[num].email; 
    cout<< endl; 

    cout<< "Relationship: "; 
    cin>> newContact[num].relationship; 
    cout<< endl; 

    writeStructToDatabase(newContact, phoneBook); 
} 

void deleteContact(struct contacts delContact[], fstream *phoneBook) 
{ 
    string first; 
    string last; 

    cout<< "First Name: "; 
    cin>> first; 
    cout<< endl; 

    cout<< "Last Name: "; 
    cin>> last; 
    cout<< endl; 

    int num = (sizeof(delContact)/sizeof(*delContact)); 

    int exit = 0; 
    int i = 0; 
    while(exit == 0) 
    { 
     if((strcmp(delContact[i].firstName, first) == 0) && 
      (strcmp(delContact[i].surName, last) == 0)) 
     { 
      for(int j = i; j < num; j++) 
      { 
       delContact[j] = delContact[j+1]; 
      } 

      exit = 1; 
     } 

     i++; 
    } 

writeStructToDatabase(delContact, phoneBook); 
} 

#include <fstream> 

using namespace std; 

#ifndef PHONE_BOOK_H 
#define PHONE_BOOK_H 

//enter methods below this line 
//ex. extern void getRandInteger(int max); 

class phoneTools 
{ 
    public: 
    void addContact(contacts newContact[], fstream *phoneBook); 
    void showPhoneBook(contacts print[]); 
    void deleteContact(contacts delContact[], fstream *phoneBook); 

    private: 
    void writeStructToDatabase(contacts writeContact[], fstream *phoneBook); 
}; 

//enter methods above this line 

#endif /* __PHONE_BOOK_H */ 

지금 여기에 내가

phoneBook.cpp: In function ‘void writeStructToDatabase(contacts*, std::fstream*)’: 
phoneBook.cpp:16: error: no match for ‘operator<<’ in ‘phoneBook << writeContact[i].contacts::firstName’ 
phoneBook.cpp:17: error: no match for ‘operator<<’ in ‘phoneBook << writeContact[i].contacts::surName’ 
phoneBook.cpp:18: error: no match for ‘operator<<’ in ‘phoneBook << writeContact[i].contacts::phoneNumber’ 
phoneBook.cpp:19: error: no match for ‘operator<<’ in ‘phoneBook << writeContact[i].contacts::email’ 
phoneBook.cpp:20: error: no match for ‘operator<<’ in ‘phoneBook << writeContact[i].contacts::relationship’ 
phoneBook.cpp: In function ‘void deleteContact(contacts*, std::fstream*)’: 
phoneBook.cpp:97: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’ 
phoneBook.cpp:98: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’ 
phoneBookDriver.cpp: In function ‘int main()’: 
phoneBookDriver.cpp:40: error: invalid conversion from ‘void*’ to ‘std::fstream*’ 
phoneBookDriver.cpp:40: error: initializing argument 2 of ‘void phoneTools::addContact(contacts*, std::fstream*)’ 
i686-apple-darwin11-llvm-g++-4.2: phoneBook.o: No such file or directory 
i686-apple-darwin11-llvm-g++-4.2: phoneBookDriver.o: No such file or directory 
i686-apple-darwin11-llvm-g++-4.2: no input files 
를 얻고 오류입니다 내 기능 클래스의 헤더 내 구조체 여기

#include <string.h> 

using namespace std; 

#ifndef STRUCT_H 
#define STRUCT_H 
struct contacts 
{ 
    string firstName; 
    string surName; 
    string phoneNumber; 
    string email; 
    string relationship; 
}; 
#endif 

내 헤더입니다

나는 마지막 날을 검색했다. 라이브러리를 가져 왔음에도 fstream 함수를 사용할 수없는 이유를 알아 내려고 노력했습니다. 플러스 함수를 사용하면이 작업을 수행 할 수 있습니다. 이것은 여기에서 나의 첫번째 질문이다, 그래서 나는 그것을 정확하게 배열 했길 바란다. 또한 이것은 단지 여름 동안의 재미를위한 것입니다. 숙제가 아니야.

+1

'phoneBook '은 포인터이기 때문에. '.' 대신'->'을 시도하십시오. –

+0

오케이. 고맙습니다. 전화 번호부 파일을 올바르게 전달하지 못하고 이것이 phoneBook.cpp에서 쓸 수없는 이유입니까? –

+1

['while (! eof())'잘못되었습니다.] (http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) 또한,'__STRUCT_H '및'__PHONE_BOOK_H'는 [예약 된 식별자] (http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-ac-identifier)입니다. – chris

답변

2

두 번째 버전을 사용해야합니다

코드는 이 아니며 동일한 것을하는입니다. 변수 이 아닌 변수 ifstream*을 사용하고 <<을 사용하면 >>이 아닌 값을 읽을 수 있습니다. 당신이 똑같은 일을했다면 효과가 있었을 지 모르지만, 당신은 뭔가 다른 일을하고 깨뜨린 것입니다.

오류가 하지eoffstream의 클래스 타입이 아니라고 않는, 다시 더주의 깊게 읽어

phoneBook.cpp:14: error: request for member ‘eof’ in ‘phoneBook’, 
    which is of non-class type ‘std::ifstream*’ 

phoneBook 비 클래스 형 ifstream* (즉, 포인터 타입) 그리고 말한다 포인터를 멤버가 없기 때문에 자명하다. 멤버를 사용하려고한다.

오류를 올바르게 읽으면 오류가 무엇인지 알려줍니다. 코드를 더 자세히 읽으면 두 번째 버전이 첫 번째 버전과 동일하지 않음을 스스로 알 수 있습니다. 이중 밑줄로 시작하는

#ifndef __PHONE_BOOK_H 

이름 (또는 밑줄 및 대문자)이 예약되어 당신이 그들을 사용해서는 안 다음 comment from chris 말한대로

또한, 하지이 작업을 수행 할.

그리고 하지이해야합니까 :이 당신이 원하는 당신이 원하는하지 무엇 인 데이터를 읽을 실패 후 을 종료합니다 루프

while(!phoneBook.eof()) 
입니다

을 :

while(*phoneBook >> writeContact[i].firstName >> writeContact[i].surName 
     >> writeContact[i].phoneNumber>> writeContact[i].email 
     >> writeContact[i].relationship) 

(>>이 아닌 <<을 사용하는 메모)

하지만 왜 mak 쉽게 당신이 간단하게 할 수있는 것 operator>>(std::istream&, contacts&)을 읽고 정의 E :

while (*phoneBook >> writeContact[i]) 

그리고 당신은 또한 당신은 제대로 포인터를 사용하지 않는 문제가 발생하지 않도록, 참조로 ifstream이 포인터하지 전달할 수 있습니다.

+0

** ** 업데이트 ** 나는 코드와 오류를 수정하여 변경 한 내용과 일치하도록했습니다. 다시 도움을 주셔서 감사합니다 –

+0

조나단은 내 전화 번호부를 채우는 상세한 답변과 더 나은 방법에 대해 감사드립니다. 나는 지금 이것을 구현하기 시작할 것이다. –

0

phoneBook은 대신 포인터 사용 ->입니다. 그것은 단지 당신의 응용 프로그램 파일에서 입력을받을 수 있도록 및 ifstream은 "입력 파일 스트림을"의미는 "ofstream"또는 이들의 부모 "fstream"의 http://www.cplusplus.com/reference/fstream/fstream/

+0

Chris 나는 크리스천을 읽고 stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier를 읽었다. 읽은 후 헤더 파일에서 이중 밑줄을 제거했습니다. 이제 PHONE_BOOK_H 예를 들어 보겠습니다. Oli 나는 while (! phoneBook-> eof()) while while을 바 꾸었고 오류가 수정되었습니다. Mohamed 필자는 입력 및 출력 작업이 모두 필요하기 때문에 phoneBook의 선언을 "ifstream"에서 "fstream"으로 변경했습니다. 그러나, 나는 아직도 그 오류를 받고있다. 나는 헤더와 다른 파일들에서도 그것을 확실히 변경했다. 도움을 주셔서 감사합니다 –

관련 문제