question

i have this code and when i try run it does not work ,i try to understand how can i inhertance from template base class , the error in "class choco:public cake<string>" it says you miss ";" but i think i dont and when i delet the template"<class T1>" it give me red line .

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
#include<iostream>
#include<string>
using namespace std;
template<class T1>    
class choco:public cake<string>{ 
string namee;
int id;
public:
cake(string n1,int i,T1 n,string f):cake(n,f){
nmaee=n1;
id=i;
}
string getnamee(){return namee;}
int getid(){return id;}
void print(){
cake::print();
cout<<"the name is "<<namee<<endl;
cout<<"the id is "<<id<<endl;
}
};
template<class T1>                           
class cake{ 
 T1 name;
 T1 flavor ;
public:
cake(T1 n, string f){
name=n;
flavor=f;
}
T1 getname(){return name;}
T1 getflavor(){return flavor;}
void print(){
cout<<"the name is "<<name<<endl;
cout<<"the flavor is "<<flavor<<endl;
}
 }; 
int main(){
cake<string>c("kk","l;");
cout<<c.getflavor();


system("pause");
return 0;
}
Line 9: Your constructor is incorrect. The constructor must be choco.
 
choco (string n1,int i,T1 n,string f) : cake(n,f)


Line 10: Your variable is mispelled, but this is not detected by the compiler because you never instantiate choco.

Place the template for cake<string> before choco.

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
#include<iostream>
#include<string>
using namespace std;

template<class T1>                           
class cake
{   T1 name;
    T1 flavor;
public:
    cake (T1 n, string f)
    {   name=n;
        flavor=f;
    }

    T1 getname()
    {return name;}

    T1 getflavor()
    {return flavor;}

    void print()
    {   cout<<"the name is "<<name<<endl;
        cout<<"the flavor is "<<flavor<<endl;
    }
 }; 

template<class T1>    
class choco : public cake<string>
{   string namee;
    int id;
public:
    choco (string n1,int i,T1 n,string f) : cake<string>(n,f)
    {   namee=n1;
        id=i;
    }
    string getnamee()
    {return namee;}

    int getid()
    {return id;}

    void print()
    {   cake::print();
        cout<<"the name is "<<namee<<endl;
        cout<<"the id is "<<id<<endl;
    }
};

int main()
{   cake<string> c ("kk","l;");
    cout<< c.getflavor();

    system("pause");
    return 0;
}
l;Press any key to continue 

. . .
Last edited on
Topic archived. No new replies allowed.