Thread: Errors in preprocessing.

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    220

    Errors in preprocessing.

    I'm at the point where I want to look a bit more into conditional compilation, and i'm at a bit of a wall here. For some reason Dev C++ doesn't like this:
    Code:
    #define GETKEYPRESS getch();
    #ifdef (_MSC_VER) \
      #include <iostream.h>
    #elif \
      #include <iostream>
    #endif
    #include <conio.h>
    Does anyone know the solution to make this work, whats wrong with my code and what/why does yours work if you post it?

    Thanks in advanced.


    Note:If you can't see, i'm trying to make it compile so that if it's a microsoft compiler it will include <iostream.h> and if it's not it will include the standardized <iostream>.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >>Note:If you can't see, i'm trying to make it compile so that if it's a microsoft compiler it will include <iostream.h> and if it's not it will include the standardized <iostream>.
    Why would you want to do that?

    Code:
    #ifdef _MSC_VER //or #if defined(_MSC_VER)
      //something
    #else
      //something
    #endif
    gg

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    So that my friends compiler doesn't freak out on him.

    His compiler freaks out if he goes '#include <iostream>' and not '#include <iostream.h>', and his compiler is an MS compiler.

    Edit:Sry, forgot to test out the code before I posted this..lol. But thanks It works, I appreciate it.
    Last edited by Tronic; 03-21-2004 at 02:04 PM.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by Tronic
    So that my friends compiler doesn't freak out on him.

    His compiler freaks out if he goes '#include <iostream>' and not '#include <iostream.h>', and his compiler is an MS compiler.

    Edit:Sry, forgot to test out the code before I posted this..lol. But thanks It works, I appreciate it.
    Maybe he can try

    #include <iostream>

    and then use

    using namespace std;

    This works with all compilers that I have tried (including Microsoft VC++, Borland bcc, GNU g++ (Windows), gnu g++ (Linux)).

    Dave

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    nono, it wasn't cout that wasn't functioning. It was that his compiler(which was VC++ if I remember right), couldn't find <iostream>, it could find, however, <iostream.h>.

    I have one more question....if you guys can help me out on this one again? Thanks again btw =)

    This question is regaurding #define functions with the stringized operator.

    For some reason my compiler is choking on this...and I can't find out whats wrong with it, everything seems to be fine but it's giving me:
    parse error before #.

    And here are the statements it concerns:
    Code:
    #define PRN std::cout << #o << '\n'
    And i'm using the macro here:
    Code:
    PRN(Welcome to the Calculator, enter in expression and hit enter:);
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #define PRN(str) std::cout << str << '\n'
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >#define PRN(str) std::cout << str << '\n'
    And if I choose to say something like this?
    Code:
    PRN ( ( someflag ) ? var1 : var2 );
    It would be better to surround all of your macro arguments in parentheses to protect against such things:
    Code:
    #define PRN(str) std::cout << (str) << '\n'
    Or you could avoid nasty macros to begin with. They have a tendency to be error prone.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by Tronic
    nono, it wasn't cout that wasn't functioning. It was that his compiler(which was VC++ if I remember right), couldn't find <iostream>, it could find, however, <iostream.h>.
    If you have a C++ compiler, Microsoft or otherwise, that can't find <iostream>, then either it is defective, its installation is defective, or the way it is being invoked is incorrect.

    (I use Microsoft VC++ version 6.0, among others.)

    Dave

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >then either it is defective, its installation is defective, or the way it is being invoked is incorrect
    Or it could just be a very old compiler. But I would bet on a namespace issue myself.
    My best code is written with the delete key.

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >And if I choose to say something like this?
    Bah. Who needs conditionals?

    >#define PRN(str) std::cout << (str) << '\n'
    It would probably also be good to use std::endl instead of '\n'.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by Prelude
    >then either it is defective, its installation is defective, or the way it is being invoked is incorrect
    Or it could just be a very old compiler. But I would bet on a namespace issue myself.
    My definition of "defective" includes "can't find or doesn't recognize <iostream>."

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Thanks for the help Xsquared and Prelude Preciate it.

    But I have some more questions..lol.

    Error:
    MACRO "PRN" passed 2 arguments but takes just 1.



    I'm passing the same argument as before into PRN() by the way.

    Not only that, but what would:
    'PRN ( ( someflag ) ? var1 : var2 );' do? And what is meant by flag?

    Also, is there an advantage on endl; over '\n'?
    If so what is it?

    EDIT:Heeey don't worry about the header issue Life is good, theres too much good in the world that can be done, why argue over pointless issues? It was the method of conditional compilation that I was trying to get down, rather then fixing his issue in this case
    Last edited by Tronic; 03-21-2004 at 03:16 PM.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >MACRO "PRN" passed 2 arguments but takes just 1.
    I assume you mean this:
    Code:
    PRN(Welcome to the Calculator, enter in expression and hit enter:);
    Your spaces are leaking out, programmers call this a whitespace leak. Double quotes keep the spaces neatly packed in so that it doesn't happen:
    Code:
    PRN("Welcome to the Calculator, enter in expression and hit enter: ");
    >Not only that, but what would: 'PRN ( ( someflag ) ? var1 : var2 );' do? And what is meant by flag?
    If the macro were defined correctly, it would print var1 if someflag were true and var2 otherwise. If the macro were not defined correctly you would get a compiler error.

    >Also, is there an advantage on endl; over '\n'?
    Yes, endl also flushes the buffer.
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Thanks again

    But also....for some reason endl; makes my compiler freak out. Does endl lie in some sort of a header?

    Also, in this case what do you mean by buffer? And are there other ways to flush this buffer?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But also....for some reason endl; makes my compiler freak out.
    std:: likes to hide from me too. I don't ever forget to put him there, he just runs off when I'm trying to figure out where one of his buddies ran off to elsewhere in the code. Damn namespaces...

    >Also, in this case what do you mean by buffer?
    Most systems will allocate a big array to hold characters. They'll fill that array up and then write the contents to the device or file you want because it's more efficient than writing a single character the instant they get it. It's like caching, but the same.

    >And are there other ways to flush this buffer?
    std::flush. It works like endl, but doesn't print a newline. Or you could just wait for the buffer to get filled up or the program to terminate.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Winsock compilation errors
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-03-2005, 08:00 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM