yan bellavance
yan bellavance

Reputation: 4830

C++ preprocessor directive

I have a macro which defines the model number of an equipment. I am having problems determining how to compare it to a string.

In a customer's specific header I have defined my macro as follows:

#define FTP_MODEL_NUM CT1030

Here I want to conditionally compile a section of code depending on the model number but no matter what value my macro has it compiles it anyway:

#if FTP_MODEL_NUM == CT1031  
    QMessageBox * lolers=new QMessageBox;  
    lolers->setWindowTitle(tr("title"));  
    lolers->setText(tr("this is test"));  
    lolers->show();  
#endif  

What am I missing? Do I absolutely need to compare it to another macro when using the == operator? I'm using Qt on Linux.

Upvotes: 0

Views: 805

Answers (2)

jwodder
jwodder

Reputation: 57470

Assuming C++'s preprocessor works the same way as C99's, what you're trying to do can't work. After FTP_MODEL_NUM == CT1031 is expanded to, e.g., CT1030 == CT1031, any identifiers remaining in the expression are replaced with 0, yielding 0 == 0, which is always true. I believe the standard way to do what you're trying to do is to define a macro with the same name as the model number (e.g., #define CT1030) and then implement the test with #ifdef CT1031.

Upvotes: 4

RedX
RedX

Reputation: 15175

You cant lexically compare macros afaik. Only numerically. Define a second macro with numbers and use that.

Upvotes: 2

Related Questions