Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

printing Struct

printing

  • Please log in to reply
8 replies to this topic

#1 freiza

freiza

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 22 February 2012 - 11:21 AM

struct max
{
   int nk;
};

void main()
{

cout<<max;  // this gives error.

}

cout<<max; //gives error.
Why it is not printing address of nk;
  • 0

#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 22 February 2012 - 12:08 PM

You didn't declare any instances of max in your main
  • 0

#3 freiza

freiza

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 22 February 2012 - 12:10 PM

oh sorry. I meant

struct max
{
int nk;
};

void main()
{
max maxil;

cout<<maxil; // this gives error.

}
  • 0

#4 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 22 February 2012 - 12:13 PM

If you want the address of the variable use &maxil
  • 0

#5 freiza

freiza

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 22 February 2012 - 12:17 PM

No I just want to know why this statement is giving error
  • 0

#6 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 22 February 2012 - 12:46 PM

It gives you the error because there is no implementation of << in the struct. If you add that implementation, it will work fine.
  • 0

#7 freiza

freiza

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 22 February 2012 - 01:28 PM

int array[10];

cout<< array;
cout<< &array;

Both of them works. Then Why maxil is not working?
I thought maxil is also a pointer to the memory region nk.
  • 0

#8 DRK

DRK

    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 23 February 2012 - 02:01 AM

I thought maxil is also a pointer to the memory region nk.

It is not.

Go with lespauled advice and implement operator<< handling your structure.
  • 0

#9 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 14 May 2012 - 08:15 PM

Well, to understand why 'cout<<maxil;' is not working, you first need to understand first what that line means or what that line do. For that you need to understand what is 'operator overloading' -- http://en.wikibooks....tor_Overloading. For simplicity, you can just consider '<<' symbol as a method name (where the dot is missing) and 'maxil' as a argument to that method. 'cout' is an instance (considering that you know what instance is) of type std::ostream which is typedefed with 'basic_ostream<char, char_traits<char> >'. The '<<' operator is overloaded in that class (you know, a class is created when we supply argument to templated class -- that why I'm not going to the template of this class). So your custom type 'Max' must be argument to one of the overloaded versions of '<<' operator. Now how do you expect 'basic_ostream<char, char_traits<char> >' class to write an overloaded '<<' operator with argument your custom type 'Max'?

That's why you need to customize the operator overloading for operator '<<'. The following code does that.


typedef struct Max
{
   int nk; 
};


// Overload << operator for our custom type 'Max'
std::basic_ostream<char> & operator << ( std::basic_ostream< char >& out, const Max& m )
{ 
return out<< m.nk; 
}


void main()
{
Max maxil;
maxil.nk = 9;
cout << maxil;
}

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download