新手模板查询 [英] novice template query

查看:77
本文介绍了新手模板查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在编译由''std :: vector''支持的以下简单模板时,得到

以下编译时错误:


-----编译错误----------


tc2.h:在成员函数''void MyArray< T> :: display()'':

tc2.h:36:错误:预期`;''在'ivi'之前''

tc2.h:37:错误:''ivi''未在此范围内声明

tc2.h:在成员函数''void MyArray< T> :: display()[with T =

std :: basic_string< char,std :: char_traits< char> ;,std :: allocator< char>

Hi,
On compiling the following simple template backed by ''std::vector'', get
the following compile time error:

----- Compilation error ----------

tc2.h: In member function ''void MyArray<T>::display()'':
tc2.h:36: error: expected `;'' before ''ivi''
tc2.h:37: error: ''ivi'' was not declared in this scope
tc2.h: In member function ''void MyArray<T>::display() [with T =
std::basic_string<char, std::char_traits<char>, std::allocator<char>

]'':
tc2.h:49:从这里实例化

tc2.h:36:错误:dependent-name''std :: vector< T,std :: allocator< _CharT> :: iterator''被解析为非类型,但是实例化产生了输入
tc2.h:36:注意:如果类型是
]'': tc2.h:49: instantiated from here
tc2.h:36: error: dependent-name ''std::vector<T,std::allocator<_CharT>::iterator'' is parsed as a non-type, but instantiation yields a type tc2.h:36: note: say ''typename std::vector<T,std::allocator<_CharT>::iterator'' if a type is meant

,请说''typename std :: vector< T,std :: allocator< _CharT> :: iterator''

------代码--------------------------


模板< typename T>

class MyArray {

public:

MyArray();

~ MyArray();

void append(const T& item);

void display();

private:

std :: vector< T> * v;

};


模板< typename T>

MyArray< T> :: MyArray(){

v = new std :: vector< T>();

}


模板< typename T>

MyArray< T> ::〜MyArray(){

删除v;

}


模板< typename T>

void MyArray< T> :: append(const T& item){

v-> push_back(item);

}


模板< typename T>

void MyArray< T> :: display(){


std :: vector< T> :: iterator ivi; // ***我知道问题在这里***

for(ivi = v-> begin(); ivi!= v-> end(); ++ ivi)

std :: cout<< * ivi<< std :: endl;

}


int main(int argc,char ** argv){


MYARRAY<的std :: string> ma1;

ma1.append(" Test1");

ma1.append(" Test2");


ma1.display();

}


-------代码结束-------------- ---------------------


如有任何意见/建议吗?


谢谢。



------ Code --------------------------

template<typename T>
class MyArray {
public:
MyArray();
~MyArray();
void append(const T& item);
void display();
private:
std::vector<T> *v;
};

template<typename T>
MyArray<T>::MyArray() {
v = new std::vector<T>();
}

template<typename T>
MyArray<T>::~MyArray() {
delete v;
}

template<typename T>
void MyArray<T>::append(const T& item) {
v->push_back(item);
}

template<typename T>
void MyArray<T>::display() {

std::vector<T>::iterator ivi; // *** I know the issue is HERE ***
for(ivi = v->begin(); ivi != v->end(); ++ivi)
std::cout << *ivi << std::endl;
}

int main(int argc, char** argv) {

MyArray<std::string> ma1;
ma1.append("Test1");
ma1.append("Test2");

ma1.display();
}

------- Code ends -----------------------------------

Any comments/help please?

Thanks.

推荐答案

bb写道:

关于编译以下简单由''std :: vector''支持的模板,得到以下编译时错误:

-----编译错误----------

tc2.h:在成员函数''void MyArray< T> :: display()'':
tc2.h:36:错误:预期`;''在''ivi'之前'
tc2.h:37:错误:''ivi''未在此范围内声明
tc2.h:在成员函数''void MyArray< T> :: display()[with T =
std :: basic_string< char,std :: char_traits< char> ;,std :: allocator< char>
Hi,
On compiling the following simple template backed by ''std::vector'', get
the following compile time error:

----- Compilation error ----------

tc2.h: In member function ''void MyArray<T>::display()'':
tc2.h:36: error: expected `;'' before ''ivi''
tc2.h:37: error: ''ivi'' was not declared in this scope
tc2.h: In member function ''void MyArray<T>::display() [with T =
std::basic_string<char, std::char_traits<char>, std::allocator<char>



如果包含怎么办? de所需的标题,< string>和< vector>?


其他一切看起来还不错。


-

Ian Collins。


What happens if you include the required headers, <string> and <vector>?

Everything else looks to be OK.

--
Ian Collins.




这里的问题是编译器没有像b $ b那样的迭代器定义。


模板< typename T>

void MyArray< T> :: display(){

std :: vector< T> :: iterator ivi; // ***我知道问题是

这里***

for(ivi = v-> begin(); ivi!= v-> end (); ++ ivi)

std :: cout<< * ivi<< std :: endl;

}


如果我将其更改为具体的,例如std :: string如下,它编译

并运行正常。但是,我的问题是如何保持通用?


std :: vector< std:string> :: iterator ivi;


干杯。

Hi,
The problem here is the iterator definition that the compiler doesn''t
like.

template<typename T>
void MyArray<T>::display() {
std::vector<T>::iterator ivi; // *** I know the issue is
HERE ***
for(ivi = v->begin(); ivi != v->end(); ++ivi)
std::cout << *ivi << std::endl;
}

If I change it to be specific e.g. std::string as follows, it compiles
and runs ok. However, my question is how to keep it generic?.

std::vector<std:string>::iterator ivi;

Cheers.


bb写道:

这里的问题是编译器没有的迭代器定义喜欢。

模板< typename T>
void MyArray< T> :: display(){
std :: vector< T> :: iterator IVI; // ***我知道问题是
这里***
typename std :: vector< T> :: iterator ivi; for(ivi = v-> begin(); ivi!= v-> end(); ++ ivi)
std :: cout<< * ivi<< std :: endl;
}

如果我将其更改为具体的,例如std :: string如下,它编译
并运行正常。但是,我的问题是如何保持通用?

std :: vector< std:string> :: iterator ivi;

干杯。
Hi,
The problem here is the iterator definition that the compiler doesn''t
like.

template<typename T>
void MyArray<T>::display() {
std::vector<T>::iterator ivi; // *** I know the issue is
HERE *** typename std::vector<T>::iterator ivi; for(ivi = v->begin(); ivi != v->end(); ++ivi)
std::cout << *ivi << std::endl;
}

If I change it to be specific e.g. std::string as follows, it compiles
and runs ok. However, my question is how to keep it generic?.

std::vector<std:string>::iterator ivi;

Cheers.




迭代器是一种依赖类型。编译器不知道迭代器

是一种类型。使用typename。如上所示。



iterator is a dependent type. The compiler doesn''t know that iterator
is a type. Use typename. as shown above.


这篇关于新手模板查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