【问题标题】:Accessing vector values from a class_Type name not allowed error不允许从 class_Type 名称访问向量值错误
【发布时间】:2013-08-04 07:55:01
【问题描述】:

我将无符号值存储在一个向量中,我需要在另一个类中使用这些值。尝试在函数中返回向量值时出现错误,我不知道自己做错了什么!这是我的代码的一部分:

class Rider
{
  friend istream &operator>>(istream &in, Rider &rhs);

public:
  Rider(const string &name = ""): m_name(name){}

  const string &name() const {return m_name;}
  const string &team() const {return m_team;}
  const string &country() const {return m_country;}
  //const unsigned &numOfSatges() const {return m_tv;}

  typedef vector<unsigned> TimeVector;
  const vector<unsigned> & get() const{return TimeVector;) //Error: Type Name is not allowed!

private:

  string m_name;
  string m_team;
  string m_country;

  //unsigned m_numOfStages;

  TimeVector m_tv;

};

这里是我试图访问存储在向量中的无符号值的地方:

class Match_Stage : public unary_function<const Rider&, bool>
{
public:
  Match_Stage(const unsigned stage) : m_stage(stage){}
  bool operator()( const Rider &rider) const
  {
    return rider.TimeVector == m_stage; //Error: Type Name is not allowed!
  }

private:
   unsigned m_stage;
};

感谢您的帮助

【问题讨论】:

    标签: c++ vector typename


    【解决方案1】:

    TimeVector 是一个类型名。您正在使用它,就好像它是一个变量一样。就像说

    int == 42;
    

    你需要一个变量:

    int i = 3;
    i == 42;
    

    您可能想要的是在您的类中返回TimeVector 的实例:

    typedef vector<unsigned> TimeVector;
    const vector<unsigned> & get() const{return m_tv;)
    

    【讨论】:

    • 非常感谢您这么快回复。当我尝试你的解决方案时,我得到了错误:expected and expression。知道我错过了什么吗?谢谢
    • @samy 你很可能做错了什么。但我看到你已经有一个 TimeVector 数据成员,所以也许你只是想使用那个。
    猜你喜欢
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2015-09-30
    • 2017-01-10
    • 2017-07-17
    • 1970-01-01
    相关资源
    最近更新 更多