Parsing error?

This is a discussion on Parsing error? within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm hoping someone that actually knows c++ can help me with this; I've never programmed in c++ but ...

  1. #1
    emm
    emm is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    3

    Parsing error?

    Hi all, I'm hoping someone that actually knows c++ can help me with this; I've never programmed in c++ but my current task is porting a c++ program from unix to windows (dos command line really) and while I've gotten to where I'm compiling it in dos, I get the error "parse error before 'if'" in one of the header files. The code looks like this:
    Code:
     
    #ifndef __TRACE__
    #define __TRACE__
    
    
    #include <iostream>
    
    
    /* global Variable of Trace-Level */
    extern int TraceLevel;
    
    #ifndef NDEBUG
    #define Trace(Level, Output) \
    	if (Level <= TraceLevel) { //parsing error on this line
        cout << Output << endl << flush; 
      }
    
    #define TraceIf( Condition, Level, Output) \
      if ((Condition) && (Level <= TraceLevel) ) { 
        cout << Output << endl << flush; 
      }
    ...
    and it's being called from this method in another header file:
    Code:
    CPairDict<T1,T2>::~CPairDict() {
      Trace(2, "Enter CPairDict<T1,T2>::~CPairDict()");
      clear();
      Trace(2, "Leave CPairDict<T1,T2>::~CPairDict()");
    }
    As I said, I barely know any c++, so whatever is wrong with this code eludes me (it compiles and runs fine under both unix and cygwin) since I didn't write it to begin with and barely know what it does. Any help would be really appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    I think the problem is that since you are using a macro, you need to have the backslashes to escape the newline sequences.

    The correct version is probably:
    Code:
    #define Trace(Level, Output) \
    	if (Level <= TraceLevel) {\
    		cout << Output << endl << flush;\
    	}
    Frankly, it may be better to use function templates:
    Code:
    template <typename T>
    inline void Trace(int Level, const T& Output) {
    	if (Level <= TraceLevel) {
    		std::cout << Output << std::endl;
    	}
    }
    
    template <typename T>
    inline void TraceIf(bool Condition, int Level, const T& Output) {
    	if (Condition && (Level <= TraceLevel)) { 
    		std::cout << Output << std::endl; 
    	}
    }
    Note that there is no need to use std::flush after std::endl since flush is incorporated into endl.
    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
    emm
    emm is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    The thing is, the program had the back slashes you mentioned in it originally, but with them in, it not only gives the parse error but claims to have stray \'s in the program (which is why I removed them before).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    The thing is, the program had the back slashes you mentioned in it originally, but with them in, it not only gives the parse error but claims to have stray \'s in the program (which is why I removed them before).
    That's strange. Do you still get the same problem with the code example I posted? Anyway, you might as well switch from using macros to using (inline) function templates.
    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

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, there's a couple things you should do then:
    a) Put the backslashes back in, and tell us specifically what errors come up (and where). This way, we can figure out what the *real* problem is, and you'll understand better what's going on.
    b) Try laserlight's solution; inline/templated functions are usually preferred over macros in C++ (many reasons; you can probably find something in the FAQ).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,552
    > the parse error but claims to have stray \'s in the program
    Make sure it's absolutely the last character on the line.
    Otherwise, it will be a stray \
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    emm
    emm is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    I think it was a problem with the macros, when I use the inline templates it seems to stop giving the error on that function (I just get it on the next macro after those), so I'll definitely try converting them all. Thank you both for your help!

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    and it's being called from this method in another header file:
    nitpick warning! The following is me being annoying.

    That is not a method. It's called destructor. Virtual member functions can be called methods. But that's about it as far as the term can be applied to C++ constructs. It's even shun by some authors. Languages like java and visual basic use the term method to describe a function member of the class. C++ doesn't.

    Stroustrup C++ glossary

    As I said, I barely know any c++
    And that is why I'm considering myself annoying
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    That is not a method. It's called destructor. Virtual member functions can be called methods.
    Well, that would make the destructor of a class intended as a base class a method, wouldnt it?
    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

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    LOL. Ok
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    Arrow A solution to a perhaps a different problem...

    I noticed that you appeared to be using
    Code:
    cout << ...;
    without declaring the std namespace
    Code:
    using namespace std;
    or using
    Code:
    std::cout <<...;
    I'm not sure if that's causing the problem, or it could be a different problem altogether.

    Hope this is helpful!

    --The Geek

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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