Issues compiling. Many an issue with ostream?

Hi- I'm having issues with classes.

In my main function I have something like this:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "ComplexNumber.h"

int main()
{
        ComplexNumber test;
	test = ComplexNumber(1,2);
	
	test.print(std::cout);
return 0;
}


in my class I have:
void print(std::ostream& os);

and in my ComplexNumber.cpp file I have:
1
2
3
4
5
void ComplexNumber::print(std::ostream& os)
{
	os << "Real: " << realNumb;
	os << "Imaginary: " << imagNumb;
}


For some reason when I debug my program in Visual Studios it stops at the print function and I have to step through the rest.

in the Call Stack it says:
> xyz.exe!Complex::print(std::basic_ostream<char,std::char_traits<char> > & os) Line 13 C++

Why won't it compile and run correctly? Why does it stop there? When I Build Solution I come up with no errors.
Last edited on
1
2
3
4
int main()
{
        Complex test;
	test = ComplexNumber(1,2);


Is Complex different from ComplexNumber?
You have shown us all the pieces that you think are correct. Perhaps the error is elsewhere? Can we see your whole ComplexNumber.h and ComplexNumber.cpp?
Disch.... I'm sorry i fixed the naming convention in my previous code. I typed it in wrong.

ComplexNumber.h
1
2
3
4
5
6
7
8
9
10
11
class ComplexNumber
{
public:
	ComplexNumber();
	ComplexNumber(double realNumb, double imagNumb);
	void print(std::ostream& os);

private:
	double realPart;
	double imagPart;
};


ComplexNumber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "ComplexNumber.h"

ComplexNumber::ComplexNumber() : realNumb(0.0), imagNumb(0.0)
{
}


ComplexNumber::ComplexNumber(double real, double imaginary) : realNumb(real), imagNumb(imaginary)
{
}

void ComplexNumber::print(std::ostream& os)
{
	os << "Real: " << realNumb;
	os << "Imaginary: " << imagNumb;
}
I don't see anything wrong.

What error does it give you? You said the debugger snaps, but is there any error message?
Disch++, even though he's not an L-value. This code looks fine. Maybe you set a breakpoint at that point and forgot about it? I dunno, just throwing stuff out there.

-Albatross
Albatross.... thank you for mentioning that. I'm usually use to working in "gedit" on Fedora but since I'm just learning classes I figured I'd go with Visual Studios for now (for debugging reasons).

With that said, apparently I did set a breakpoint trying to figure out how to get the line numbers to come up. I had not clue there was a such thing as a breakpoint. I just thought that the red dot meant that there was some sort of an error there.

Anyways... I'm a little embarrassed that I had to publicly go through this so I'm just going to shut up and keep on working. Thanks guys.
Hey, I made that mistake once too.

-Albatross
LOL!
It was when I was beginning programming. I didn't have to go public about it, but I once set a breakpoint and my program kept stopping at that point. I did it to keep the console from closing on me when the program was done. However, I eventually worked out a way around this (cin.ignore()). Needless to say, after putting that in it took me a few minutes before I figured out why my program was stopping twice.

-Albatross
Topic archived. No new replies allowed.