0
#include <iostream>
using namespace std;

template<class T>
class MyClass {
    private:
        T data;
    public:
        MyClass(T data) { this->data = data; }
};

template<class T>
bool operator< (const MyClass<T> & a, const MyClass<T> & b) {
    return (a.data < b.data);
}

int main() {
    MyClass<int> x(1);
    MyClass<int> y(2);

    if(x < y) cout << "x is less than y!" << endl;
    return 0;
}

when i compile this it says T data is private, i know i needa declare some friend operators but not really sure how to do it :( can anyone help me please??

4
Contributors
3
Replies
17
Views
3 Years
Discussion Span
Last Post by richieking
0

You don't have a permission to create this line
you should to work with the getter in the class because it is private .

 return (a.data < b.data);

here for example you can type somting like this

Getdata(); // you should to define this method in your class. and to write it as public 

Edited by zouhair.kasmi

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.