NPS
NPS

Reputation: 6355

'identifier' already has a body but identifier not found

I have a Qt project using precompiled headers (not sure if that's relevant) in VS2012 and it compiles and works fine. But when I try to compile the same project in QtCreator it shows errors. First of all - both projects correspond to each other and were previously correctly configured (they compiled and worked just fine). After latest changes to the code, however, something went wrong.

Errors:

pch.h:34: error:C2084: function 'void handleExceptionByShowingMessage(std::exception &)' already has a body
main.cpp:16: error:C3861: 'handleExceptionByShowingMessage': identifier not found

These go on for 30+ lines with another function defined in pch.h (inline too) and a few more cpp files. But all errors are analogical.

From pch.h:

inline void handleExceptionByShowingMessage(std::exception &e)
{
    QMessageBox msgBox;
    msgBox.setText(QString::fromUtf16((ushort*)e.what()));
    msgBox.setStandardButtons(QMessageBox::Discard);
    msgBox.setIcon(QMessageBox::Warning);
    int ret = msgBox.exec();
}

I don't paste function calls from cpp files because it's just a regular use. All cpp files include pch.h correctly (first line of code) and as I said - the exactly same code and file structure works in VS2012 (whose compiler, I believe, QtCreator actually uses...).

If you need more code/information please let me know.

Update: Yes, all headers have #pragma once. Interesting notice though - when I moved these two function definitions to a dummy header file and included it in the pch.h, the project compiled fine.

Upvotes: 0

Views: 648

Answers (2)

slaphappy
slaphappy

Reputation: 6999

#pragma once only prevents the file containing the directive from being included several times. You precompiled header file, pch.h, gets included when the precompiled headers are loaded, and a second time when you files are compiled. The #pragma directive sees them as separate files so it won't work in this context.

The pch.h file (or stdafx.h) is an optimization and ideally should not contain source code but only #includes to other header files. That way, including it several times won't be an issue.

Another solution would be using an include guard (#ifndef PCH #define PCH #endif), which will prevent the file to be included several times.

Upvotes: 2

vlad_tepesch
vlad_tepesch

Reputation: 6883

you may try static inline if the code is in header. I do not know why inline does not imply static but i also stumbled about this a time ago.

Upvotes: 1

Related Questions