Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio: C++ newline constant error

Using Visual Studio on a Qt project (with the Qt addon), seems to always throw up an error message:

error C2001: newline in constant

From the following line:

this->setApplicationVersion(QString(BUILD_VERSION));

or whenever I use a compiler constant defined in my QMake file. BUILD_VERSION is defined in my QMake build recipe, compiling the project works successfully if I use a different IDE (even when I use the same MSVC compiler, everything works).

I imagine I'm either missing a preference in the Qt addon or inside Visual Studio, or I need to redefine my constant's outside of my QMake files etc...

The constant is defined and picked up by Intellisense as:

#define BUILD_VERSION \"0.1.0\"

Found the issue is down to escaping the quotes in the constant, in Visual Studio this won't work but it will work with Mingw, and the MSVC compiler on the command line. Defining constants without the escaping works with Visual Studio:

#define BUILD_VERSION "0.1.0"

The problem is to define the constants in the QMake file, I need to escape like:

#define BUILD_VERSION \\\"0.1.0\\\"

Is there a way to define them in the QMake file and keep them working with Visual Studio?

like image 261
sixones Avatar asked Aug 12 '12 13:08

sixones


1 Answers

consider writing

this->setApplicationVersion( QString( TO_STRING( BUILD_VERSION ) ) );

where TO_STRING converts a something to string:

#include <iostream>
using namespace std;

#define TO_STRING_AUX( x )  #x
#define TO_STRING( x )      TO_STRING_AUX( x )

#define BUILD_VERSION 0.1.0

int main()
{
    cout << TO_STRING( BUILD_VERSION ) << endl;
}

i don't have QMake but I gather that the problem is that QMake does a TO_STRING-like operation, and then also doing it yourself, as I noted first, is a general solution.


EDIT: @sixones asks in a follow up comment,

here's a warning thrown from the use of TO_STRING; warning C4003: not enough actual parameters for macro 'TO_STRING_AUX' any ideas how to solve?

The problem is apparently that the macro passed as argument is defined as nothing (not undefined, but defined as nothing).

A solution is to use a C99/C++11 variadic macro definition, as follows:

#define TO_STRING_AUX( ... )    "" #__VA_ARGS__
#define TO_STRING( x )          TO_STRING_AUX( x )
like image 133
Cheers and hth. - Alf Avatar answered Oct 26 '22 15:10

Cheers and hth. - Alf