简体   繁体   English

如何从模板化类中的变量返回向量迭代器?

[英]How do you return a vector iterator from a variable in a templated class?

I'm trying to return an iterator for a vector in a templated class (I'm not sure if that makes a difference, but I've read that may, so I thought I'd mention it). 我正在尝试为模板化类中的向量返回迭代器(我不确定这是否有区别,但是我已经读过了,所以我以为提到了)。 The problem is that I get an error about C++ not supporting default-int when I try this. 问题是,当我尝试此操作时,出现关于C ++不支持default-int的错误。 I've looked online and from what I can see in forums and explanaions, I don't think I'm that far off, it just won't compile. 我已经看过网上了,从论坛和解释中可以看到,我认为我没有那么遥远,只是无法编译。

template<class T>
class Table
{
public:
  ...

  vector<shared_ptr<vector<T>>>::iterator GetRowIterator();
  //vector<shared_ptr<vector<CellValueType> > >::const_iterator GetRowIterator();

  ...
protected:

  vector<shared_ptr<vector<CellValueType> > > data;  //outside vector is rows, inside vector is columns

  ...
};

vector<shared_ptr<vector<T> > >::const_iterator Table<T>::GetRowIterator()
{
  return data.begin();
}

The errors that I get are: 我得到的错误是:

error C2146: syntax error : missing ';' before identifier 'GetRowIterator'

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   

Edit: 编辑:
Changed the end angle brackets so they are not as close together - same error. 更改了端角括号,使它们之间的距离不太近-相同的错误。

Any thoughts as to why this is occurring? 有什么想法为什么会这样呢?
As always, thanks for advice/help in advance! 一如既往,感谢您的建议/帮助!

Also remember to use typename when declaring the template-dependent return type: 另外,在声明依赖模板的返回类型时,请记住使用typename:

typename vector< shared_ptr< vector< T > > >::iterator GetRowIterator();

and the method definition 和方法定义

typename vector< shared_ptr< vector< T > > >::const_iterator Table<T>::GetRowIterator()
{
  return data.begin();
}

Notice also that when defining a template class method outside the class definition, you have to use the template keyword: 还要注意,在类定义之外定义模板类方法时,必须使用template关键字:

template <class T> typename vector< shared_ptr< vector< T > > >::const_iterator Table<T>::GetRowIterator()
    {
      return data.begin();
    }

So that the compiler can know what the T is about. 使编译器可以知道T的含义。

This part here: 这部分在这里:

vector<shared_ptr<vector<T>>>

It is a problem with the C++ syntax you can not put >> together like that. C ++语法存在问题,您不能像这样将>>放在一起。

vector<shared_ptr<vector<T> > >

This is a problem that is being addressed by the new standard. 新标准正在解决此问题。

Because the lexer is the first stage of the compiler it sees the >>> as a shift left operator followed by >. 由于词法分析器是编译器的第一阶段,因此它将>>>视为左移运算符,后跟>。 Thus you are getting syntax errors in your code. 因此,您在代码中遇到语法错误。 To supress this problem you just need to add white space between the > when closing templates. 要解决此问题,您只需要在关闭模板时在>之间添加空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM