C++ C++;操作员错误
获取此错误: C:\CodeBlocks\kool\praks3\vector.h | 62 |错误:将'const vector'作为'std::string vector::toString()[带短无符号int n=2u]'的'this'参数传递将丢弃限定符| 使用此代码:C++ C++;操作员错误,c++,templates,operator-keyword,C++,Templates,Operator Keyword,获取此错误: C:\CodeBlocks\kool\praks3\vector.h | 62 |错误:将'const vector'作为'std::string vector::toString()[带短无符号int n=2u]'的'this'参数传递将丢弃限定符| 使用此代码: #include <iostream> #include <vector> #include <cmath> #include <string> #include
#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;
}
#包括
#包括
#包括
#包括
#包括
模板
类向量{
公众:
std::向量坐标;
向量();
向量(std::向量CRD);
浮动距离(向量v);
std::string toString();
模板
friend std::ostream&operator使toString
方法const
:
std::string toString() const;
及
模板
std::string Vector::toString()常量{…}
这是因为在操作符中将const
限定符添加到向量,使toString
方法const
:
std::string toString() const;
及
模板
std::string Vector::toString()常量{…}
这是因为您将const
限定符添加到操作符中的向量,如果您有一个const Foo
,您只能在其上调用const
成员函数。因此将其声明为std::string toString()const
如果你有一个const Foo
,你只能调用它上面的const
成员函数。所以把它声明为std::string toString()const
这是因为你的向量声明为const,而你的toString操作符不是const方法。
因此,禁止使用const instant调用此方法。
如果在将向量转换为字符串时未对其进行编辑,则应将其声明为常量方法:
std::string toString() const;
这是因为向量声明为const,而toString运算符不是const方法。
因此,禁止使用const instant调用此方法。
如果在将向量转换为字符串时未对其进行编辑,则应将其声明为常量方法:
std::string toString() const;
恭喜。那么,你的问题是什么?我在上面没有看到一个问号。恭喜。那么,你的问题是什么?我在上面没有看到一个问号。