Thread: '<<' operator

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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;

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 10:21 AM
  3. Replies: 3
    Last Post: 12-09-2008, 11:19 AM
  4. Replies: 2
    Last Post: 07-07-2008, 03:46 AM
  5. Replies: 1
    Last Post: 07-07-2008, 03:38 AM