得到這個錯誤:C++操作員錯誤
C:\碼塊\庫爾\ praks3 \ vector.h | 62 |錯誤:使 'const的矢量< 2U>' 爲 '這個' 的「參數的std :: string的Vector :: toString()[帶短符號的無符號整數n = 2u]'丟棄限定符|
與此代碼:
#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;
}
恭喜。那麼,你的問題是什麼?上面沒有看到一個問號。 – AnT 2011-03-13 17:44:39