cout not working with macro?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nakiya
    New Member
    • Apr 2009
    • 3

    #1

    cout not working with macro?

    I wrote a simple macro for printing a message on a line. But I get a strange error by the compiler. Can someone help plz.
    Code:
    #define Notify(comm)\
    	({cout << (comm) << endl;})
    .
    .
    .
    const char* zText;
    zText = something;
    Notify(zText);
    .
    .
    ...... In copy constructor `std::basic_ios <char, std::char_trait s<char> >::basic_ios(co nst std::basic_ios< char, std::char_trait s<char> >&)':
    ...../ios_base.h:738: error: `std::ios_base: :ios_base(const std::ios_base&) ' is private
    ..../main.cpp:186: error: within this context
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    I suggest an experiment: add a new line where you manually expand the macro to see if that line gets the same error.
    Code:
    #define Notify(comm)\ 
        ({cout << (comm) << endl;}) 
    ...
    const char* zText; 
    zText = something; 
    Notify(zText); 
    ({cout << (zText) << endl;});
    ...
    I don't know about C++, but that would be a weird combination of parentheses, braces and semicolon in C.

    I'm not into C++, but my impression is that those who are prefer inline functions to macros.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Define your macro this way:

      Code:
      #define Notify(comm)  cout << (comm) << endl 
      int main()
      	{
      const char* zText; 
      zText = "Hello"; 
      Notify(zText);
      Note there is no comma at the end of the macro. You supply that comma when you use the macro.

      Remember, this is C++ and not C. Therefore, you should not be using macros. Instead, use one of the following:

      1) templates
      2) function overloading
      3) enums
      4) inline functions

      The reason is that the code in the macro is not in your source code. Therefore, the compiler cannot verify the macro is being correctly used not can your debugger debug through a macro since that code is not inthe source file.

      Comment

      • nakiya
        New Member
        • Apr 2009
        • 3

        #4
        Weaknessforcats : Your solution works. But I do not understand this reason u give as to why mine doesn't.

        The reason is that the code in the macro is not in your source code. Therefore, the compiler cannot verify the macro is being correctly used not can your debugger debug through a macro since that code is not inthe source file.
        all the code was in one file.
        and further, compiler should not see the macro because it is expanded by the preprocessor.
        i.e. g++ -E main.cpp
        and it produces error free code as far as I see :(

        Comment

        • RRick
          Recognized Expert Contributor
          • Feb 2007
          • 463

          #5
          Expanded code causes the problem.

          The macro expansion works fine. That's why you don't get any errors with the -E option. -E tells the compiler to stop after the macro expansion.

          The compiler is definitely annoyed about compiling the code from the macro expansion. The following code produces similar compiler errors. The compiler doesn't like the parens '()' outside of the braces '{}'.

          Code:
          #include <iostream>
          using namespace std;
          
          int main ()
          {
          
              const char* zText = "xxxx";
          
              cout << zText << endl;    // Works
              
              ({cout << (zText) << endl;});   //  Uh-ho
          
          }

          Comment

          Working...