need help

why it syas when i call friend template class the "name" unclear indenifer!
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
#include<iostream>
using namespace std; 
template <class T> 
class SW { 
public:
      string name ;
	T key ;
	float price;

SW( T k, string n, float p )

{
Key=k;
name=n;
price=p;
}
 
template <typename t>
void set_key(T k)
{
       key=t;
}

T get_key()
{
       return key;
}
void print_SW_info ()
{
       cout<< name;
       cout<<price ;
       cout<<getKey
}
 friend void customer();
//complete hint: ask the user to inter his name and country  

}; 

void customer(){
cout<<"enter name";
cin >> name;}
 
 int main()
 {
SW <string>obj("x","y",1);
customer();
obj.set_key("x");
obj.print_SW_info();



/*SW <int>obj(12,"r",1.0);
customer();
obj2.set_key(12);*/
 
 
system("pause");
   return 0;
}
A friend declaration tells the compiler that the specified piece of code can access nonpublic members of a class.
However, to do so, said piece of code needs an instance of the class to access its members.
The function 'customer' doesn't have any.

1
2
3
4
5
6
7
8
9
template<class T>
void customer(SW<T>& object)
{
    cout<<"enter name";
    cin >> object.name;
}
...

customer(obj);


-----
Seriously, put some effort in formatting your code, it's awful to read.
i am sorry but what the effort should i put it to make my code clear!
i am just begginner ,so you r advice will be helpful please
thank you
Consistent parentheses and whitespace between tokens help make the code less confusing, though they are somewhat subjective.
However correct indentation is fundamental to help readers break up the code into separate logical blocks.
This is way easier to read IMO

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
#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(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;
}
Last edited on
Topic archived. No new replies allowed.