Cout with class object

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
#include <iostream>
include namespace std;

class alpha{

private:
  int data;

public:
  alpha(int d){ data = d;}

  void display(){
cout << data;}


};

int main(){

alpha dd(22);

cout << "The value of data is: " << dd.display(); //1. this will not work, why?

cout << "The value of data is: "; dd.display() // 2. but this works, why?

return 0;
}


So, the question is what is precisely wrong with the first "cout" line in the "main"?
I'm learning classes myself, but it look like your just setting data to d, and not returning data's value anywhere?
Aha!

Fixed your code:

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
#include <iostream>
using namespace std;

class alpha{

private:
  int data;

public:
  alpha(int d)
  {data = d;
  }

  int display(){
return data;}


}dd(22);

int main(){


cout << "The value of data is: " << dd.display(); //1. this will not work, why?

cout << "The value of data is: " << dd.display(); // 2. but this works, why?

return 0;
}


Made a few changes, you'll see your mistake :)

(Pats Self On Back)
Last edited on
@Code Assasin:
It's good to know you are just learning "class". Actually there's nothing wrong with my code, in fact you have made it worse by modifying the second "cout" line in the "main".

cout << "The value of data is : << dd.display; " will not work, why?

But this will work-
1
2
cout << "The value of data is: " ;    // you MUST add this semi-colon
 dd.display(); 
Last edited on
LOL. ROFL.
Sorry about jacking your code up man, never meant to. Thanks for giving me the heads up, I really need to work on them!
You have some mistakes:
line 2: include namespace std; should be using namespace std;

line 22: In your code you are trying to pass to cout the return value of member function dd.display();.
What's the return value of that function: void meaning it does not return a thing. So your compiler complains and tries to find out a solution for this. Error messages sometimes are not vary helpful so you must try to see the whole picture.

Possible solutions are:
1) redeclare your member function to return an object cout knows how to treat: int, float or even some object class you have defined BUT also implemented operetor<< for your class. If that sounds too much stick for now with build-in types like int, floats, arrays etc.
2) Just do what you have done: define your member function to pass to cout some value. This will happen whenever it's called (so also on line 22 but there compiler complains for other reason mentioned before). So that's why line 24 works fine.

Finally (not an error by the strict definition of errors) but try to use initialization list instead of initializing your variables inside constructor body:
line 10 should be
1
2
3
  alpha(int d)
:data(d)
{}


instead of
alpha(int d){ data = d;}

It has the same effect but it's better to use the first approach (for reason that will become apparent when not build-in types are used)
@eypros:
Thanks for those insightful points. I suppose the problem is that the class cannot be operated on by the "<<" operator since there is no definition for it (i.e. the operator) in the class.
Topic archived. No new replies allowed.