Program showing no output on console.

Hi all,

I am trying a program with friend function(max) which is common to 2 classes(XYZ and ABC). Here though compilation doesn't show any error/warning, I can't see expected output on console. So I tried to display something just to see if it comes on screen. But strange enough, nothing comes up on the screen.

Can you please have a look at the code below? Am I missing something? (specially to include any required header file)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

class ABC;		//Forward declaration
//-----------------------------------------------------------------------------//

class XYZ
{
	int x;
public:
	void setvalue(int i) {x = i;}
	friend void max(XYZ, ABC);	
};
//-----------------------------------------------------------------------------//

class ABC
{
	int a;
public:
	void setvalue(int i) {a = i;}
	friend void max(XYZ, ABC);	
};
//-----------------------------------------------------------------------------//

void max(XYZ m, ABC n)	//Definition of friend
{
	if(m.x >= n.a)
		std::cout << m.x;	//no output
	else
		std::cout << n.a;	//no output
	std::cout << "Test1";	//for testing only
}
//-----------------------------------------------------------------------------//

int main()
{
	std::cout << "Test2";		//for testing only
	class ABC abc;
	abc.setvalue(10);
	XYZ xyz;
	xyz.setvalue(20);
	max(xyz, abc);
	std::cout << "Test3";		//for testing only
	return 0;
}
So you're not even getting "Test2" and "Test3" displayed in the console?

What OS are you using? What compiler/IDE?

Edit: Have you tried stepping through it with a debugger?
Last edited on
I am using g++ compiler on a GNU/Linux and don't have a debugger... :(
Just using crimson editor.
And yes, I am not getting Test1, Test2 or Test3 on screen
Last edited on
How do you run this program? Afaik, this program will not do anything by itself - it won't start terminal; you have to either use it from terminal, or have some IDE provide you with their terminal for program to work.
A shot in the dark: You are naming the program "test" when you build it. Unix/Linux already have a program named test, so you are running that program and not yours. Try giving the absolute path:
$ ./test

Pro tip: If you aren't seeing "Test2" or "Test3" on console, you need to try this:
1
2
3
4
5
6
7
#include <iostream>

int main( void )
{
  std::cout << "hello world\n";
  return 0;
}


Knowing if that works changes the direction of your problem solving significantly.
Thanks for your inputs MatthewRock & LowestOne

@MatthewRock: I am compiling and running it from bash shell prompt like below
Compile:
-bash-3.00$ g++ Friend_Function_2Classes.cpp -o Friend_Function_2Classes

Run:
-bash-3.00$ ./Friend_Function_2Classes
Test220Test1Test3-bash-3.00$

@LowestOne: It worked. I added void in int main() as suggested by you and it showed output. Also to add, now when I remove void again from int main() and recompile and run it, it still shows output :-)

Can you please explain the reason behind this behavior?
int main() and int main( void ) are both same ;

The latter is use to explicitly define that the function takes no arguments
Last edited on
The former is also used to explicitly define that the function takes no arguments
Linux is kindof funky. I was using std::cout in constructors and it showed the output on Windows, but not on Linux. I don't think it is related, but Linux is a bit weird sometimes.
@patkipramod

You should get into the habit of turning on all the warnings when compiling, use at least the g++ options -Wall -Wextra -pedantic.

To be really thorough, you can upgrade warnings to errors with -Werror.

Even though there are a zillion options, it is worth having a read of the gcc manual page.

There are others handy options to do floating point comparison & narrowing, default cases in switches, Plus other goodies.

Have fun.



Edit:

With int main( void ), it has never been a requirement for C++, Although it was for ANSI C - if my memory serves me correctly from 25 years ago!
Last edited on
Thank you all for your informative and wonderful replies.
@TheIdeasMan: I'll try to explore more gcc options and will make use of manual also. Thanks for the suggestion. :-)

I have started to enjoy c++
Topic archived. No new replies allowed.