Thread: ostream problem...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    39

    Question ostream problem...

    my code requirement is that a string between two macros is to be displayed at the console. Like this:
    Code:
    TRACE << "hello world" << ENDTRACE;
    Question is: how should the two MACROS be defined.

    I am thinking of TRACE expanding to a static ostream object whose inserter can be overloaded to display the string at the console. What do you think?

    Any ideas would be helpful.

    TIA
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <iostream.h>
    
    #define TRACE cout
    #define ENDTRACE endl
    
    int main(void)
    {
        TRACE << "hello world" << ENDTRACE;
    }
    It's a bit poor really, but it works.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    39

    Talking

    well, for the given problem that's probably the simplest and best solution.

    But I didn't specify the problem in detail The reason I said console was because that was what I was trying to do in a test program.

    Ok, so the problem is: I need to invoke a method passing on the string between the two MARCOS. Something like:
    Code:
    TRACE << "The number is: " << some_method_returning_int() << ENDTRACE;
    if some_method_returning_int() returned: 5, the effect of macro expansion should be:
    Code:
    someObj::trace_to_correct_channel( "The number is: 5" );
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    A solution to your problem would be:
    Code:
    #define TRACE {stringstream __a;__a
    #define ENDTRACE "";someObj::trace_to_correct_channel(__a.str().c_str());}
    I do not recommend use of macros like this, though.

    (Edit) Don't forget to #include <sstream>
    Last edited by Sang-drax; 10-18-2002 at 10:56 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM