【问题标题】:the object has type qualifiers that are not compatible with the member function using vector该对象具有与使用向量的成员函数不兼容的类型限定符
【发布时间】:2020-06-04 14:03:34
【问题描述】:

示例生成错误

对象具有与成员不兼容的类型限定符 功能

我不知道为什么。

啊.h

class A {
public:
   void f2(XXX* ..) const;
protected:
   const vector<XYZ> f(){return m;}
   vector<XYZ> m;
}

A.cpp

void A::f2(XXX* ..) const
{
const vector<XYZ>& P= this->f(); // Here I get this error as well
}

谁能解释我做错了什么? 谢谢。

【问题讨论】:

    标签: c++ stl


    【解决方案1】:

    您不能从const 函数调用非常量函数。这意味着f 也需要是const 合格的:

    const std::vector<XYZ> f() const { return m; }
    

    否则你不能从f2调用它。

    【讨论】:

    • 嘿!谢谢你的回答。看起来 f() 之后的 const 解决了我的问题。关于你答案的第一部分 - 这是我的问题,它都写在我的代码中,但我忘了在这里写下来。无论如何,它现在有效。非常感谢!!
    • 是的,我删除了那部分答案。如果它解决了您的问题,请尽可能考虑accepting
    猜你喜欢
    • 2014-08-31
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2017-03-09
    相关资源
    最近更新 更多