私はC++、特にデータ構造の初心者です。このプロジェクトでは、トランケートされたシリーズ(そのようなもの)を扱っています。私はこの奇妙なコンパイルのエラーを取得し、私はそれが私に言っているか分からない。私がコピーコンストラクタを作成する前にプログラムが正常に動作していたので、それが原因である可能性があります。私は苦労して1つにしていましたが、それが私の考え方であるかどうかはわかりません。コピーコンストラクタ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;
}
'coefs.at(I)= rhs.coefs.at(i)は、あなたのコンテナが持っている'そう、セグメンテーションフォールトにつながります以前は初期化されていません。 – informaticienzero
私は、あなたの<<演算子をフレンド演算子として宣言し、それをクラス外に定義しなければならないと思います。 –
@informaticienzero libC++ abi.dylib:タイプstd :: out_of_rangeのキャッチされていない例外で終了: トラップを中止する:6、これは何と関係がありますか?どうすれば修正できますか? –