I need some clarification please

This is giving me some kind of error I do not understand:

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
//class templates & friend functions
#include <iostream>

using namespace std;

void  print(void);

template <class T>
class test
{
    private:
        double d1,d2;
    public:
        test(T a, T b)
        {
            d1=a;
            d2=b;
            lowest();
            print();
        }
        friend void print();
        T lowest();
};

template<class T>
T test<T>::lowest()
{
    T ans;
    ans=d1<d2 ? d1 : d2;
    cout<<"\n\nLowest value: "<<ans;
}

template <class T>
void print()
{
    test<T> temp;
    test<T> *ptr=&temp;
    cout<<"\nd1 = "<<ptr->d1;
    cout<<"\nd2 = "<<ptr->d2;
}

int main()
{
    double x,y;

    cout<<"Enter a number: ";
    cin>>x;
    cout<<"Type in another number: ";
    cin>>y;
    test<double> obj1(x,y);
    cin.get();
}


ERROR: undefined reference to 'print()'
this error comes with no line number?!?
I use Code::Blocks with Dev-C++ MingW's compiler
In the constructor of class test the friend function print is called

1
2
3
4
5
6
7
        test(T a, T b)
        {
            d1=a;
            d2=b;
            lowest();
            print();
        }


However it is only declared

void print(void);

but is not defined.

The other function with the same name is a template function that is it is not the same as the friend function. You declared two overload functions with name test in the global namespace one of wich (non-template) is not defined.
I defined it in line 34 and made another constructor test(){} but it still did not work!?!
Last edited on
As I said it is another function. Your friend function is not a template function. It is declared as

void print(void);

without any template parameter.
If you would want to have a template friend function you have to write

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <class T>
class test
{
    private:
        double d1,d2;
    public:
        test(T a, T b)
        {
            d1=a;
            d2=b;
            lowest();
            print();
        }
        template <class T2>
        friend void print();
        T lowest();
};
Post your new code.
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
//class templates & friend functions
#include <iostream>

using namespace std;

void  print(void);

template <class T>
class test
{
    private:
        double d1,d2;
    public:
        test(T a, T b)
        {
            d1=a;
            d2=b;
            lowest();
            print();
        }
        test(){}       //this is the new thing
        friend void print();
        T lowest();
};

template<class T>
T test<T>::lowest()
{
    T ans;
    ans=d1<d2 ? d1 : d2;
    cout<<"\n\nLowest value: "<<ans;
}

template <class T>
void print()
{
    test<T> temp;
    test<T> *ptr=&temp;
    cout<<"\nd1 = "<<ptr->d1;
    cout<<"\nd2 = "<<ptr->d2;
}

int main()
{
    double x,y;

    cout<<"Enter a number: ";
    cin>>x;
    cout<<"Type in another number: ";
    cin>>y;
    test<double> obj1(x,y);
    cin.get();
}
Are you reading what I wrote to you?!
Thanks a lot vlad from moscow. it is now working!!!
but does that mean friends are not members of classes?
Last edited on
closed account (o3hC5Di1)
Friends are indeed not actual members of classes (although I'm not sure how it works under the hood).
They are functions or classes which can be granted permission to access the private member data of the class that befriends them.

All the best,
NwN
Topic archived. No new replies allowed.