I cannot understand this code [duplicate] - c++

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.

Related

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

redefinition of 'WhatEver' as different kind of symbol [duplicate]

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.

how to use a static char [duplicate]

This question already has answers here:
Linker error when using static members
(2 answers)
Closed 10 years ago.
I am currently working with a wxWidgets project where I have to copy a wxString to a static c string that can hold the value for the life time of the program. Essentially my headerfile and source file look like this:
*****************PortDialog.h*****************
...
static char *portName;
-----------------------end
and the source file is;
***************PortDialog.cpp*****************
.
.
.
wxString str = "COM1";
strcpy(portName, (const char*)str.mbc_str());
---------------------end
However I run into the following linking error.
error LNK2001: unresolved external symbol "public: static char * portDialog::eportName" (?portName#portDialog##2PADA)
Can somebody explain to me what is the mistake I am making here? Is it correct to use static char * for the said purpose?
static variables declared in header should be initialized in cpp file see http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr038.htm
Besides the explanations you have received regarding the linker error, you should also be aware that you cannot call strcpy with the destination as a char * which has not been allocated memory to hold the source string. It would compile (and link) but could do just about anything during run-time.
static Variables should be declared in the cpp file
char* PortDialog::PortName = NULL;
Like u do to access methods(functions) of a class using Scope Resolution Operator
void PortDialog::SomeFunction()
{
//Code Goes here
}

C++ dll function using std:string [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
std::string in C#?
How can I call a function of a C++ DLL that accepts a parameter of type stringstream from C#?
Is there a way to convert from a c++ std:string to C# System.String? I'm calling a function from a C++ dll that takes a std:string as input. Is there a simple way to do this?
C#:
[DllImport(#"MyDLL.dll")]
[return:MarshalAs(UnmanagedType.I1)]
public static extern bool myFunction([In, Out]string input);
C++:
extern "C" __declspec(dllexport) BOOL __stdcall myFunction(const std::string& input)
{
//Code is here
}