|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Bizzare Errors in C++ code (g++)
Hey, I've got some C++ code that I have to maintain on Linux, it was originally written in Windows, but I have to port it to Linux.
Anyway, I'm attempting to compile it, but I'm getting very strange errors: include/zone.h:33: error: expected ‘;’ before ‘&’ token include/zone.h:33: error: expected `;' before ‘protected’ include/zone.h:33: error: expected ‘;’ before ‘&’ token include/zone.h:33: error: expected `;' before ‘XString’ include/zone.h:33: error: expected ‘;’ before ‘&’ token include/zone.h:33: error: expected `;' before ‘private’ include/zone.h:33: error: ‘XString’ does not name a type include/zone.h:34: error: expected ‘;’ before ‘&’ token include/zone.h:34: error: expected `;' before ‘protected’ include/zone.h:34: error: expected ‘;’ before ‘&’ token include/zone.h:34: error: expected `;' before ‘XString’ include/zone.h:34: error: expected ‘;’ before ‘&’ token include/zone.h:34: error: expected `;' before ‘private’ include/zone.h:34: error: ‘XString’ does not name a type include/zone.h:102: error: ‘XString’ does not name a type include/zone.h:103: error: ‘XString’ does not name a type The corresponding code is (lines 27-37, the one with TimeZoneName is lin 33): class CORE_API XZone #ifdef USE_COMPAT : public XCompatZone #endif { X_PROTECTED(bool, DaylightObserved); // daylightObserved_ X_PROTECTED(XString, TimeZoneName); // seconds west of UTC: timeZoneName_ X_PROTECTED(XString, AltZoneName); // altZoneName_ X_PROTECTED(int, TimeZoneOffset); // timeZoneOffset_ X_PROTECTED(int, AltZoneOffset); // altZoneOffset_ X_PROTECTED_CONST_PTR(XDaylightRule, DaylightRule); // rule_ What is X_PROTECTED, you ask? #define X_PROTECTED(T,A) \ public: \ const T& A() const { return pv_##A; } \ protected: \ T& ref_##A() { return pv_##A; } \ T& A(REF_PARAM) { return pv_##A; } \ private: \ T pv_##A; |
|
#2
|
|||
|
|||
|
Does the line:
Quote:
__________________
Some people have 20 years of experience. Some have 1 year of experience 20 times. My personal site: Basic geek randomness |
|
#3
|
|||
|
|||
|
Quote:
Well, see, that error comes after the weird errors with the semicolon. So I was assuming that even if I fixed the X_STRING error, I'd still have those semicolon errors. Do you think this thought process is wrong? Also, is X_STRING part of a windows library? I can't find much about it online. |
|
#4
|
||||
|
||||
|
Code:
#define X_PROTECTED(T,A) \
public: \
const T& A() const { return pv_##A; } \
protected: \
T& ref_##A() { return pv_##A; } \
T& A(REF_PARAM) { return pv_##A; } \
private: \
T pv_##A;
Given the above, X_PROTECTED(XString, TimeZoneName); expands (roughly) into: Code:
public:
const XString& TimeZoneName() const { return pv_TimeZoneName; }
protected:
XString& ref_TimeZoneName() { return pv_TimeZoneName; }
XString& TimeZoneName(REF_PARAM) { return pv_TimeZoneName; }
private:
XString pv_TimeZoneName;
Suppose I try to compile that Code:
#include <iostream>
using namespace std;
class Foo {
public:
const XString& TimeZoneName() const { return pv_TimeZoneName; }
protected:
XString& ref_TimeZoneName() { return pv_TimeZoneName; }
XString& TimeZoneName(REF_PARAM) { return pv_TimeZoneName; }
private:
XString pv_TimeZoneName;
};
int main () {
Foo f;
return 0;
}
I get Quote:
And if I use a macro: Quote:
Notice the change in line number information. And, since you mention that the line with TimeZoneName is 33 I can only assume that this is your error. |
|
#5
|
|||
|
|||
|
Quote:
I see. What macro are you using? I'm a little confused by that. Anyway, just to clarify, you still think (with supporting examples) that my errors are coming from XString not being defined, and that the real error I'm getting is from another header file (X_PROTECTED is defined elsewhere, in the spirit of full disclosure). I think I've seen that problem before. So I'll try to find where I can define XString or get XString defined or whatever. I may be back later... |
|
#6
|
||||
|
||||
|
Quote:
My comment (and example) about the macro is that the text is expanded into a single line; hence all the line 33 errors in your output (or line 15 in mine). Quote:
|
|
#7
|
||||
|
||||
|
You have just discovered why macros are a bad idea!
|
|
#8
|
|||
|
|||
|
Quote:
Maybe so. However, the reason the code fails to compile is that it declares functions returning reference to XString, but no XString type is declared. Trying to return a reference to an unknown type is the reason the compiler complains about expecting something before an & token. The wording of the error message may not be all that clear, but compilers are only required to diagnose errors - they are not necessarily required to diagnose errors in an intuitive manner. Presumably, there is a header file that declares XString somewhere which needs to be #include'd before using the X_PROTECTED macro.
__________________
Right 98% of the time, and don't care about the other 3%. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Bizzare Errors in C++ code (g++) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|