13 Replies - 886 Views - Last Post: 01 September 2013 - 11:40 PM Rate Topic: -----

#1 Warsaw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 04-August 13

Problem with operator overloading without friends

Posted 31 August 2013 - 09:44 PM

Hi, I am trying to overload the output operator without using "friend" and got an error if used like...
// Error: no operator "<<" matches these operands
out << "Directed by " << strDDirector << endl;



Here is the code for the classes and the operator overload:
class Media {
	public:
		// Constructor
		Media() : intMID(0), strMTitle(), intMYear() {}
		Media(int intID, string strTitle, int intYear) : intMID(intID), strMTitle(strTitle), intMYear(intYear) {}
		// Member functions
		void changeID(int NewID) {
			intMID = NewID;
		}
		ostream& print(ostream& out) const {
			out << " ID " << intMID << " Title " << strMTitle << " (" << intMYear << ") "; 
			return out;
		}
	private:
		// Data members
		int intMID, intMYear;
		string strMTitle;
};

class DVD: public Media {
	public:
		// Constructor
		DVD(): strDDirector() {}
		DVD(int intID, string strTitle, int intYear, string strDirector = "unknown") : Media(intID, strTitle, intYear) {
			strDDirector = strDirector;
		}
		// Member functions
		ostream& print(ostream& out) const {
			out << "DVD:";
			out << Media::print(out);
			out << "Directed by " << strDDirector << endl;
			return out;
		}
	private:
		// Data members
		string strDDirector;
};

class Book: public Media {
	public:
		// Constructor
		Book() : strBAuthor(), intBPages(0) {}
		Book(int intID, string strTitle, int intYear, string strAuthor, int intPages) : Media(intID, strTitle, intYear) {
			strBAuthor = strAuthor;
			intBPages = intPages;
		}
		// Member functions
		ostream& print(ostream& out) const {
			return out;
		}
	private:
		// Data members
		string strBAuthor;
		int intBPages;
};

class Journal: public Media {
	public:
		// Constructor
		Journal() : intJVol(0), intJNum(0) {}
		Journal(int intID, string strTitle, int intYear, int intVol, int intNum) : Media(intID, strTitle, intYear) {
			intJVol = intVol;
			intJNum = intNum;
		}
		// Member functions
		ostream& print(ostream& out) const {
			return out;
		}
	private:
		// Data members
		int intJVol, intJNum;
};

// Operator overloading
ostream& operator<<(ostream& out, const Media& m) {
	return m.print(out);
}


This post has been edited by Warsaw: 31 August 2013 - 09:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with operator overloading without friends

#2 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • Joined: 26-June 13

Re: Problem with operator overloading without friends

Posted 31 August 2013 - 10:09 PM

When is this error occurring, during compilation?
Was This Post Helpful? 0
  • +
  • -

#3 Warsaw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 04-August 13

Re: Problem with operator overloading without friends

Posted 31 August 2013 - 10:12 PM

Before. I am using visual studio and it puts a red underline on the second << symbol.
Was This Post Helpful? 0
  • +
  • -

#4 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • Joined: 26-June 13

Re: Problem with operator overloading without friends

Posted 31 August 2013 - 10:55 PM

I was able to compile under visual studio. I was also able to compile it using devcpp.
Was This Post Helpful? 0
  • +
  • -

#5 Warsaw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 04-August 13

Re: Problem with operator overloading without friends

Posted 31 August 2013 - 11:56 PM

im using visual studio 2012 and its not compiling for me saying there are build errors. :(
Was This Post Helpful? 0
  • +
  • -

#6 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • Joined: 26-June 13

Re: Problem with operator overloading without friends

Posted 01 September 2013 - 01:51 AM

Where's the error occurring, in this statement

out << "Directed by " << strDDirector << endl;



What is the exact error the compiler is outputting? Could you copy and paste it?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: Problem with operator overloading without friends

Posted 01 September 2013 - 01:59 AM

The code posted in post #1 doesn't even compile as is.

I suspect that the OP forgot to post the lines:
#include <iostream>

using namespace std;



which will give the error he is reporting.

Is suspect that salazar and Warsaw are not seeing the errors because they unconsciously supplied the lines:
#include <iostream>
#include <string>

using namespace std;



Without including the string header file, the error the OP was seeing would have looked like:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)
IntelliSense: no operator "<<" matches these operands
            operand types are: std::basic_ostream<char, std::char_traits<char>> << const std::string
IntelliSense: no operator "<<" matches these operands
            operand types are: std::basic_ostream<char, std::char_traits<char>> << const std::string


Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Problem with operator overloading without friends

Posted 01 September 2013 - 03:11 AM

Code looks mostly ok, though it's unclear why print returns ostream...

Perhaps you want that print to be virtual? Actually, if it was pure virtual, then you'd prevent instances of Media, which might be desirable, considering it's a base class.
Was This Post Helpful? 1
  • +
  • -

#9 Warsaw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 04-August 13

Re: Problem with operator overloading without friends

Posted 01 September 2013 - 03:32 AM

ok here is the full code. what i posted earlier did not include the headers and namespaces...
#include <iostream>
#include <cstring>

using namespace std;

