【问题标题】:boost apply::visitor -> discards qualifiersboost apply::visitor -> 丢弃限定符
【发布时间】:2012-11-24 12:58:16
【问题描述】:

我使用 boost::variant 制作了一个程序,但不知何故 const 不正确。

错误:将 'const CompareTitle' 作为 'bool CompareTitle::operator()(const T1&, const T2&) [with T1 = TestSeizoen, T2 = TestSeizoen]' 的 'this' 参数传递会丢弃限定符 [-fpermissive]

[T1=TestFilm, T2 = TestSeizoen] 等出现同样的错误。

这是代码:

#include <iostream>
#include <vector>
#include "boost/variant.hpp"

using namespace std;

class TestFilm{
private:
   string titel_;
public:
   TestFilm(const string& titel): titel_(titel){};
   const string titel() const {return titel_;};
};

class TestSeizoen{
private:
    string titel_;
public:
    TestSeizoen(const string& titel): titel_(titel){};
    const string titel() const {return titel_;};
};


struct CompareTitle: boost::static_visitor<bool>{
  template <typename T1, typename T2>
  bool operator() (const T1& t1 , const T2& t2){
     return t1.titel() == t2.titel();
  }
};


int main() {
   typedef boost::variant<TestFilm,TestSeizoen> var;
   vector <var> vec;
       TestFilm film1("titel1");
   vec.push_back(film1);
   TestSeizoen seizoen1("titel2");
   vec.push_back(seizoen1);
   vector<var>::iterator it;
   bool compare = boost::apply_visitor(CompareTitle(),*vec.begin(),*(vec.begin()+1));
   return 0;
}

我试图让 operator() 成为一个 const 成员函数,但这并没有解决问题。有人可以帮忙吗?如果需要,我可以提供更多信息。提前致谢。

【问题讨论】:

    标签: boost constants static-visitor


    【解决方案1】:

    我认为你需要保证 operator() 不会改变结构成员,即:

    bool operator() (const T1& t1 , const T2& t2) const {
    ...
    }
    

    这是为我编译的(使用 GCC 4.4.5)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多