このエラー取得:C++オペレータエラー
C:\コードブロック\クール\ praks3 \ vector.h | 62 |エラー:STD」の 'この' 引数として 'constのベクトル< 2U>' 渡す::文字列をVector :: toString()[短い符号なしint n = 2u] '修飾子を破棄します。このコードで
:
std::string toString() const;
と
template <unsigned short n>
std::string Vector<n>::toString() const{...}
あなたはoperator<<
にVector
にconst
修飾子を追加しているためです。
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <sstream>
template <unsigned short n>
class Vector {
public:
std::vector<float> coords;
Vector();
Vector(std::vector<float> crds);
float distanceFrom(Vector<n> v);
std::string toString();
template <unsigned short m>
friend std::ostream& operator <<(std::ostream& out, const Vector<m>& v);
};
template <unsigned short n>
std::string Vector<n>::toString() {
std::ostringstream oss;
oss << "(";
for(int i = 0; i < n; i++) {
oss << coords[i];
if(i != n - 1) oss << ", ";
}
oss << ")";
return oss.str();
}
template <unsigned short m>
std::ostream& operator<<(std::ostream& out, const Vector<m>& v) {
out << v.toString(); // ERROR HEEEERE
return out;
}
おめでとう。だから、あなたの質問は何ですか?私は上記の1つの疑問符が表示されません。 – AnT