【发布时间】:2011-03-13 17:41:48
【问题描述】:
收到此错误:
C:\CodeBlocks\kool\praks3\vector.h|62|错误:将 'const Vector' 作为 'std::string Vector::toString() 的 'this' 参数传递 [with short unsigned int 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;
}
【问题讨论】:
-
恭喜。那么,你的问题是什么?我在上面没有看到一个问号。
标签: c++ templates operator-keyword