得到这个错误: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