2013-06-17 1 views
0

나는이 내 헤더 파일입니다다른 멤버 내에서 멤버를 오버로드 내 학교 과제에

작은 도움이 필요합니다

#include <iostream> 
#include <cstring> 

using namespace std; 

#include "ISBNPrefix.h" 

class ISBN 
{ 
    char str[11]; 
    char area[6]; 
    char publisher[8]; 
    char title[7]; 
    bool registered; 

    public: 
    ISBN(); 
    ISBN(const char*,ISBNPrefix &); 

    void toStr(char*)const; 
    void toStrWithStyle(char*)const; 
    bool empty()const; 
    bool isRegistered() const; 
    bool read(istream& is, const ISBNPrefix& list); 
    void display(ostream&) const; 
}; 

int isValid(const char* str); 

을이 내 파일의 구현 :

#define _CRT_SECURE_NO_WARNINGS 

#include<iostream> 
#include <iomanip> 

using namespace std; 

#include "ISBN.h" 

ISBN::ISBN() 
{ 
    str[0]='\0'; 
    area[0]='\0'; 
    publisher[0]='\0'; 
    title[0]='\0'; 
    registered=false; 
} 

ISBN::ISBN(const char* s,ISBNPrefix& p) 
{ 
    if(isValid(s)==1) 
    { 
     strcpy_s(str,s); 
    } 
    else 
    { 
     *this=ISBN(); 
    } 
} 

bool ISBN::empty()const 
{ 
    bool chk=false; 
    if(str[0]=='\0') 
     chk=true; 

    return chk; 
} 
void ISBN::toStrWithStyle(char* s) const 
{ 
    if(registered) 
    { 
     sprintf(s,"%s-%s-%s-%c",area,publisher,title,str[9]); 
    } 
    else 
    { 
     toStr(s); 
    } 
} 

void ISBN::toStr(char* s) const 
{ 
    if (str[0]!='\0') 
     strcpy(s,str); 
    else 
     strcpy(s,"no data"); 
} 

void ISBN::display(ostream & os) const 
{ 
    char str[14];  
    toStrWithStyle(str); 
    cout<< setw (13) <<str; 
} 

int isValid(const char* str) 
{ 
    int rc=0; 
    if(str!=0) 
    { 
     int sum,i=0; 
     sum=0; 
     for(i=0;i<10;i++) 
      sum+=(str[i]-'0')*(10-i); 
     if(sum%11==0) 
     { 
      rc= 1; 
     } 
    } 
    else 
     rc=0; 

    return rc; 
} 

bool ISBN::read(istream& is, const ISBNPrefix& list) 
{ 
    char str[11]; 
    bool quit=false; 
    bool ok=false; 
    char lists; 

    do{ 
     cout<<"ISBN (0 to quit) : "; 
     is.getline(str,11); //or is.get(str,11) 

     if(strcmp(str,"0")==0) 
      quit=true; 
     else if (isValid(str)==1) 
     { 
      *this=ISBN(str,list); 
      ok=true; 
      cout<<"isbn is valid"<<endl; 
     } 
     else 
     { 
      *this=ISBN(); 
      cout<<"invalid ISBN"<<endl; 
     } 
    } while(!quit&&!ok); 
    return !quit; 
} 

ISBN::read 여기서 내가 말하는 곳

*this=ISBN(str,list); 

다른 회원에게 과부하를 걸고 싶지만 할 수 없습니다. 누구나 저에게 어떻게 할 수 있습니까?

+2

이것은 많은 코드이며, 매우 포맷되지 않은 것입니다. [SSCCE] (http://sscce.org)를 참조하고 사이트를 참조하여 몇 가지 팁 다운 팁을 찾아보십시오. – chris

+0

스택 오버플로에 오신 것을 환영합니다. 최고의 도움을 얻으려면 완전한 예제를 포함하는 것이 좋지만 동시에 문제와 관련없는 코드/정의를 정리/제거해야합니다. 수행 한 조사를 나타내 려하고 문제와 관련된 모든/컴파일러 오류 경고를 포함시켜야합니다. – kfsone

+1

다른 사람이 제안한대로 질문에서 코드를 잘라내어 "과부하"로 무엇을 의미하는지 설명하십시오. 이는 그것이 무엇을 요구하고 있는지 명확하지 않기 때문입니다. C++에서 함수 오버로딩이 있습니다. 그러나 여기에서 설명하는 것과 같은지 확실하지 않습니다. –

답변

2

먼저 char []를 사용하여 std :: string을 사용하는 것이 좋습니다. 그것은 많은 문제를 줄일 수 있습니다.

bool ISBN::read(istream& is) 
{ 
    ISBN result; 

    // reading into result 

    std::swap(*this,result); 

    return !quit; 
} 

또는 (비 멤버 함수와 같은) 더 나은 :

std::istream& operator>>(istream& is, ISBN& obj) 
{ 
    ISBN result; 

    // reading into result 

    is(!quit) 
    is.clear(std::ios_base::failbit); 


    std::swap(obj,result); 

    return is; 
} 

당신이 당신의 자원 클래스를 RAII해야 어떤 방법으로 ISBN을 읽는 동안 나는 이런 식으로 뭔가를 작성합니다. 특별한 경우 char[] 대신 std::string입니다.

+0

함수 프로토 타입을 변경할 수 없습니다. 이 프로토 타입은 이미 교수가 제공하고 있으며,이 코드는 프로토 타입 용으로 작성되었습니다. 함수에서 ISBN :: read i는 (istream & is, ISBNPrefix & list)를 사용합니다. 이 두 값을 ISBN :: ISBN (const char * s, ISBNPrefix & p)에 전달하고 싶습니다. 이 두 변수를 어떻게 사용합니까? – user2492067

+0

@ user2492067 간단히 ISBN 문자열을 읽고 새 ISBN을 만들고 새 ISBN으로 바꾸십시오. –

관련 문제