So I'm trying to write a program that is due tomorrow. I am trying to overload operators and dealing with template classes. I have everything written already but keep getting this error whenever i try to compile the driver file:

NOTE: proj4.cpp has #include "box.h" and box.h compiles fine, but it says box.h has errors whenever i try to compile proj4.cpp

student@student-desktop:~$ g++ -c proj4.cpp
box.h: In member function ‘void listT<elemType>::insert(elemType) [with elemType = Box]’:
proj4.cpp:51: instantiated from here
box.h:18: error: ‘bool Box::operator<(Box&)’ is private
listT.cpp:63: error: within this context

Here is some of the code that I think is part of the problem:


#ifndef _Box_H
#define _Box_H
#include <iostream>
#include <string>
using namespace std;

class Box
	//Insertion(<<) operator definition
	friend ostream& operator<< (ostream&, const Box&);
	//Extraction(>>) operator definition
	friend istream& operator>> (istream&, Box&);
	//Equality(==) operator definition
	bool operator== (Box&); 
	//Greater than(>) operator definition
	bool operator> (Box&);
	//Less than(<) operator definition
	bool operator< (Box&); *********************************************LINE18 ERROR


//Overloading equality(==) operator
bool Box::operator== (Box& box)
	return (getSurfaceArea() == box.getSurfaceArea());

//Overloading greater than(>) operator
bool Box::operator> (Box& box)
	return (getSurfaceArea() > box.getSurfaceArea());

//Overloading less than(<) operator
bool Box::operator< (Box& box)
	return (getSurfaceArea() < box.getSurfaceArea());


template<class elemType>
void listT<elemType>::insert(elemType insertItem)
	elemType temp;
	list[length++] = insertItem;
	for(int i = length - 1; i >= 0; i--)
		if(list[i] < list[i-1])*********************************LINE63 ERROR
			temp = list[i];
			list[i] = list[i-1];
			list[i-1] = temp;
} // end insert


//Load data into array
	inFile >> boxTemp;
	bList.insert(boxTemp);**********************************************LINE 51 ERROR

		inFile >> boxTemp;

Can anyone help me? thanks again.

By default members of a class are private not public. If you want to make them public you need to use the public keyword.

class foo
    int bar;
    foo() : bar(10) {}
    int getBar() const {return bar; }

int main
    foo temp;
    int testBar;
    testBar =; // error bar is private
    testBar = temp.getBar();  // okay getBar() is public

Thank you but i already know that, it is saying that operator< is private...and i did not reference any private data members in the overloading implementation. Why is it telling me operator< is private?

You never put the function in the public part of your class so the entire function is private and can not be used by anything other than other member functions. So when you try to use it in your list class it will cause an error because the list is not part of the class.

Edited 5 Years Ago by NathanOliver: n/a

This question has already been answered. Start a new discussion instead.