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 };
Related
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?
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
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this functor class :
#include <string>
using namespace std;
class IsPlayerOfType
{
public:
IsPlayerOfType(const string& type) : type_(type) {}
bool operator()(const Player* player) const
{
return (player->getType() == type_);
}
private:
string type_;
};
The class "Player" represent a player that has several methods and attributes. Among them, there is the method getType() which returns a string.
At some point of my program I have a variable called players_ which is of type vector<Player*>
Finally I have the following code to count the number of players of a certain type in my vector :
int number = count_if(players_.begin(), players_.end(), IsPlayerOfType("Defensive"));
When compiling I get a lot of errors such as :
error C2011: 'IsPlayerOfType' : 'class' type redefinition
error C2440: '' : cannot convert from 'const char [10]' to 'IsPlayerOfType'
error C2780: 'iterator_traits<_Iter>::difference_type std::count_if(_InIt,_InIt,_Pr)' : expects 3 arguments - 2 provided
I don't understand very well how count_if works, I tried to write this code inspiring myself from this answer : https://stackoverflow.com/a/13525420
I don't see where I'm wrong and the compiler errors confuse me.
My psychic debugging skills tell me that you forgot the #define include guards in the header that defines IsPlayerOfType causing the header to be multiply included in some source file. Keep in mind that #include works by the preprocessor doing text substitution which means the preprocessor would need additional logic to even attempt to prevent multiple inclusion.
Also note that using at file scope in a header is quite dangerous and should be avoided.
This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 9 years ago.
I'm trying to migrate some legacy code into a newer project and I don't really get this one fixed. The code compiled and worked well in the older environment.
I have a header file which contains these definitions:
std::string ToString(shared_ptr<const SomeObject> obj);
std::string ToString(SomeObject* obj);
And an implementation file with following lines:
using namespace std;
string ToString(shared_ptr<const SomeObject> obj)
{
// code cut
return outstring.str();
}
string ToString(SomeObject* obj)
{
// code cut
return outstring.str();
}
I'm trying to compile it with clang and I get the following redefinition error:
.../Filename.cxx:15:8: error: redefinition of 'ToString' as different
kind of symbol
string ToString(shared_ptr<const SomeObject> obj)
^
.../Filename.h:15:13: note: previous definition is here
std::string ToString(SomeObject* obj);
Why is it a redefinition as different kind of symbol? How should I fix this? And last but not least, why does it work with older compilers?
Check if string and shared_ptr are declared, and try specifying namespaces for them (replace shared_ptr with boost::shared_ptr or std::shared_ptr) to make sure that the same class is used in declaration and implementation of ToString.
This question already has answers here:
C variable declarations after function heading in definition [duplicate]
(3 answers)
What weird C syntax is this? [duplicate]
(3 answers)
Closed 9 years ago.
I've been given a C/C++ code that looks like this:
extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi, extrafield_local)
zipFile file;
const char* filename;
const zip_fileinfo* zipfi;
const void* extrafield_local;
{
... function body
}
Is declaring the parameters of a function like that possible? I'm getting errors from the compiler (g++).
Thanks in advance.
This is a very old-school C (pre-ANSI C syntax) way for doing things. I suggest you change it, if you own the code, to
extern int ZEXPORT zipOpenNewFileInZip3 (
zipFile file,
const char* filename,
const zip_fileinfo* zipfi,
const void* extrafield_local)
...
There are some more details here and here
That is ancient syntax for defining functions in C. It predates the first standardized version of the C language. More importantly, that syntax has never been valid C++. Since you are compiling this code (which is obviously C code) with a C++ compiler, it is failing.