2017-10-13 23 views
0

저는 C++과 특히 데이터 구조의 초보자입니다. 이 프로젝트는 잘린 시리즈 (이와 비슷한 것)로 작업하고 있습니다. 이 이상한 편집 오류가 발생하고 그것이 무엇을 말하고 있는지 잘 모르겠습니다. 범인이 될 수 있도록 복사 생성자를 만들기 전에 프로그램이 정상적으로 실행되고있었습니다. 나는 힘든 시간을 보내면서 만들었지 만 그것이 내가 어떻게되는지 확신 할 수 없다.복사 생성자 C++ 컴파일 오류

이 오류는 다음과 같이 그렇다고 같습니다

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:1077:1: note: 
     candidate template ignored: could not match 
     'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' 
     against 'Series' 
operator<<(basic_ostream<_CharT, _Traits>& __os, 
^ 
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:1094:1: note: 
     candidate template ignored: could not match 
     'shared_ptr<type-parameter-0-2>' against 'Series' 
operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p) 
^ 
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:1101:1: note: 
     candidate template ignored: could not match 'bitset<_Size>' against 
     'Series' 
operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x) 

코드 :

#include <iostream> 
#include <vector> 

using namespace std; 

class Series { 
    private: 
      size_t degree; 
      vector <double> coefs; 

    public: 
      Series():degree(0){ } 

      Series(size_t d): degree(d){ 
        for(int i = 0; i < (degree+1); i++){ 
          coefs.push_back(0.0); 
        } 
      } 

      Series(double term){ 
        coefs[0] = term; 
      } 

      Series(size_t d2,vector <double> newcoeffs): Series(d2) { 
          for (int i = 1; i < (d2+1); i++) { 
            coefs.at(i) = newcoeffs.at(i-1); 
          } 
      } 

      Series(const Series & rhs) { 
          degree = rhs.degree; 
          vector <double> coefs; 
          for (int i = 1; i < (degree+1); i++) { 
            coefs.at(i) = rhs.coefs.at(i); 
          } 
      }  

      ~Series() { 
        coefs.clear(); 
      } 

      void print(ostream & out) const{ 
        if (degree == 0) { 
          return; 
        } 
        for (int i = 1; i < degree+1; i++) 
          out << coefs[i] << "x^" << i << " + "; 
        out << coefs[0]; 
      } 

}; 
ostream & operator <<(ostream & out, const Series & s){ 
        s.print(out); 
        return out; 
} 

int main(){ 
    vector <double> v {2.1,3.5,6.2}; 
    vector <double> z {1.1,2.3,4.0}; 
    Series two(3,z); 
    Series one(two); 

    cout << one << end; 
    cout << two << endl; 
    return 0; 
} 
+0

'coefs.at (I) = rhs.coefs.at (i)는, 컨테이너가이 같은'가능성이 세그먼트 오류로 이어질 것 전에 초기화되지 않았습니다. – informaticienzero

+0

<< 연산자를 friend 연산자로 선언하고 클래스 외부에서 정의해야한다고 생각합니다. –

+0

@informaticienzero 지금 libm ++ abi.dylib : 유형 std :: out_of_range의 캐치되지 않는 예외로 종료 : 트랩을 중단합니다 : 6, 이것과 관련이 있습니까? 어떻게 수정해야합니까? –

답변

3

그냥 오타에있어이 main

cout << one << end; 

해야

cout << one << endl; 

end은 이미 std 네임 스페이스에있는 함수의 이름이므로 유용하지 않은 오류 메시지가 표시됩니다. 이것은 using namespace std;이 종종 advised against 인 이유의 좋은 예입니다.

+0

정말 고마워요! Idk 어떻게 내가 간과했는지 –

0

문제와 직접 관련이 없지만 코드가 std::out_of_range 예외로 연결됩니다. 이는 을 사용하고 있기 때문에 값이 아닌 값을 std::vector에 액세스하는 함수입니다.

for (int i = 1; i < (degree+1); i++) 
    // SEGFAULT ! 
    coefs.at(i) = rhs.coefs.at(i); 

컨테이너에 요소를 추가하려면 std::vector::push_back을 사용해야합니다.

for (int i = 1; i < (degree+1); i++) 
    coefs.push_back(rhs.coefs.at(i)); 

또는 현대 C++ 심지어 짧은 :

for (auto elem : rhs) 
    coefs.push_back(elem); 
+0

'at'가 범위를 검사하고'std :: out_of_range'를 던지기 때문에 분할 오류가 발생하지 않을 것입니다 – bolov

+0

@bolov True. 나는'at '를 사용하지 않았고, 항상'[]'을 사용했기 때문에 이것을 잊었다. – informaticienzero