【发布时间】:2016-05-14 14:11:13
【问题描述】:
template <class T>class vector{ //this is the part for my vector, which may lead to the error shown below.
protected:
T* arr;
void expand(){
if(size<capacity)return;
if(capacity<con)capacity=con;
T* temp=new T[capacity=2*capacity];
for(int i=0;i<size;i++)temp[i]=arr[i];
delete[] arr;
arr=temp;
temp=NULL;
};
public:
vector(int c=con,int s=0,T v=0){arr=new T[capacity=c];for(size=0;size<s;arr[size++]=v);}
T& operator[](int r)const{return arr[r];}
int insert(int r,const T& e){ //inserting at designated place.
expand();
for(int i=size;i>r;i--)arr[i]=arr[i-1];
arr[r]=e;
size++;
return r;
}
int insert(const T& e){ //inserting at the end
return insert(size,e);
}
};
template<class Tv>class vertex{
public:
Tv data;
vertex(Tv const& e):data(e){}
};
template<class Tv,class Te>class GraphMatrix{
private:
vector<vertex<Tv> > v;
public:
int insert(Tv const& vt){
return v.insert(vertex<Tv>(vt)); //error happens here "expected primary-expression before '>' token."
}
}
int main(){
GraphMatrix<int,int> mail;
int i=1;
mail.insert(i);
}
我正在使用 g++ 编译器,但出现错误“预期在 '>' 标记之前的主表达式”。 是模板的问题吗?我该如何解决? 现在问题发生在将 vertex(vt) 插入我编写的向量(我自己的向量)中。
【问题讨论】:
-
第二个错误来自级联。第一个是关于
vertex<Tv>在vertex<Tv> p中的使用;一旦失败,p就没有意义了。所以忽略那个;一旦你修复了第一个,它几乎肯定会消失。 -
对不起,我做了一些修改以使其更清晰。
-
vector::insert没有一个参数。它也不返回int。如您所知,这是否是您的真实代码:ideone.com/RU1TFW -
是的,我写了自己的向量,这里没有介绍。但似乎问题与插入函数中的 vertex
更相关。
标签: c++ templates initialization