Boost foreach conflicts with Q_FOREACH (Qt) and moc generation? - c++

I have a program edited in Vs 2008 using some libraries such as Qt and Point Cloud Library (PCL).
PCL has a 3rd party library which contains boost.
However, some errors appeared after compiling:
1>C:\Program Files\PCL
1.5.1\3rdParty\Boost\include\boost/multi_index/sequenced_index.hpp(926)
: error C3083: 'Q_FOREACH': the symbol to the left of a '::' must be a
type 1>C:\Program Files\PCL
1.5.1\3rdParty\Boost\include\boost/multi_index/sequenced_index.hpp(926)
: error C2039: 'tag' : is not a member of 'boost' 1>C:\Program
Files\PCL
1.5.1\3rdParty\Boost\include\boost/multi_index/sequenced_index.hpp(926)
: error C2061: syntax error : identifier 'tag' 1>C:\Program Files\PCL
1.5.1\3rdParty\Boost\include\boost/multi_index/ordered_index.hpp(1399) : error C3083: 'Q_FOREACH': the symbol to the left of a '::' must be a
type 1>C:\Program Files\PCL
1.5.1\3rdParty\Boost\include\boost/multi_index/ordered_index.hpp(1399) : error C2039: 'tag' : is not a member of 'boost' 1>C:\Program
Files\PCL
1.5.1\3rdParty\Boost\include\boost/multi_index/ordered_index.hpp(1399) : error C2061: syntax error : identifier 'tag' 1>C:\Program Files\PCL
1.5.1\3rdParty\Boost\include\boost/multi_index/hashed_index.hpp(1254) : error C3083: 'Q_FOREACH': the symbol to the left of a '::' must be a
type 1>C:\Program Files\PCL
1.5.1\3rdParty\Boost\include\boost/multi_index/hashed_index.hpp(1254) : error C2039: 'tag' : is not a member of 'boost' 1>C:\Program
Files\PCL
1.5.1\3rdParty\Boost\include\boost/multi_index/hashed_index.hpp(1254) : error C2061: syntax error : identifier 'tag' 1>C:\Program Files\PCL
1.5.1\3rdParty\Boost\include\boost/multi_index/random_access_index.hpp(1012)
: error C3083: 'Q_FOREACH': the symbol to the left of a '::' must be a
type 1>C:\Program Files\PCL
1.5.1\3rdParty\Boost\include\boost/multi_index/random_access_index.hpp(1012)
: error C2039: 'tag' : is not a member of 'boost' 1>C:\Program
Files\PCL
1.5.1\3rdParty\Boost\include\boost/multi_index/random_access_index.hpp(1012)
: error C2061: syntax error : identifier 'tag'
For the first problem, the error location in the source file is:
template<typename SuperMeta,typename TagList>
inline boost::mpl::true_* boost_foreach_is_noncopyable(
boost::multi_index::detail::random_access_index<SuperMeta,TagList>*&,
boost::foreach::tag) // <-------------error here for the first compile error.
{
return 0;
}
I think maybe this indicates that the Q_FOREACH conflicts with the boost foreach.
But I do not know how to solve this problem?

