Thread: Parsing error?

  1. #1
    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
    28,413
    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.
    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
    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
    28,413
    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.
    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

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

  7. #7
    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
    Ireland
    Posts
    8,446
    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
    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
    28,413
    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?
    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

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    LOL. Ok
    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, 05:45 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM