C++操作员错误

发布于 2024-10-22 00:47:58 字数 1233 浏览 3 评论 0原文

收到此错误:

C:\CodeBlocks\kool\praks3\vector.h|62|错误:传递 'const Vector<2u>'作为 'std::string Vector::toString() [with Short unsigned int n = 2u]' 的 'this' 参数丢弃限定符|

用这个代码:

    #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;
}

getting this error:

C:\CodeBlocks\kool\praks3\vector.h|62|error: passing 'const Vector<2u>' as 'this' argument of 'std::string Vector::toString() [with short unsigned int n = 2u]' discards qualifiers|

with this code:

    #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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

離殇 2024-10-29 00:47:59

toString 方法设为 const

std::string toString() const;

template <unsigned short n>
std::string Vector<n>::toString() const{...}

是因为您将 const 限定符添加到 中的 Vector运算符<<。您只能对 const 限定对象调用 const 限定方法。

Make the toString method const:

std::string toString() const;

and

template <unsigned short n>
std::string Vector<n>::toString() const{...}

That's because you add the const qualifier to the Vector in the operator<<. You are only allowed to call const qualified methods on const qualified objects.

终陌 2024-10-29 00:47:59

这是因为您的向量被声明为 const,而您的 toString 运算符不是 const 方法。
因此,禁止使用 const 实例调用此方法。
如果在将向量转换为字符串时不对其进行编辑,则应将其声明为 const 方法:

std::string toString() const;

This is because your vector is declared as const, while your toString operator is not a const method.
Therefore, calling this method is forbidden with a const instant.
If you do not edit the vector while converting it to string, you should declare it as a const method :

std::string toString() const;
生生漫 2024-10-29 00:47:59

如果您有一个 const Foo,则只能调用它的 const 成员函数。因此将其声明为 std::string toString() const

If you have a const Foo, you can only invoke const member functions on it. So declare it as std::string toString() const.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文