Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By whiteflags

'<<' operator

This is a discussion on '<<' operator within the C++ Programming forums, part of the General Programming Boards category; If someone can help me I appreciate it. The following code give me an error. I could not figure it ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    7

    '<<' operator

    If someone can help me I appreciate it. The following code give me an error. I could not figure it out
    Code:
    #include <iostream>
    using namespace std;
    
    void A_print(){
        cout << "A_print" << endl;
    }
    
    int B_print(int x){
        int y = x/2;
        
        return y;
    }
    int main(){
        
        A_print();
        cout << "2nd  " << A_print() << endl; // ?? right this line if I change A_print() to B_print(6) it is ok 
    
        cout << endl;
        system("pause");
        return 0;
    }
    Thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    A_print's return type is void, so you cannot print its return value as it has none.

    One solution is to call A_print separately.
    iMalc likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Yeah it's a good idea to post the error:
    Code:
    In function 'int main()':
    Line 16: error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"2nd  ")) << A_print()'
    compilation terminated due to -Wfatal-errors.
    If we ignore that you called a function for a moment we can boil your code down to this
    Code:
    cout << "2nd " << cout << "A_print" << endl << endl;
    cout can't exactly print itself; and you don't return a value from A_print() to display anyway. There is a way to make this syntactically correct. Break it up.
    Code:
    cout << "2nd ";
    A_print();
    cout << endl;
    Khabz likes this.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    Thank you guys,

    Actually I figure it out this:

    Functions like int, float, functions does return something, has to be called with "cout".
    Functions like void, does not return something has to be called without "cout".

    Am I right?

  5. #5
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    You're right.

    If the function returns a value you're free to do whatever you please with it, as if it was a variable.
    If it's void you mustn't include cout before you call it. It does it itself.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    Thanks, that help a lot and make sense for me now.

    Appreciate your time guys.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Quote Originally Posted by valentin View Post
    Thank you guys,

    Actually I figure it out this:

    Functions like int, float, functions does return something, has to be called with "cout".
    Functions like void, does not return something has to be called without "cout".

    Am I right?
    Well, to be more precise, you can include a call to a function that returns something other than void in a cout statement. It doesn't mean you have to.
    You can't, however, include a function call to a function returning void in a cout statement. The reason is that you are essentially telling the compiler to pass whatever the function is returning to cout, but if it returns nothing, how can it do that?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ternary Operator to choose which operator to use
    By cncool in forum C Programming
    Replies: 7
    Last Post: 06-27-2011, 01:35 PM
  2. [ ] operator requires + operator?
    By m37h0d in forum C++ Programming
    Replies: 10
    Last Post: 02-04-2009, 09:21 AM
  3. Replies: 3
    Last Post: 12-09-2008, 10:19 AM
  4. Replies: 2
    Last Post: 07-07-2008, 03:46 AM
  5. Replies: 1
    Last Post: 07-07-2008, 03:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21