返回信息流我想写一个处理vector的函数 但是想使用模板,这样就可以用来处理多种类型。
后来想干脆给写一个类,把自己写的使用vector的函数封装在一起
于是写了如下个头文件报错了
#include<iostream>
#include <vector>
template<typename T>
class Vectortest:public vector
{
public:
Vectortest(void);
~Vectortest(void);
int removeDup(vector<T> &v1);
这里提示说错误 “ 1 error C2061: 语法错误 : 标识符“vector” ”
int Display(vector<T>& v);
原来我使用的是void vs2008 报错错误“ 4 error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int” 是不是现在不能再写void类型的函数了?
private:
vector<T> v2;
vector<T>::const_iterator vectiter;
};
我对模板不是很熟,求达人指点~~~谢谢~ [em17]
这是一条镜像帖。来源:北邮人论坛 / cpp / #26197同步于 2009/7/13
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
关于template 的问题
unicornhx
2009/7/13镜像同步12 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
template <typename T>
class Vector: std::vector<T>
{
};
这样吧
说类型错误?using namespace std没?
【 在 unicornhx (unicorn) 的大作中提到: 】
: 我想写一个处理vector的函数 但是想使用模板,这样就可以用来处理多种类型。
: 后来想干脆给写一个类,把自己写的使用vector的函数封装在一起
: 于是写了如下个头文件报错了
: ...................
:) 添加后 没有了error C2061
但提示错误 1 error C2955: “std::vector”: 使用类 模板 需要 模板 参数列表
这是怎么回事?
你怎么改的?
【 在 unicornhx (unicorn) 的大作中提到: 】
: :) 添加后 没有了error C2061
: 但提示错误 1 error C2955: “std::vector”: 使用类 模板 需要 模板 参数列表
: 这是怎么回事?
: ...................
using namespace std;
template <typename T>
class YourVector: public vector<T> {
// members here
};
这样呢?
:) 有效果~
诶 现在运行后在这一句出现了一个错误提示
Vectortest<int>::Display(L);
error C2352: “Vectortest<T>::Display”: 非静态成员函数的非法调用
【 在 Vampire 的大作中提到: 】
: using namespace std;
: template <typename T>
: class YourVector: public vector<T> {
: ...................
//vectortest.cpp//
#include "vectortest.h"
using namespace std;
template <typename T>
void Vectortest<T>::removeDup(vector<T> &v1)
{
v2._Reverse=v1.size;
}
template<typename T>
void Vectortest<T>::Display(vector<T>& v)
{
vectiter=v.begin();
while(vectiter!=v.end())
cout<<*vectiter++<<" ";
}
template<typename T>
Vectortest<T>::Vectortest(void)
{
}
template<typename T>
Vectortest<T>::~Vectortest(void)
{
}
int main()
{
vector<int> L,M;
int n,val,s,sm;
cout<< "请输入向量总数:"<<endl;
cin>>n;
L.reserve(n);
M.reserve(n);
cout<<"请输入"<<n<<"个元素"<<endl;
for(int i=0;i<n;i++)
{
cin>>val;
L.push_back(val);
}
Vectortest<int>::Display(L); /// 这里提示非静态函数非法调用 是不是不能把main 放在这个cpp里?
return 0;
}
【 在 unicornhx 的大作中提到: 】
: //vectortest.cpp//
: #include "vectortest.h"
: using namespace std;
: ...................
不是只有静态的函数才能全局调用,否则要先定义一个对象调用?我瞎掰的