• Advertisement
Sign in to follow this  

missing ''? (operator overloading)

This topic is 4216 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This program was made so that I could practice operator overloading and a seemingly simple problem is baffling me. The error is for each of the three lines in my ostream function and it reads, missing ';' before 'std::cout'. Here is the code:
#include <iostream>
#include <string>

class heavyarmor
{
	friend std::ostream& operator<<(std::ostream& os, heavyarmor& passedha);

	public:
		heavyarmor(const std::string armorname = ""): m_armorname(armorname) {}
		int m_resistance;
		int m_weight;
		void displaystats();
		std::string getname() { return m_armorname; }
	private:
		std::string m_armorname;
};

void heavyarmor::displaystats()
{
	std::cout << "heavyarmor's stats:\n";
	std::cout << "Chainmail:\tArmor resistance : " << m_resistance;
	std::cout << "\tArmor weight : " << m_weight;
}

std::ostream& operator<<(std::ostream& os, heavyarmor& passedha);

int main()
{
	heavyarmor chainmail("Orogulf's Bright Chain Vest");
	chainmail.m_resistance = 3;
	chainmail.m_weight = 5;
	chainmail.displaystats();
	std::cout << chainmail;

	return 0;
}

std::ostream& operator<<(std::ostream& os, heavyarmor& passedha)
{
	os std::cout << "\nCalling chainmail's name with the overloaded";
	os std::cout << " operator (.)\n";
	os std::cout << "\tArmor name : " << passedha.getname();
	
	return os;
}



Share this post


Link to post
Share on other sites
Advertisement
remove the std::cout. i think you'll be fine then.

std::ostream& operator<<(std::ostream& os, heavyarmor& passedha)
{
os << std::endl << "Calling chainmail's name with the overloaded";
os << " operator (.)" << std:endl;
os << "\tArmor name : " << passedha.getname();

return os;
}

In C++, it's better and standard to use std::endl to cause a newline than "\n". Unless you have a reason why you don't want the stream buffer flushed.

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
In C++, it's better and standard to use std::endl to cause a newline than "\n". Unless you have a reason why you don't want the stream buffer flushed.


Actually, it's better to use '\n' for the intermediate newlines and keep std::endl for the last one. When writing an overloaded stream insertion operator, using '\n' internally is probably preferable: leave the decision for flushing the output to the user of the class -- unless you are writing a lot of data (in which case it would get flushed anyway).

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by Alpha_ProgDes
In C++, it's better and standard to use std::endl to cause a newline than "\n". Unless you have a reason why you don't want the stream buffer flushed.


Actually, it's better to use '\n' for the intermediate newlines and keep std::endl for the last one. When writing an overloaded stream insertion operator, using '\n' internally is probably preferable: leave the decision for flushing the output to the user of the class -- unless you are writing a lot of data (in which case it would get flushed anyway).


That's basically what I heard. I heard '\n' has less overhead, and to use std::endl at the end to prevent formatting problems.

Share this post


Link to post
Share on other sites
Two more questions, when trying to run from console, I get to my debug folder that contains the .exe, but when I get to the debug directory in the console, I type in the name of the exe and its say it cannot recognize it as a command.

Second, how do I make a shortcut to a directory running from console.

Share this post


Link to post
Share on other sites
Quote:
Original post by kevtimc
Two more questions, when trying to run from console, I get to my debug folder that contains the .exe, but when I get to the debug directory in the console, I type in the name of the exe and its say it cannot recognize it as a command.


Because you say ".exe", I assume Windows (thus a DOS window).

- Does the .exe appear when you 'dir' in that directory?
- Try it including the .exe extension.
- Try specifying it as .\{name of exe}.

Quote:
Second, how do I make a shortcut to a directory running from console.


I'm not really sure what you mean.

Share this post


Link to post
Share on other sites
Quote:
Original post by kevtimc
Second, how do I make a shortcut to a directory running from console.


If you go to your start menu to accessories, then right click on the Command Prompt icon and do Sent To -> Desktop (Create Shortcut), it will (dur) place a shortcut to your command prompt on the desktop.

If you then right click the shortcut, in properties you should see an edit box labelled "Start In" or something, which will be the Windows directory or your My Documents directory, or just blank, by default. Can't remember

Type the complete path to your project's folder in here and click OK. When you double click this shortcut, it should now open in your project folder.

This will only apply to this specific shortcut and other Command Prompt icons will still behave as before.

HTH Paul

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by kevtimc
Two more questions, when trying to run from console, I get to my debug folder that contains the .exe, but when I get to the debug directory in the console, I type in the name of the exe and its say it cannot recognize it as a command.


Because you say ".exe", I assume Windows (thus a DOS window).

- Does the .exe appear when you 'dir' in that directory?
- Try it including the .exe extension.
- Try specifying it as .\{name of exe}.


-Yes
-Didn't work
-Don't understand

Share this post


Link to post
Share on other sites
Very strange. So, when you type dir, your program appears in the list as "name.exe", but if you type "name" or "name.exe" it says it can't recognise the command?

Zahlman is suggesting you type .\name.exe, which would equate to c:\the_full_current_directory\name.exe, although if the name on its own isn't working, I don't see why that would. I'm not saying it is not worth a try, but would be very very weird if that worked, but the name on its own didn't.

Just to clarify, we are talking Windows? We are talking either MS-DOS Prompt (Win98 etc) or Command Prompt (2000, XP etc)?

If I was in C:\Paul, and there was an exe called MMORPG.exe in that directory, the commands would look (a bit) like this:

C:\Paul> dir
Contents of C:\Paul or some such nonsense
. <dir>
.. <dir>
MMORPG.EXE Size Blah blah blah MMORPG.exe

Total size: whatever blah blah

C:\Paul> MMORPG
Welcome to the ultimate MMORPG. Press a key to exit.
x
Thank you for playing.

C:\Paul> etc...

Does that tally with what you are seeing (except for my improvised bits, obviously)? Very weird if so.

[EDIT]
Just to clarify, here is a screenshot of me navigating to a directory and running a file called x.exe from XP Command Prompt, in case it helps:

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement