0

입력 할 책의 양을 입력하고 오버로드 된 연산자 ([])를 사용하는 응용 프로그램을 만들었지 만 배열을 저장할 포인터를 줄 때마다 오류가 발생합니다C++ 오버로드 연산자 []

2 인텔리과 같이 표현은 통합 또는 범위가 지정되지 않은 열거 형 라인이 있어야합니다 : (11) 칼럼 : 24 도서관 책

오류 1 ERR을 또는 C2440 : '초기화'는 '부호 INT'라인에 '표준 : : 문자열'에서 변환 할 수 없습니다 : (11) 칼럼 :

#include <iostream> 
#include <string> 
using namespace std; 

class Books{ 
private: 
    string* storage; 
    string book; 
public: 
    Books(){ 
     storage = new string[book]; 
    } 
    void savebooks(int iterate){ 
     for (int i = 0; i < iterate; ++i){ 
      cout << "Book: "; 
      getline(cin, storage[i]); 
     } 
    } 

    const string operator[](const int ref){ 
     return storage[ref]; 
    } 
    ~Books(){ 
     delete storage; 
    } 
}; 

int main(){ 
    //local variables 
    int quantity; 
    //user display 
    cout << "Welcome to Book Storage Viewer" << endl; 
    cout << "How many books would you like to insert: "; 
    cin >> quantity; 
    //instantiante 
    Books bk; 
    //other handle 
    bk.savebooks(quantity); 
    //display books 
    cout << "These are the books you've entered" << endl; 
    for(int i = 0; i < quantity; ++i){ 
     cout << bk[i] << endl; 
    } 
    system("pause"); 
    return 0; 
} 

: 어쨌든 여기에 1 도서관 책

하지만 내 코드입니다 또한이 코드를 올바르게 코딩했는지, 그리고 더 이상 오류가 있으면 100 % 확실하지 않습니다. 감사합니다.

+0

오류가 발생한 행을 나타낼 수 있습니까? 우리가 이야기하기가 어렵습니다. – Brian

+0

'저장소 = 새 문자열 [도서]; 컴파일 오류 – gongzhitaao

+0

여기 @BrianBi 오류 줄을 표시하고 오류가 발생한 곳인 – user3264250

답변

2

이 문

storage = new string[book]; 

가 잘못되었습니다. 첨자 값은 필수 또는 비 암호화 된 enueration 유형을 가져야한다.

그것은 클래스 정의 오타과 대신을 것 같다

string book; 

난 당신이 다음

private: 
    string* storage; 
    int quantity; 
public: 
    Books(int quantity) : quantity(quantity) { 
     storage = new string[this->quantity]; 
    } 
    void savebooks(){ 
     for (int i = 0; i < quantity; ++i){ 
      cout << "Book: "; 
      getline(cin, storage[i]); 
     } 
    } 
//... 

그리고 주에서 의미가 생각 예를

int book; 

거기에 있어야한다

이어야합니다.개
int quantity; 
//user display 
cout << "Welcome to Book Storage Viewer" << endl; 
cout << "How many books would you like to insert: "; 
cin >> quantity; 
//instantiante 
Books bk(); 
bk.savebooks(); 
+0

그럼 해결하려면 어떻게해야합니까? – user3264250

+0

@ user3264250 업데이트 된 게시물보기. –

+0

책의 내용을 선언하면 전체 응용 프로그램이 엉망이되지 않겠습니까 – user3264250

0

이것은 무엇입니까? 컴파일러가 오류의 위치를 ​​표시하지 않습니까?

Books(){ 
     storage = new string[book]; 
} 
+0

빨간색 마킹이 책에 있음을 나타냅니다 – user3264250

+0

'new [] '연산자를 사용하여'string' 값의 배열을 할당 했으므로 정확합니다 , 그리고 여러분은 할당 할'string' 값의 수를 나타내는 숫자 값을 지정할 것을 기대합니다 ('storage = new string [number of strings]'). 그러나 여러분은 그것을 count 대신에'string'으로 지정하기 때문에 실패합니다 오류가 있습니다. –

1

내 제안 : 얼마나 많은 책을 나타 내기 위해 Books에 아무것도 없다

  1. . 아마 당신은`Books '의 생성자에있는 책의 수를 전달하기를 원할 것입니다. Books(int numBooks) { storage = new string[numBooks]; }
  2. 책 수를 Books에 저장하려면 회원을 추가하십시오. 회원 books에 대한 사용은 없습니다. 아마도 books을 사용하여 책 수를 저장하려고했을 것입니다. 그렇게하기로 결정한 경우 string books;int books;으로 변경하고 생성자에서 books을 초기화해야합니다. Books(int numBooks) : books(numBooks) { storage = new string[numBooks]; }
  3. 책 수를 Books의 구성원으로 저장하려는 경우 quantitysavebooks()으로 전달할 필요가 없습니다. void savebooks(){ for (int i = 0; i < books; ++i){ cout << "Book: "; getline(cin, storage[i]); } }
+0

감사합니다. 매우 유익했습니다. – user3264250

1

은 아무도 delete [] storage

인해 delete storage하지만 그것이 있어야 내가 코드를 컴파일하려고 프로그램 의 끝에서 세그먼트 오류를 ​​metions없는 것, 그리고 : savebooks()는 다음과 같이 구현 될 수 책 수가 100 개 미만이면이 문제가 해결됩니다.

#include <iostream> 
    #include <string> 
    using namespace std; 

    class Books{ 
    private: 
     string* storage; 
     string book; 
    public: 
     Books(){ 
      storage = new string[100]; 
     } 
     void savebooks(int iterate){ 
      for (int i = 0; i < iterate; ++i){ 
       cout << "Book: "; 
       getline(cin, storage[i]); 
      } 
     } 

     const string& operator[](const int ref){ 
      return storage[ref]; 
     } 
     ~Books(){ 
      delete [] storage; 
     } 
    }; 

    int main(){ 
     //local variables 
     int quantity; 
     //user display 
     cout << "Welcome to Book Storage Viewer" << endl; 
     cout << "How many books would you like to insert: "; 
     cin >> quantity; 
     //instantiante 
     Books bk; 
     //other handle 
     bk.savebooks(quantity); 
     //display books 
     cout << "These are the books you've entered" << endl; 
     for(int i = 0; i < quantity; ++i){ 
      cout << bk[i] << endl; 
     } 
     return 0; 
    }