Erro: Need typename because vector<type> is a dependent scope
Apr 9, 2016 at 9:14am UTC
I am writing a class for a priority vector, but I am facing trouble when I declare an iterator with <type> which is the typename of my class template. The error is in line 37.
error: need 'typename' before 'std::vector<type>::iterator' because 'std::vector<type>' is a dependent scope|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include<iostream>
#include<vector>
#include<typeinfo>
using namespace std;
string first (string a, string b){
int siz = min(a.size(), b.size());
int place = 0;
while (a[place]==b[place] && place<siz){
place++;
}
if (a[place]>b[place]){
return b;
}
else if (a[place]<b[place]){
return a;
}
}
template <class type>
class priority_vector{
public :
type check;
string check2;
vector <type> pv;
bool isString(){
if (typeid (type).name()==typeid (check2).name()){return true ;}
else {return false ;}
}
void push_in(type input){
if (isString()){
if (pv.size()==0){
pv.push_back(input);
}
else {
vector <type> :: iterator vit;
vit = pv.begin();
while (first(*pv, input)==*pv){
pv ++;
}
vit.insert(vit, input);
}
}
else {
if (pv.size()==0){
pv.push_back(input);
}
else {
vector <type> :: iterator vit;
vit = pv.begin();
while (*pv<input){
pv++;
}
vit.insert(vit, input);
}
}
}
type operator [](int a){
return pv[a];
}
};
int main(){
priority_vector <string> v_s;
int n;
cin>>n;
int in;
for (int i=0; i<n; i++){
cin>>n;
v_s.push_in(n);
}
for (int i=0; i<n; i++){
cout<<v_s[i];
}
return 0;
}
Apr 9, 2016 at 12:38pm UTC
The error is pretty clear. You need to use
typename to tell the compiler that vector<type>::iterator is a type, otherwise the compiler will assume it's a variable or function.
typename vector<type>::iterator vit;
Last edited on Apr 9, 2016 at 12:39pm UTC
Apr 9, 2016 at 2:04pm UTC
Yeah, but why does it need that? Type was already declared at the template.
Apr 9, 2016 at 2:17pm UTC
Topic archived. No new replies allowed.