The problem is that Qt defines a foreach macro (#define foreach Q_FOREACH) which conflicts with the boost::foreach namespace.
The easiest ways to solve it is to either include Boost before Qt or simply undefine the Qt's macro before including the boost's header file. I prefer the second since it won't need extra documentation (// remember to include Boost before Qt), and it is easier to manage in nested header files and if you use precompiled headers.
#undef foreach
#include <boost/foreach.hpp>
This option is less invasive than disabling Qt's keywords (compilation flag -DQT_NO_KEYWORDS) and can be applied only in the affected files if wanted. It won't affect the use of Q_FOREACH (obviously if you use the Qt's foreach it will fail). It also works independently that Qt is included before or after <boost/foreach.hpp>.

Setting compiler flag -DQT_NO_KEYWORDS, disables the clash between boost and qt!
But then you need to replace the qt keywords in your code like slots, signals, emit ..., see this post.
(I got this message when introducing boost::multi_index container in my project.)
(For qmake projects CONFIG += no_keywords does this.)
(For cmake project add_definitions(-DQT_NO_KEYWORDS) does this.)

Related

Libs ,DLLs and .h files

Im a C# guy thats why I'm lost here, this project claims to have compiled that library for Windows https://soildgeo.codeplex.com/releases/view/108738
the folder comes with Include,Lib64 and Bin64 folders. If I'm not mistaken, I only need to copy .lib files to the VC/lib folder and the folders in include to VC/include folder, so that I can start coding against these APIs, they appear in the #include<> intellisense but then they don't compile. my questions are, are my assumptions on this whole process is right ? if not what are the right steps ? and lastly, do I need to use the Dlls found in Bin64 folder at any point ?
Im using VS2013
Thanks in advance
Edit
1>------ Build started: Project: PhysBAMTest, Configuration: Debug x64 ------
1> main.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(163): error C2146: syntax error : missing ';' before identifier 'attribute'
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(163): error C2530: 'PhysBAM::LOG::anonymous-namespace'::cout' : references must be initialized
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(163): error C2065: 'unused' : undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(163): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(163): error C2143: syntax error : missing ';' before '='
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(164): error C2146: syntax error : missing ';' before identifier '__attribute__'
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(164): error C2530: 'PhysBAM::LOG::anonymous-namespace'::cerr' : references must be initialized
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(164): error C2065: 'unused' : undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(164): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(164): error C2374: 'PhysBAM::LOG::anonymous-namespace'::__attribute__' : redefinition; multiple initialization
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(163) : see declaration of 'PhysBAM::LOG::anonymous-namespace'::attribute'
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(164): error C2143: syntax error : missing ';' before '='
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Edit
after adding the WIN32 macro based on an answer below , I got a linker error this time
Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl PhysBAM::LOG::cout_Helper(void)" (?cout_Helper#LOG#PhysBAM##YAAEAV?$basic_ostream#DU?$char_traits#D#std###std##XZ) referenced in function "void __cdecl PhysBAM::LOG::anonymous namespace'::dynamic initializer for 'cout''(void)" (??__Ecout#?A0x5bbeb782#LOG#PhysBAM##YAXXZ) C:\Users...\Documents\Visual Studio 2013\Projects\PhysBAMTest\PhysBAMTest\main.obj PhysBAMTest
All those attribute, unused, __attribute__ are what some call GNU-ishms, that is, features specific to the GCC compatible compilers (GCC itself, Clang, ICC and others). But MS VC does not aim for GCC compatibility, so they just don't work.
The people that did the port to Windows managed to remove these construct by using a few precompiler tricks, I think in the file Utilities/PHYSBAM_OVERRIDE.h. But for those to work you have to define the macro WIN32. Do that in the project "C/C++ Preprocessor Settings" page, not in the code, so it will be defined for all the source files at once.
Alternatively, and this IMO would be the correct solution, patch the sources, and replace every occurrence of #ifdef WIN32 or #if defined(WIN32) with #ifdef _WIN32. The macro _WIN32 is always predefined in Win32 and Win64, but the WIN32 is not.
Well, technically you'd have to differentiate each occurence if the issue is about being a Windows system or about being a MSVC compiler (there is a GCC compiler for Windows). And then use _WIN32 for the system dependencies and _MSC_VER for the compiler ones.
And then, if you feel like it, send a patch to the Codeplex project.
UPDATE
About the linker error, that should be easy to fix: just add the necessary "*.lib" file or files to the project. You can add them to the Linker Property Pageof the project.
Additionally what you did with .lib and .h, you need to keep .dll in either in, System32 dir, or in same dir where you have exe.
Answers to this question may help you understand better.

Include winnt.h in my project

I am doing a small personal project involving printing out info about a PE file.
I have read that most of the pe structure is already defined in winnt.h header.
Upon including the winnt.h, and compiling the project i get the following error:
#error : "No Target Architecture"
I tried putting a define x86, it might have fixed the architecture problem, but there are even more compilation problems.
For example:
1>c:\program files (x86)\windows kits\8.0\include\um\winnt.h(363): error C2146: syntax error : missing ';' before identifier 'WCHAR'
1>c:\program files (x86)\windows kits\8.0\include\um\winnt.h(363): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\8.0\include\um\winnt.h(367): error C2143: syntax error : missing ';' before '*'
1>c:\program files (x86)\windows kits\8.0\include\um\winnt.h(367): error C2040: 'PWSTR' : 'CONST' differs in levels of indirection from 'WCHAR *'
check Compiler->Enable Microsoft extensions in Project options or use -Ze flag if you're compiling from the command line.

unidentified datatype in external header

I am attempting to write a .dll using a library that comes with some demo projects that run with no issues. When I attempt to #include a header, the compiler complains that certain datatypes are not defined. here is a sample of the errors generated
1>c:\program files (x86)\audiokinetic\wwise v2012.1.4 build 4260\sdk\include\ak\wwise\utilities.h(82) : error C2061: syntax error : identifier 'LPCWSTR'
1>c:\program files (x86)\audiokinetic\wwise v2012.1.4 build 4260\sdk\include\ak\wwise\utilities.h(90) : error C2061: syntax error : identifier 'LPCWSTR'
1>c:\program files (x86)\audiokinetic\wwise v2012.1.4 build 4260\sdk\include\ak\wwise\utilities.h(97) : error C2061: syntax error : identifier 'LPCWSTR'
1>c:\program files (x86)\audiokinetic\wwise v2012.1.4 build 4260\sdk\include\ak\wwise\utilities.h(107) : error C2061: syntax error : identifier 'LPCWSTR'
1>c:\program files (x86)\audiokinetic\wwise v2012.1.4 build 4260\sdk\include\ak\wwise\utilities.h(110) : error C2061: syntax error : identifier 'DWORD'
1>c:\program files (x86)\audiokinetic\wwise v2012.1.4 build 4260\sdk\include\ak\wwise\utilities.h(113) : error C2061: syntax error : identifier 'DWORD'
adding
#include <windows.h>
#include <atlstr.h>
to this external utilities.h file solves the problem, but as its a 3rd party header I don't believe I should be editing it, and considering these other demo projects #include the same header as I am, it seems to suggest that the problem lies elsewhere.
Can anyone think of a reason why this could be happening. The library I'm trying to use is Wwise (a sound engine). Here is a list of .libs im linking
AkSoundEngine.lib
AkMemoryMgr.lib
AkStreamMgr.lib
AkMusicEngine.lib
CommunicationCentral.lib
dxguid.lib
ws2_32.lib
dsound.lib
dinput8.lib
xinput.lib
Typically this is solved by changing the order your headers are included.
Assuming a file called header.h has the following:
LPCWSTR foo;
In you main:
// This is fail.
#include "header.h"
#include <windows.h>
Try:
// Success!
#include <windows.h>
#include "header.h"

MS VisC++ 2010 express: Suddenly Project no longer sees header files

This may be hard to explain. I have been developing a program for about a month now in MS VisC++ 2010 express. I have had no strange problems and was ready to build my project for the 100th time when suddenly, many of the .cpp files seemed to loose contact with the .h files. For instance, in the main function, objects that I had created and used weeks ago, suddenly got the red line under them and the syntax error "theClass is undefined". This applies to iostream, so suddenly cout is undefined. I have no idea what happened. One second to another all external header files and libraries suddenly disappeared in the eyes of the IDE. In the .cpp files, when I declare the class constructor
//initializes a poker game
aPokerGame::aPokerGame(void)
{
stopPlaying = 'n';
}
I get the line under the class name aPokerGame and this error is "must be a class or a namespace name". Isn't this what would be happen if the .h files disappeared? In fact, not all .cpp files are having this problem, only some. Others have really weird errors in them like "no default constructor exists for class"std::basic_ostream<wchar...". All these errors appeared at once.
The header files seem fine. Though one of them has "Error: expected a declaration" under the "Private:" heading for the private members.
Please help!
Here is a sample of the compile errors:
1>------ Build started: Project: firstProj, Configuration: Debug Win32
------ 1> userPlayer.cpp 1>c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\userplayer.h(9):
error C2504: 'Player' : base class undefined 1>c:\program files
(x86)\microsoft visual studio 10.0\vc\include\iostream(10): error
C2059: syntax error : 'namespace' 1>c:\program files (x86)\microsoft
visual studio 10.0\vc\include\iostream(10): error C2334: unexpected
token(s) preceding '{'; skipping apparent function body
1>c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\userplayer.cpp(8):
error C3254: 'Player' : class contains explicit override '{ctor}' but
does not derive from an interface that contains the function
declaration
1>c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\userplayer.cpp(8):
error C2838: '{ctor}' : illegal qualified name in member declaration
1>c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\userplayer.cpp(8):
error C2535: 'Player::Player(void)' : member function already defined
or declared 1>
c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\player.h(18)
: see declaration of 'Player::Player'
1>c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\userplayer.cpp(13):
error C3254: 'Player' : class contains explicit override '{dtor}' but
does not derive from an interface that contains the function
declaration
1>c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\userplayer.cpp(13):
error C2838: '{dtor}' : illegal qualified name in member declaration
1>c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\userplayer.cpp(13):
error C2535: 'Player::~Player(void)' : member function already defined
or declared 1>
c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\player.h(19)
: see declaration of 'Player::~Player'
1>c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\userplayer.cpp(18):
error C3254: 'Player' : class contains explicit override 'userBets'
but does not derive from an interface that contains the function
declaration
1>c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\userplayer.cpp(18):
error C2838: 'userBets' : illegal qualified name in member declaration
1>c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\userplayer.cpp(38):
fatal error C1075: end of file found before the left brace '{' at
'c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\player.h(12)'
was matched 1> pokerRound.cpp
1>c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\npcplayer.h(9):
error C2504: 'Player' : base class undefined
1>c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\userplayer.h(9):
error C2504: 'Player' : base class undefined 1>c:\program files
(x86)\microsoft visual studio 10.0\vc\include\iostream(10): error
C2059: syntax error : 'namespace' 1>c:\program files (x86)\microsoft
visual studio 10.0\vc\include\iostream(10): error C2334: unexpected
token(s) preceding '{'; skipping apparent function body 1>c:\program
files (x86)\microsoft visual studio 10.0\vc\include\time.h(35): error
C2059: syntax error : 'string' 1>c:\program files (x86)\microsoft
visual studio 10.0\vc\include\time.h(35): error C2334: unexpected
token(s) preceding '{'; skipping apparent function body
1>c:\users\bn\dropbox\myprojects\mysoftware\c++\firstproj\firstproj\pokerround.cpp(16):
error C2059: syntax error : 'namespace'
Probably you are missing a semicolon after a class definition or something like that in a header file which you #include into a bunch of other files above some standard header files, which makes everything mess up and the compiler report things as errors that are only errors because you forgot one little thing way up the line.
Double check the syntax of each of your header files meticulously, looking for mismatching parentheses or braces, and making sure you have semicolons where they are needed.

Dependencies, Namespace, and Headers

I believe have dug myself into a hole. While breaking up my single file, couple thousand line chunk of code into multiple files I appear to have grossly screwed up the structure of the program.
I'm new to C++ and its header files and namespace management so I've been learning as I go.
I guess I don't fully understand how #include, using, and namespaces interrelate and what transfers over to other files, etc.
Reading through MSDN documentation I can see bits and pieces of my problem but the solution eludes me.
As of now I have four .cpp files and headers:
1) A Main File
2) A GameData .cpp and its header that is contained in the namespace pData
3) A GameSettings .cpp and its header that is contained in the namespace pSettings
4) A GeneralScreens .cpp and its header that is contained in the namespace pScreens
Upon compile the debugger spews over 100 of error like:
>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(454): error C2447: '{' : missing function header (old-style formal list?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(463): error C2039: 'pair' : is not a member of 'std'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(463): error C2955: 'pScreens::std::pair' : use of class template requires template argument list
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(173) : see declaration of 'pScreens::std::pair'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(33): error C2873: 'wcsrtombs' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(41): error C2039: 'wctob' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(41): error C2873: 'wctob' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2039: 'wmemchr' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2873: 'wmemchr' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2039: 'wmemcmp' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2873: 'wmemcmp' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2039: 'wmemcpy' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2873: 'wmemcpy' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(43): error C2039: 'wmemmove' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(93): error C2039: 'nothrow_t' : is not a member of 'std'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(93): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(93): error C2143: syntax error : missing ',' before '&'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(99): error C2039: 'new_handler' : is not a member of 'std'
Are these errors indicative of something?
I'm assuming something is wrong with the way I'm referencing the std library though I have no idea how to pinpoint the error because all the errors are in library files.
Most likely you're missing a semicolon on the end if a class or struct definition in a header file. The compiler then treats every identifier it sees as an instance of that class instead of what you're expecting. Then the compiler complains because it has no idea what to do with the stray {}s it sees.
Check the header that's #included just before <utility> in your implementation file; that header is most likely where the fault lies.
Side note: To avoid this problem, always #include standard headers before your own headers in a .CPP file. For example, instead of
// MyGameThing.cpp
#include "MyGameThing.hpp"
#include <utility>
#include <string>
// code
do
// MyGameThing.cpp
#include <utility>
#include <string>
#include "MyGameThing.hpp"
// code
That way you won't get compile errors like this buried in the standard headers.
The first compiler error is going to be your best clue:
c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(454): error C2447: '{' : missing function header (old-style formal list?)
I'm guessing that what's happening here is you may have a class or struct declaration with no ending semicolon. So you have something like this:
struct Foo
{
} // <--- no semicolon here will generate funky errors
But overall, pay close & special attention to the first compiler error.
As #Billy mentions in the comments below, the rules for struct and class are the same. In either case, you need the semicolon. Also, old-school C code will often typedef structs like this:
typedef struct tag_Foo
{
} Foo; // <-- still need the semicolon
Here, you still need the semicolon.
If using Visual C++, open the first cpp file and press CTRL+F7, this will compile only the cpp file, check the first error and try to correct this. When fixed go to the next cpp file if the error persists.
Billy's attempt is a decent one, but the usual response to a missing ';' is for the compiler to say so. A missing '}' on the other hand can seriously confuse standard headers included afterward because all their content is within the scope of either the namespace or class that should have been closed. Also, ';' missing is more immediately detectable while a missing '}' isn't necessarily an error so the compiler just pisses a fit at the first thing that doesn't make sense in that scope.
Furthermore, at line 454 in utility for the 2010 compiler is the opening brace for a specialization of tuple_size for std::pair. Post preprocessor that definition would look like so:
template < ... >
struct tuple_size< ::std::pair<_Ty1, _Ty2> >
If you have forgotten a '}' closing a namespace then that's not where pair<> exists anymore. Instead, since it's declared like so:
namespace std { template < ... > struct pair ... }
Whatever scope wasn't closed is now where std is being declared and struct tuple_size< ::std::pair<...> > doesn't make any sense to the parser. Since it's not a valid name it tends to pretend it's not even there and then '{' at the global scope without anything of sense before it is pretty f'n confusing.
BTW, an often unspoken benefit of making minimal examples that cause the problem is that you end up FINDING the problem trying to make it happen.