Class name that is already a macro - c++

I have the following code :
class MessageBox
{
public:
MessageBox();
};
But the problem is that MessageBox is a os-specific macro in my case and it causes problems.I tried #define MyMacro MessageBox, then #undef MessageBox and re-define it later, but that only works inside the header, and so when i try to create an object of type MessageBox in a source file, it does not work.

If the name of you class matches a macro that you really can't change, then you'll have to change the name of your class.

One possible solution is to create a header which would be added by rest of headers or sources of your project which need it instead of system header and create a thin wrapper function:
#include <system_header.h>
inline void myMessageBox( ... )
{
MessageBox( ... );
}
#undef MessageBox
it is difficult to say how to pass parameters without further details of that macro.

Related

How to add form to existing class?

I have an existing (QCreator build) class derived from QTextEdit
class CCC_MdiChild : public QTextEdit
I like to add GUI form like so
I have no issue creating new form.ui and adding it to the project.
I do not know how to modify existing constructor ( and its definition ) to include "form.ui" .
I am asking for an assistance / help to do that.
Here is my current constructor
Sorry , I did try to format the code but I could not "cut and paste " it as code .
( and I could not reedit it after it was edited once )
Since I did realize my poor formatting any automated comments in that aspect will be ....
The required steps are documented here. For completeness, they are:
Since you've added the .ui file to the project, the UI compiler should generate the corresponding header ui_form.h, which you should #include in the header for your class;
Add a private member to your class of the type defined in this header, which is in namespace Ui and takes its name from your .ui file;
Call the object's setup function as setupUi(this) in the constructor(s) of your class.
Alternatively, you also use multiple inheritance, or avoid including ui headers in header files with the slightly more complicated forward-declared pointer member approach.

Are there any shortcut key to insert a definition of a function?

I use the Jetbrain's IDE Clion.
I want to insert a definition of a function whose forward declaration is already in header file to source file
For example,
I write foo.h file like this.
namespace sample{
class Foo{
void bar();
};
}
When I use the shortcut, I want to insert this to foo.cpp.
sample::Foo:bar(){}
Do you have any good suggestion?
Try this to implement your method :
// Definition of our method
void sample::Foo::bar()
{
// .....
}
Implement required methods :
On the Code menu, click Implement methods (Ctrl+I). Alternatively, you can right-click anywhere in the class file, then click Generate (Alt+Insert), and select Implement methods.

Visual Studio not building correctly

I've been having two different problems when trying to compile a C++ project today.
Sometimes it won't reflect any of my changes in the new build: If I change some wording on the output or change around actual functionality and compile and hit Start Debugging, it will behave exactly as it did before I made the changes. Hitting Clean or Rebuild Solution fixes this, but it takes about a full minute to compile this. I guess it's not detecting any changes, but in the output window I see it list the file names of files I made changes to...
I'm also getting a lot of "...already defined in main.obj" errors (one for every function and variable) whenever I try to use a header file or define a function outside of a class. Renaming the functions lets it compile once, but then the second compile will bring up the errors again. It kinda works if I just define the class in a .cpp file, don't use a header file, and don't use any functions outside of the class.
The project is an open-source program I downloaded the other day to mess with (I'm building a bot to control it). I didn't have the first problem until today, but the second one's always been happening. All I've done is add a file (two if you count both Bot.cpp and Bot.h); Bot.cpp includes Bot.h and some files from the program, and the program's main.cpp includes Bot.cpp.
I'll post some code I guess, but I can't find anything wrong with what I'm doing. I'm wondering if there's something I need to do to the existing files? (there were VS solution files included with the project that I used to open it, since VS Express doesn't help you create projects from existing code.)
//Bot.h
#ifndef _Bot_h_
#define _Bot_h_ 1
#include <string>
class Bot{
private:
uint32 inputs = 0;
bool active = false;
public:
Bot(){};
~Bot(){};
void Start();
void Stop();
void Run();
void Wait(int seconds);
void Press(int buttons);
};
#endif
//Bot.cpp
#ifndef _Bot_cpp_
#define _Bot_cpp_ 1
#include "main.h"
//Some other project files included between these
#include "Bot.h"
using namespace std;
void Bot::Start(){
if (active == false){
active = true;
Run();
}
}
void Bot::Stop(){
active = false;
}
void Bot::Run(){
while (active == true){
printf("Has my code updated?\n");
Wait(2);
}
}
//There are more functions defined here
#endif
All I've really done in the original source code is include Bot.cpp at the bottom of the list of includes in the main.cpp file, and then add a call to create the class. I'm a little out of practice with C/C++ so maybe it's something simple. I'm also bad at writing posts like this so if anything needs clarified I'll try to help.

Why Qt default project use separate header files for manwindow.cpp?

I just created a Qt default project with a Qt designer form.
The class MainWindow is declared in a mainwindow.h and then included in mainwindows.cpp.
Why is it done this way ? Why not a declaration of this form directly in mainwindows.cpp ?:
class MainWindow
{
...
}
What is the proper way to add my code ? For example, a button that trigger a method.
In C++ you typically put class definitions into header files (.h), and method implementations in source files (.cpp). That allows clients of the class to use the class without having to see the implementation of each function. That also means that when adding a method, you'll typically have to make two changes: add the method to the class definition (in the header) and then add the method's implementation to the .CPP file.
In header file:
class MainWindow
{
void SomeMethod();
};
In source file:
void MainWindow::SomeMethod()
{
// Your code here.
}
The definition of MainWindow class is needed in another file, where an instance of it is constructed in the main function and then shown. That's why the class needs to be defined in a header file.
There are a number of ways to add your own code: for the button you described you could create in entirely in the QtCreator UI, or you could create it "programmatically" in the MainWindow constructor.

Defining a preprocessor directive for windows 7

I am making use of SetDllDirectory() and wants to know how can i define a directive for windows 7 HP 64 bit i am thinking something like this :
In preprocessor directives :
Add WIN7
In the .cpp i was thinking to add something like
#ifndef WIN7<- where the function is used
SetDllDirectory();
#endif
but as soon as i add the statements the SetDllDirectory becomes commented.
And this is something i tried with , i added WIN7 in the preprocessor definations and added :
#if defined(__WIN7__)
if (regkeyExists) {
if (regkey->HasValue("LibPath")) {
regkey->QueryValue("LibPath", value);
if (!value.empty()) {
wxSetEnv("ABCLib", value);
SetDllDirectory(value.c_str());
}
}
}
SetDllDirectory("C:\\Program Files\\ABC\\ABCProject\\lib");
#endif
Is this the right to declare windows 7
Thanks
You don't want to define your own macro to detect Windows 7, use the ones provided by Microsoft - wrap your Win7 only code in:
#ifdef _WIN32_WINNT_WIN7
xyz()
#endif
If you do create your own, you must define it when you want to compile for Windows 7, it seems that you're hiding the SelDllDirectory() function - #ifndef means "if not defined" so if WIN7 macro is not defined anywhere, then you get that function.
How about NTDDI_VERSION or _WIN32_WINNT?