C++ class function name is changing because of a #define [duplicate] - c++

This question already has answers here:
How does C++ Preprocessors work?
(1 answer)
Why are preprocessor macros evil and what are the alternatives?
(8 answers)
Closed 2 years ago.
I have a class like this:
class MyClass
{
public:
void GetForm() {}
};
Now I want to call GetForm:
MyClass myobj;
myobj.GetForm();
And at compilation I get this error:
error: 'class MyClass' has no member named 'GetFormA'; did you mean 'GetForm'?
What I have found is that in a file (winspool.h), there is a definition:
#define GetForm __MINGW_NAME_AW(GetForm)
And this results in GetFormA I guess for encoding thing.
But, how can it be possible that a #define replace in a name of a function of an object?
How can resolve this?

Related

Initializing static field [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I don't understand why the following code cannot be compiled:
// program.h
class Sensor;
class Program {
private:
static Sensor* sensor;
public:
void SetSensor(Sensor *s) { sensor = s; }
};
I get this compiler error:
cc3No0Or.ltrans0.ltrans.o*: In function Program::SetSensor(Sensor*)
program.h:##: undefined reference to Program sensor
You only have a declaration for the static member, you need also the definition...
Add
Sensor* Sensor::sensor;
in a .cpp file and it will work.

C++ static member of class template -- linker warning "multiple definition" [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Static member initialization in a class template
(3 answers)
Closed 4 years ago.
Let's say for some reason, I want to have a class template MyTemp with some static data member smDummyVar :
Mytemp.h
#ifndef MY_TEMP_H
#define MY_TEMP_H
template<class T>
class MyTemp{
...
private:
static int smDummyVar;
...
};
#include "MyTemp.cpp"
#endif //MY_TEMP_H
Mytemp.cpp
...
template<class T> int MyTemp<T>::smDummyVar = 0;
...
Since the compiler requires that the definition and declaration of a template be at the same place, so I include MyTemp.cpp in MyTemp.h .
Now: I want to use MyTemp at many places and create objects using the template:
case1.cpp
#include "MyTemp.h"
void dummyfunc1(){
MyTemp<int> myTemp1;
}
case2.cpp
#include "MyTemp.h"
void dummyfunc2(){
MyTemp<int> myTemp2;
}
I won't get any error from the compiler, but I'd get warning from the linker:
"multiple definition for MyTemp<int>::smDummyVar" ... defined in invalid_group(case1.o) ... rejected in favour of symbol defined in ...(case2.o)
Question: how can I get rid of this warning ?
Thanks a lot in advance for your help !
====================================
inspired by one of the answers in this thread Why can templates only be implemented in the header file?
I found the following solution:
i. remove #include "MyTemp.cpp" in MyTemp.h
ii. specialized.h
#include "MyTemp.h"
typedef MyTemp<int> MySpecialized;
iii. specialized.cpp
#include "MyTemp.cpp"
template class MyTemp<int>;
iv. give specialized.cpp to cmake file
v. include specilized.h in case1.cpp and case2.cpp
the "multiple definition" warning issued by the linker will go away.
Thank you guys for helping !

Why do I get this compiler error? [duplicate]

This question already has answers here:
error: unknown type name ‘bool’
(4 answers)
Closed 6 years ago.
I want to understand this error: syntax error before 'bool', on the following code:
typedef struct hdate{
date_arc_u date;
unsigned short time;
bool test;
}PACKED_ST horodate_a
When I change bool to another type there is no error.
I already use bool in others parts of the code without error.
I don't understand this error here ....
It's probably because you are writing C code, and the bool type does not exist in C. Your file extension is probably .c, not .cpp, and your code definitely looks like it was written in C.
Probably your C compiler doesn´t know about bool type.
You can try:
1- Including this at first #include <stdbool.h>
2- Declaring at first typedef enum bool { false, true };

Is it possible to hide (or undefine) a typedef? [duplicate]

This question already has answers here:
Undef a typedef in C++?
(3 answers)
Closed 7 years ago.
To avoid confusion and possible errors, is it possible to hide or undefine a typedef.
I do a lot of c++ and java at the same time. In java, the boolean type is boolean, and in c++ it's bool. The problem is that somewhere in the windows c++ libraries, there is a : typedef unsigned char boolean; That means that, in my c++ code, I mistype the bool type as boolean, it will compile and it could cause unexpected error, because it's an unsigned char instead of a true bool.
So what can I do to hide or undefine the boolean typedef in c++?
Yes it's possible with a precompiler directive like the example below:
typedef int foo;
#define foo not_to_be_used
int main() {
foo a = 1; // error: unknown type name 'not_to_be_used'
}
Basically, this way you cancel the typedef. The code above will issue an error if foo is used below the pre-compiler definition.
Live Demo

using static member variable of a class inside a method of this same class [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
if I have a static member variable of a class A declared in the file Af.h
and I want to use this variable inside a method "met" of this same class inside the file Af.cpp, how do I proceed?
here is my files
Af.h
class A
{
public:
static std::vector <int> vec;
void met();
//....
};
Af.cpp
//...
void A::met()
{
// I will use here some int variable i
vec.push_back(i);
//...
}
Unfortunately,this code provides the following compiling error:
undefined reference to A::vec
You need to define it in Af.cpp:
std::vector<int> A::vec;