从class_Type名称访问矢量值时不允许出现错误

Accessing vector values from a class_Type name not allowed error

本文关键字:不允许 错误 class Type 访问      更新时间:2023-10-16

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

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

感谢您的帮助

TimeVector是一个类型名。你把它当作一个变量来使用。这就像说

int == 42;

你需要一个变量:

int i = 3;
i == 42;

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

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