//=================================================================//
//                           CLASSES                               //
//=================================================================//
class Media {
	public:
		// Constructor
		Media() : intMID(0), strMTitle(), intMYear() {}
		Media(int intID, string strTitle, int intYear) : intMID(intID), strMTitle(strTitle), intMYear(intYear) {}
		// Member functions
		void changeID(int NewID) {
			intMID = NewID;
		}
		virtual ostream& print(ostream& out) const {
			out << " ID " << intMID << " Title " << strMTitle << " (" << intMYear << ") "; 
			return out;
		}
	private:
		// Data members
		int intMID, intMYear;
		string strMTitle;
};

class DVD: public Media {
	public:
		// Constructor
		DVD(): strDDirector() {}
		DVD(int intID, string strTitle, int intYear, string strDirector = "unknown") : Media(intID, strTitle, intYear) {
			strDDirector = strDirector;
		}
		// Member functions
		ostream& print(ostream& out) const {
			out << "DVD:";
			out << Media::print(out);
			out << "Directed by " << strDDirector << endl;
			return out;
		}
	private:
		// Data members
		string strDDirector;
};

class Book: public Media {
	public:
		// Constructor
		Book() : strBAuthor(), intBPages(0) {}
		Book(int intID, string strTitle, int intYear, string strAuthor, int intPages) : Media(intID, strTitle, intYear) {
			strBAuthor = strAuthor;
			intBPages = intPages;
		}
		// Member functions
		ostream& print(ostream& out) const {
			return out;
		}
	private:
		// Data members
		string strBAuthor;
		int intBPages;
};

class Journal: public Media {
	public:
		// Constructor
		Journal() : intJVol(0), intJNum(0) {}
		Journal(int intID, string strTitle, int intYear, int intVol, int intNum) : Media(intID, strTitle, intYear) {
			intJVol = intVol;
			intJNum = intNum;
		}
		// Member functions
		ostream& print(ostream& out) const {
			return out;
		}
	private:
		// Data members
		int intJVol, intJNum;
};

// Operator overloading
ostream& operator<<(ostream& out, const Media& m) {
	return m.print(out);
}



what i am trying to do is to overload the input operator using this code:
// Operator overloading
ostream& operator<<(ostream& out, const Media& m) {
	return m.print(out);
}



...and link it to the print function of the base and derived classes (changed to virtual from baavgai's comment) :
// From the base class Media
virtual ostream& print(ostream& out) const {
			out << " ID " << intMID << " Title " << strMTitle << " (" << intMYear << ") "; 
			return out;
		}


// Derived class DVD
ostream& print(ostream& out) const {
			out << "DVD:";
			out << Media::print(out);
			out << "Directed by " << strDDirector << endl;
			return out;
		}



here is the error i am getting... :(/>

Quote

1 IntelliSense: no operator "<<" matches these operands
operand types are: std::basic_ostream<char, std::char_traits<char>> << const std::string

i think its when the case is "<< stringVariable"
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Problem with operator overloading without friends

Posted 01 September 2013 - 04:14 AM

Interesting. Compiles fine for me. Of course, Microsoft and STL...

Look at this out << Media::print(out);. What is the return type of print? Why are you feeding that to out? Again, having print with any return type is probably messing you up.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: Problem with operator overloading without friends

Posted 01 September 2013 - 02:29 PM

That code in post #9 fails to compile with this set of errors in VS2012:
Error	1	error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)	c:\z\test\simplec++11\simplec++11.cpp	19	1	SimpleC++11
Error	2	error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)	c:\z\test\simplec++11\simplec++11.cpp	39	1	SimpleC++11
	3	IntelliSense: no operator "<<" matches these operands
            operand types are: std::basic_ostream<char, std::char_traits<char>> << const std::string	c:\z\Test\SimpleC++11\SimpleC++11.cpp	19	41	SimpleC++11
	4	IntelliSense: no operator "<<" matches these operands
            operand types are: std::basic_ostream<char, std::char_traits<char>> << const std::string	c:\z\Test\SimpleC++11\SimpleC++11.cpp	39	26	SimpleC++11



And the reason for it because of line #2:
#include <cstring>


That line include the old C-style strings function header file.

What you want is the new C++ style header file:
#include <string>


Was This Post Helpful? 1
  • +
  • -

#12 Warsaw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 04-August 13

Re: Problem with operator overloading without friends

Posted 01 September 2013 - 02:49 PM

@Skydiver: thanks that fixed the problem. :)/>

@baavgai: yup, changed it to
Media::print(out);
...the previous one was returning rubbish.


by the way, is there a way to return the name of the class being used instead of manually typing it to the output like
 out << "DVD:"; 
?

This post has been edited by Warsaw: 01 September 2013 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Problem with operator overloading without friends

Posted 01 September 2013 - 04:31 PM

At some point, you're going to have to type something. However, if I were doing this, I'd add another method to your base class.

e.g.
class Media {
	// ..
	virtual const string &getMediaType() const { return "?"; }
	virtual void print(ostream& out) const {
		out << getMediaType() << ": ID " << intMID << " Title " << strMTitle << " (" << intMYear << ") "; 
	}
};

class DVD: public Media {
	const string &getMediaType() const { return "DVD"; }	
	virtual void print(ostream& out) const {
		Media::print(out);
		out << "Directed by " << strDDirector << endl;
	}



This has the bonus that you can easily identify the type of instance you have.
Was This Post Helpful? 1
  • +
  • -

#14 Warsaw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 04-August 13

Re: Problem with operator overloading without friends

Posted 01 September 2013 - 11:40 PM

thanks! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1