#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??

Recommended Answers

All 3 Replies

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 

declare the operator inside the class.

Then use bool Myclass::operator<()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.