Cannot include std::thread header in wxWidgets application - c++

I have the following code:
#include "wx\wx.h"
class BClient : public wxApp
{
virtual bool OnInit();
virtual int OnQuit();
};
IMPLEMENT_APP(BClient)
bool BClient::OnInit()
{
return true;
}
int BClient::OnQuit()
{
return 0;
}
As soon I try to add the line
#include <thread>
I receive this errors:
Error 10 error C2347: '__w64' : can not be used with type '__w64
unsigned __int64'
Error 12 error C2143: syntax error : missing ';' before ','
Error 13 error C2059: syntax error : ','
The errors refer to this file: c:\program files (x86)\microsoft visual studio 12.0\vc\include\concrt.h
So, for some reason, wxwidgets and std::thread doesn't mix together.
Can someone explain to me why this is happening and is there a workaround for this issue ?
Thanks.

Something is wrong with your MSVS installation. Here, adding #include <thread> line either before or after #include <wx/wx.h> one works without any problems.
Also, on a completely unrelated note, there is no OnQuit() in the base class, only OnExit().

Related

Mutually dependent classes and inheritance

I'm working on a little C++ WinAPI wrapper. I'd like to create my own window and append my own controls to it.
I created 2 classes : Window and Control.
The problem is that I struggle to deal with the multiple header files, because the class Control needs the class Window and vice versa. When I compile my code, I get many errors..
Here are my files :
globals.h :
// globals.h
// I need this file to define a few constants and to include the main headers needed by my classes
#ifndef GLOBALS_H
#define GLOBALS_H
#include <windows.h>
#include <iostream>
#include <vector>
#include "window.h"
#define SOME_GLOBAL_CONSTANT 1
#endif
window.h :
// window.h
#ifndef WINDOW_H
#define WINDOW_H
#include "globals.h"
#include "control.h"
class Window
{
public:
Window(RECT windowRect);
virtual ~Window();
void appendChild(Control* child);
private:
RECT m_windowRect;
Control m_staticBackground;
std::vector<Control*> m_children;
};
#endif
window.cpp :
// window.cpp
#include "window.h"
Window::Window(RECT windowRect) : m_windowRect(windowRect)
{
std::cout << SOME_GLOBAL_CONSTANT << std::endl;
}
Window::~Window()
{
m_children.clear();
}
void Window::appendChild(Control* child)
{
m_children.push_back(child);
}
control.h :
// control.h
#ifndef CONTROL_H
#define CONTROL_H
#include "globals.h"
class Control
{
public:
Control(Window* parentWindow);
virtual ~Control();
private:
RECT m_controlRect;
Window* m_parentWindow;
};
#endif
control.cpp :
// control.cpp
#include "control.h"
Control::Control(Window* parentWindow) : m_controlRect({}), m_parentWindow(parentWindow)
{
std::cout << SOME_GLOBAL_CONSTANT << std::endl;
}
Control::~Control()
{}
And finally, main.cpp :
#include "globals.h"
class MyCustomControl : public Control
{
public:
MyCustomControl(Window* parentWindow) : Control(parentWindow)
{}
~MyCustomControl()
{}
};
class MyWindow : public Window
{
public:
MyWindow(RECT windowRect) : Window(windowRect)
{
kid1 = new MyCustomControl(this);
appendChild(kid1);
}
~MyWindow()
{
delete kid1;
}
private:
MyCustomControl* kid1;
};
int main()
{
MyWindow appWindow;
std::cout << SOME_GLOBAL_CONSTANT << std::endl;
return 0;
}
Here are all the compilation errors I get :
1>------ Build started: Project: include, Configuration: Debug Win32 ------
1>window.cpp
1>d:\visual studio 2017\projects\include\include\control.h(9): error C2061: syntax error: identifier 'Window'
1>d:\visual studio 2017\projects\include\include\control.h(14): error C2143: syntax error: missing ';' before '*'
1>d:\visual studio 2017\projects\include\include\control.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\visual studio 2017\projects\include\include\control.h(14): error C2238: unexpected token(s) preceding ';'
1>main.cpp
1>d:\visual studio 2017\projects\include\include\control.h(9): error C2061: syntax error: identifier 'Window'
1>d:\visual studio 2017\projects\include\include\control.h(14): error C2143: syntax error: missing ';' before '*'
1>d:\visual studio 2017\projects\include\include\control.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\visual studio 2017\projects\include\include\control.h(14): error C2238: unexpected token(s) preceding ';'
1>d:\visual studio 2017\projects\include\include\main.cpp(8): error C2664: 'Control::Control(const Control &)': cannot convert argument 1 from 'Window *' to 'const Control &'
1>d:\visual studio 2017\projects\include\include\main.cpp(8): note: Reason: cannot convert from 'Window *' to 'const Control'
1>d:\visual studio 2017\projects\include\include\main.cpp(8): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>d:\visual studio 2017\projects\include\include\main.cpp(32): error C2512: 'MyWindow': no appropriate default constructor available
1>d:\visual studio 2017\projects\include\include\main.cpp(13): note: see declaration of 'MyWindow'
1>control.cpp
1>d:\visual studio 2017\projects\include\include\window.h(13): error C2061: syntax error: identifier 'Control'
1>d:\visual studio 2017\projects\include\include\window.h(17): error C3646: 'm_staticBackground': unknown override specifier
1>d:\visual studio 2017\projects\include\include\window.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\visual studio 2017\projects\include\include\window.h(18): error C2065: 'Control': undeclared identifier
1>d:\visual studio 2017\projects\include\include\window.h(18): error C2059: syntax error: '>'
1>d:\visual studio 2017\projects\include\include\window.h(18): error C2976: 'std::vector': too few template arguments
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.25017\include\vector(700): note: see declaration of 'std::vector'
1>Generating Code...
1>Done building project "include.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Any help would be greatly appreciated.
Thank you in advance,
winapiwrapper.
Edit: I have also read this thread, but I couldn't solve my problem.
Welcome to programming. Strike down one error message and two shall take its place.
Well... Not really. What's happening is as you resolve one error, this reveals the errors that the precious errors were hiding.
First a PSA:
Don't write much code without compiling and testing. This way you have a smaller amount of code you need to check when something goes wrong. Start with a main function. Make sure it builds. Add the headers you need. Make sure it builds. Write a function for main to call. Make sure it builds. Repeat until program is finished. Trying to add too much all at once results in cascading storms of errors like you have experienced here.
Global include header files are usually a sucker bet. They often lead you into problems like the circular dependency you have here and make files include everything even when they don't have to. This can slow down build times.
Next, read the following link before continuing: Resolve header include circular dependencies
Now on with the answer:
window.h includes control.h. control .h includes window.h though global.h. This results in the Chicken and egg problem discussed in the above link. One of the headers is going to be included before the other and not be able to find the contents of the other file. Fortunately control.h only needs a reference to Window, not the whole thing, and this can be satisfied with a forward declaration and removing the global include file.
I'm not going to demonstrate cleaning this up. The process is well documented in the link.
This exposes hydra head number 2: Window contains Control m_staticBackground and does not explicitly initialize it. This results in the compiler hunting around for a default constructor for Control, something that does not exist.
Solution: Explicitly initialize m_staticBackground
Window::Window(RECT windowRect) : m_windowRect(windowRect),
m_staticBackground(this)
{
std::cout << SOME_GLOBAL_CONSTANT << std::endl;
}
BIG Mother Freaking note here: m_staticBackground(this) is dodgy as hell. this has not been fully constructed yet, so if you do more than simply store it in Control::Control (which is all you are currently doing) very bad, unpredictable things can happen. Do not use parentWindow or m_parentWindow inside the body of the Control constructor. If possible find a better, safer, way to do this. If not possible, document it with DO NOT USE messages to remind your future self or anyone else looking at the code not to use them.
Once this has been fixed you get to
MyWindow appWindow;
over in main. MyWindow's constructor requires a RECT you don't currently have a RECT to provide, so I'll stop here.
Move "window.h" from globals.h to controls.cpp. And put class Window; before class Controls { in controls.h. This is called forward declaration.
You even do not need window.h in controls.cpp, you can move it directly to your main.cpp.

A dll with a vector of vectors. in one of its classes methods

In my c++ program i'm trying to create a dll that houses the functionality of my a* algorithm.
I encounter a problem when trying to pass the map into it, I first tried to use a 2d array, but that limited my map sizes, so i'm now trying to use a vector in a vector and I keep hitting some odd snag.
In my dlls .h file:
namespace IInterface
{
class IInterface
{
public:
// Sets the map
static __declspec(dllexport) void setMap(int h, int w,vector<vector<byte>> &myarray);
private:
static vector<vector<byte>> mymap;
}
Finaly in the .cpp i have:
#include "IInterface.h"
#include <Windows.h>
#include <stdexcept>
#include <vector>
using namespace std;
namespace IInterface
{
void IInterface::setMap(int h, int w,vector<vector<byte>> &myarray)
{
mymap = myarray;
}
}
Im getting a few errors on compilation even tho the code looks fine to me.
error C2061: syntax error : identifier 'vector' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 7 1 DMAstarDLL
error C2143: syntax error : missing ';' before '<' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
error C2238: unexpected token(s) preceding ';' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
error C2511: 'void IInterface::IInterface::setMap(int,int,std::vector<_Ty> &)' : overloaded member function not found in 'IInterface::IInterface' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.cpp 13 1 DMAstarDLL
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
I looked at some samples, but there was really nothing that matched this scenario. I have a sneaking suspicion i'm forgetting something crucial, but I cant see it. Ideas on getting this to work?
your dlls.h does not include vector type - you should tell the compiler vector definition and include .
Tip: don't use using namespace std; in header file only in cpp. Instead of this use std::vector ...etc.
Secondly, be careful when your dll interface contains stl. This library differs as regards Release and Debug versions, so if you load Release dll in Debug program you could have problems.

Error in official Twain header VS2012 C++

I am trying to program a C++ Wrapper for TWAIN with Visual Studio 2012, Windows 7.
I have installed Twain SDK and have all DLL like Twain_32.dll in C:/Windows
I have twain.h : https://svn.apache.org/repos/asf/incubator/ooo/trunk/main/twain/inc/twain.h
And I have my main :
#include <windows.h>
#include "twain.h"
int main()
{
return 0;
}
And I get 200 errors like for this line
#ifdef _MSWIN_
typedef HANDLE TW_HANDLE;
typedef LPVOID TW_MEMREF;
error C2146: syntax error : missing ';' before identifier 'TW_HANDLE'
How can I resolve that ?

VS2008 C++ "interface" as a parameter name fails to compile

As the title says, I'm getting a compiler error in a VS2008 C++ program. I'm not sure how better to describe my problem than in code. The following compiles unless I uncomment the TEST line.
#include <windows.h>
#include <iostream>
using namespace std;
//#define TEST //<-- uncomment for error
#ifdef TEST
void test(void* interface)
{
return;
}
#endif
int main()
{
cout << "Hello World" << endl;
system("PAUSE");
return(0);
}
When uncommented I get the following errors:
1>main.cpp(7) : error C2332: 'struct' : missing tag name
1>main.cpp(7) : error C2144: syntax error : '<unnamed-tag>' should be preceded by ')'
1>main.cpp(7) : error C2144: syntax error : '<unnamed-tag>' should be preceded by ';'
1>main.cpp(7) : error C2059: syntax error : ')'
1>main.cpp(8) : warning C4094: untagged 'struct' declared no symbols
1>main.cpp(8) : error C2143: syntax error : missing ';' before '{'
1>main.cpp(8) : error C2447: '{' : missing function header (old-style formal list?)
This is unmanaged code, so I'm not sure what the issue with the word interface is. Is there any way to get this code to compile as is, or do I have to change every instance of the term interface to something else?
Thanks!
If your code needs to include Windows.h then you should avoid using the name interface as it's reserved for the use that the Windows SDK has reserved for it (essentially it's a synonym for the keyword struct). There are probably hacks to work around that problem (you could #undef interface after including the SDK headers), but you should probably avoid using that identifier.
The word interface is reserved by MSVC++, as it is a non-standard keyword added by Microsoft Compiler, which is used to define interface in MSVC++.
So use a different name for the parameter, something like this:
#ifdef TEST
void test(void* test_interface)
{
return;
}
#endif

C++ code compiles in CentOS g++, but not in Visual studio 2008

I have a problem: My codes work well in CentOS g++, but when I compile them in visual studio 2008, the visual studio tells me errors like below:
1.c:\program files (x86)\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(19) : error C2144: syntax error : '__w64 unsigned int' should be preceded by '}'
2.error C2143: syntax error : missing '}' before 'namespace'.
My file coding is UTF-8. Should I change them in Unicode all, I change some error file in Unicode,it still has errors like above.
below are some sourecode:
#ifndef ENRC_CODE_DEFS_H
#define ENRC_CODE_DEFS_H
enum ENReturnCode
{
ENRC_SUCCESS = 0,
ENRC_FAILED,
ENRC_NODATA,
ENRC_CONFIG_NOT_AVAILABLE,
ENRC_INVALID_SUBSCRIBE_ID,
ENRC_INVALID_SUBSCRIBE_CONDITION,
ENRC_INVALID_SUBSCRIBER,
ENRC_INVALID_PARAMETER,
ENRC_THREAD_RUNNING,
ENRC_SUBSCRIBE_LIST_EMPTY,
ENRC_OUT_OF_MEMORY // 10
}
e:\my_code\cppcommon\include\errordefs.h(5) : error C2143: syntax error : missing ';' before 'enum [tag]'
next:
#ifndef EN_SMS_SRC_TAO2CPP_H_
#define EN_SMS_SRC_TAO2CPP_H_
#include "SystemMonitorMasterServiceC.h"
#include "SystemMonitorSlaveServiceC.h"
#include "CommonDefs.h"
#include "SystemMonitorServiceDataDefs.h"
namespace EN
{
namespace SMS
{
template < typename _Ty, typename _Cy>
inline
void Tao2Cpp_Enum(_Ty taoValue, _Cy &cppValue)
{
cppValue = (_Cy)taoValue;
}
error C2143: syntax error : missing '}' before 'namespace'
A lot of errors like above.
Thanks. I waste some time to make it easy to read.
You need a semi-colon in the enum declaration:
enum ENReturnCode
{
...
};