main.c
#include "stackg.h"
int main()
{
return 0;
}
stackg.h
#ifndef STACKG_H
#define STACKG_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct stack_gt* stack_gt;
stack_gt stkg_init(
void* (*alloc)(const void* data, const int size),
void (*dealloc)(void* data),
void (*copy)(void* data_d, const void* data_s),
const int size
);
void stkg_free(stack_gt s);
int stkg_is_empty(stack_gt s);
int stkg_is_full(stack_gt s);
const int stkg_size(const stack_gt s);
void stkg_clear(stack_gt s);
int stkg_push(stack_gt s, const void* data);
int stkg_pop(stack_gt s, void* data);
int stkg_peek(stack_gt s, void* data);
#ifdef __cplusplus
}
#endif
#endif
The above program compiles successfully with the GCC compiler, but in MSVC2008 it gives the following error :
error C2040: 'stack_gt *' differs in levels of indirection from 'stack_gt'
What should I tell MSVC to make it compile the program without changing anything in the code?
Edit
Error occurs at line 8 of stackg.h :: typedef struct stack_gt* stack_gt;
Edit 2
If nothing else, I'll go with typedef struct _stack_gt* stack_gt;
The problem is that here:
typedef struct stack_gt* stack_gt;
you are giving stack_gt a different type, while this works fine:
typedef struct stack_gt* stack_gtB;
clang gives us a nicer error message:
error: typedef redefinition with different types ('struct stack_gt *' vs 'stack_gt')
This is covered in the draft C++ standard section 7.1.3 The typedef specifier paragraph 6:
In a given scope, a typedef specifier shall not be used to redefine the name of any type declared in that scope to refer to a different type. [ Example:
class complex { / ... / };
typedef int complex; // error: redefinition
—end example ]
Using the same name though is fine, so this would be ok:
typedef struct stack_gt stack_gt;
covered in paragraph 3:
In a given non-class scope, a typedef specifier can be used to redefine the name of any type declared in that scope to refer to the type to which it already refers. [ Example:
typedef struct s { / ... / } s;
typedef int I;
typedef int I;
typedef I I;
—end example ]
Another idea:
#ifdef __cplusplus
extern "C" {
typedef void * stack_gt
#else
typedef struct stack_gt* stack_gt;
#endif
This is ugly, but you don't need to rewrite any other part of the code, only this header included in C++ . It is only used as an opaque pointer in C++ anyways, and C doesn't notice.
Related
I'm need to declare incomplete type (may it be siginfo_t) in header, and in source import some system header (sys/signal.h), that defines that type.
So, here is the problem:
// a.h
struct siginfo_t;
void foo(siginfo_t*);
// a.cpp
#include "a.h"
#include <signal.h>
void foo(siginfo_t* x) { /* implementation */ };
But in signal.h it is defined like:
typedef struct siginfo_t { /* */ } siginfo_t;
Compiler error: error: typedef redefinition with different types ('struct siginfo_t' vs 'siginfo_t').
How to achieve no errors there? Thanks!
In the header <sys/siginfo.h> the typedef name siginfo_t is an alias for an unnamed structure
typedef struct {
int si_signo;
int si_code;
union sigval si_value;
int si_errno;
pid_t si_pid;
uid_t si_uid;
void *si_addr;
int si_status;
int si_band;
} siginfo_t;
But you introduced the same alias name for named structure
typedef struct siginfo_t { /* */ } siginfo_t;
So the compiler issues the error
error: typedef redefinition with different types ('struct siginfo_t'
vs 'siginfo_t')
because you may not introduce the same alias name for a named structure and an unnamed structure. These structures are different types.
So, there is the right approach:
// a.h
#pragma once
extern "C" {
struct siginfo;
}
void foo(siginfo*);
// a.cpp
#include "a.h"
#include <sys/siginfo.h>
void foo(siginfo* x) { /* */ };
I am trying to execute following code using g++ and getting incomplete type error
#include <stdio.h>
struct try_main{
union{
struct try_inner_one{
int fl;
float g;
}one;
struct try_inner_two{
char a;
}two;
}un;
int chk;
};
void func(struct try_inner_one o){
printf("%d\n",o.fl);
}
int main(){
struct try_main z = {{1,2},3};
func(z.un.one);
return 0;
}
Error:
union.c: In function ‘void func(try_inner_one)’:
union.c:15:6: error: ‘o’ has incomplete type
void func(struct try_inner_one o){
^
union.c:15:18: error: forward declaration of ‘struct try_inner_one’
void func(struct try_inner_one o){
^
union.c: In function ‘int main()’:
union.c:20:16: error: parameter 1 of ‘void func(try_inner_one)’ has incomplete type ‘try_inner_one’
func(z.un.one);
Above code is successfully getting compiled with gcc
What is the reason for this error and how to fix this
Thanks
C and C++ have different scoping rules. The full name of the type in C++ isn’t struct try_inner_one, since the type definition is nested inside the unnamed union inside try_main.1
If you want to write code that works equally in both C and C++, pull the type definition to the top level:
struct try_inner_one {
int fl;
float g;
};
struct try_inner_two {
char a;
};
struct try_main {
union {
struct try_inner_one one;
struct try_inner_two two;
} un;
int chk;
};
1 The fully qualified name of this type can’t be spelled in C++ since the type it’s nested inside is unnamed. You could give a name to the union type, that would allow you to spell the fully qualified name of try_inner_one in C++. However, that name wouldn’t be legal C code, since C doesn’t have a scope resolution operator.
If you want to keep the nested type definition you could give the union a name (in the following, union_name) and do the following to keep the code compiling for both C and C++:
// (Type definition omitted.)
#ifdef __cplusplus
using try_inner_one = try_main::union_name::try_inner_one;
#else
typedef struct try_inner_one try_inner_one;
#endif
void func(try_inner_one o){
printf("%d\n", o.fl);
}
What is the reason for this error
The reason is that try_inner_one nested within the union nested within try_main cannot be found by unqualified name lookup in the context outside of that union in C++ (unlike in C).
how to fix this
You can use a qualified name in C++:
void func(decltype(try_main::un)::try_inner_one o){
You can simplify if you give a name for the union:
union u { // note the name
struct try_inner_one{
void func(try_main::u::try_inner_one o){
A cross-language compatible solution is to define the struct outside of each other as described in Kondrad Rudolph's answer.
A word of warning: C++ is more restrictive than C on how inactive members of union can be accessed.
It seems you are compiling your program as a C++ program. In this case each declaration within the structure try_main has the scope of this structure.
So you need to declare the function like
void func( decltype( try_main::un )::try_inner_one o );
or
void func( const decltype( try_main::un )::try_inner_one &o );
I'm trying to understand function declaration using typedefs.
What does this code do in C++?
typedef void fcn_t(void);
typedef void (*ptr_t)(void);
fcn_t f;
fcn_t *pf;
ptr_t pf2;
In my understanding:
fcn_t is the type of a function, and so the line with f is a function declaration (not a definition), and I could later define it like void f(void) { blabla(); bleble(); } just as if I had declared void f(void); instead of fcn_t f;;
fcn_t * is the type of a function pointer, and the line with pf is a pointer variable definition, and pf is default-initialized (assuming the code excerpt is from the global scope);
There is no difference between fcn_t* and ptr_t, thus everything I said about pf applies to pf2.
Did I get it right? Would any of the three declarations have its meaning changed if I marked them extern? What would change if the code was compiled as C instead of as C++?
Yes you are right on all three counts. The only thing that would change if you marked them extern are the function pointers. Function declarations are by default extern in C++.
Try and compile the following program
template <typename...>
struct WhichType;
typedef void fcn_t(void);
typedef void (*ptr_t)(void);
// constexpr auto one = WhichType<fcn_t>{};
// constexpr auto two = WhichType<fcn_t*>{};
// constexpr auto three = WhichType<ptr_t>{};
fcn_t f;
void f() {}
int main() {
f();
}
Uncommenting the commented lines will likely give you a compiler error that tells you what types the WhichType instance is being instantiated with, and as a result it should show you the exact types of all three things you asked about. It's a trick I picked up from Scott Meyers' book "Effective Modern C++".
To test whether the declarations are extern or not, write two simple implementation files, with one containing the definition of the variable
main.cpp
template <typename...>
struct WhichType;
typedef void fcn_t(void);
typedef void (*ptr_t)(void);
fcn_t f;
int main() {
f();
}
definition.cpp
void f() {}
and then compile, link and run definition.cpp and main.cpp (via g++ -std=c++14 definition.cpp main.cpp and then ./a.out). If the declaration was not extern then the compile would fail with an undefined symbol error.
Here is a simple illustration of these typedef
#include <stdio.h>
typedef void fcn_t(void);
typedef void (*ptr_t)(void);
fcn_t f;
fcn_t *pf;
ptr_t pf2;
void
f(void)
{
printf("I am void\n");
}
void
vSayHi(void)
{
printf( "Hi\n" );
}
int
main(void)
{
pf2 = vSayHi;
pf2();
pf = vSayHi;
pf();
f();
return 0;
}
OUTPUT:
Hi
Hi
I am void
The following code explains the situation I encountered:
#ifdef __cplusplus
namespace ns
{
class pod
{
const short foo;
const char bar;
public:
pod(short f,char b):foo(f),bar(b){}
char foobar();
};
}
#else
typedef struct pod pod;
#endif
#ifdef __cplusplus
extern "C"{
#endif
extern pod* pod_new(short f, char b);//BANG!!!
extern char pod_foobar(pod* p); //BANG!!!
#ifdef __cplusplus
}
#endif
I can't put the C linkage functions inside the namespace ns, or the C client won't find their definitions. When I pull them out of the namespace, the C++ definition won't compile either,all because of the pod solution, which I learned from C++ FAQ Lite,it's only a preprocessor trick. And sadly, this trick couldn't deal with namespaces.
What am I supposed to do? Should I throw out all type-safety, and replace pod* with void*, or is there a nicer solution for this kinda situation? Any advices? Please!
I'd just give two different declarations for the functions
for C++:
extern "C" ns::pod* pod_new(short f, char b);
extern "C" char pod_foobar(ns::pod* p);
and for C:
typedef struct pod pod;
extern pod* pod_new(short f, char b);
extern char pod_foobar(pod* p);
But if this doesn't satisfy you, for C++ you could also have a typedef
typedef ns::pod ns_pod;
for C
typedef struct ns_pod ns_pod;
and then have the same common function prototype.
extern ns_pod* pod_new(short f, char b);
extern char pod_foobar(ns_pod* p);
Edit: In C the struct pod or struct ns_pod is an incomplete type, so in C directly you could never do anything that uses the fields or asks for its size. The difference between a pointer to an incomplete type and a void* is that you can only assign such a struct pointer to another struct pointer of the same incomplete type.
typedef struct ns_pod2 pod2;
ns_pod* q = pod_new(...); // valid
ns_pod2* r = pod_new(...); // a constraint violation! (= compiler error)
The second one would need an explicit cast, if you want to insist. This is one of the reasons why casts are frowned upon by many C programmers.
I figured it out myself :) by checking the symbols of the obj files with nm.
It turns out that the C++ namespaces have no effects on functions with C linkage, therefore I can rewrite the code above like this:
#ifdef __cplusplus
namespace ns
{
class pod
{
const short foo;
const char bar;
public:
pod(short f,char b):foo(f),bar(b){}
char foobar();
};
}
#else
typedef struct pod pod;
#endif
#ifdef __cplusplus
namespace ns{
extern "C"{
#endif
pod* pod_new(short f, char b);
char pod_foobar(pod* p);
void pod_free(pod* p);
#ifdef __cplusplus
}
}
#endif
i made a code but has errors and wan't able to solve them:-
the following errors are in one of mine header file(code shown below)
error C2143:syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Header file code:-
#ifndef _DEVICE_SOURCE_FICARD_HH
#define _DEVICE_SOURCE_FICARD_HH
#ifndef _FRAMED_SOURCE_HH
#include "FramedSource.hh"
#endif
#include <DeviceSource.hh>
typedef struct LtagBufferEntry
{
char *pBuffer;
struct LtagBufferEntry *pNext;
} LBufferEntry;
class FICardDeviceParameters {
public:
(RetEntry*)(*p_lm_lock_fn)( void *data ); //error at this line
void (*p_lm_unlock_fn)( void *data );
int nFICardFrameSize;
//%%% TO BE WRITTEN %%%
};
class DeviceSourceFICard: public DeviceSource {
public:
static DeviceSourceFICard* createNew(UsageEnvironment& env, FICardDeviceParameters fi_params,
DeviceParameters params);
protected:
DeviceSourceFICard(UsageEnvironment& env, FICardDeviceParameters fi_params, DeviceParameters params);
// called only by createNew(), or by subclass constructors
virtual ~DeviceSourceFICard();
private:
// redefined virtual functions:
virtual void doGetNextFrame();
private:
void deliverFrame();
private:
DeviceParameters fParams;
LBufferEntry *pData;
char * pRetData;
//int nFICardFrameSize;
FICardDeviceParameters fiParams;
};
#endif //_DEVICE_SOURCE_FICARD_HH
Defination of RetEntry:-
typedef struct tagRetBuffer
{
char *pBuffer;
int nDataLn;
} RetEntry;
void InitBufferHandling();
void TransferBuffer( void *pBuffer );
RetEntry *lm_lock_fn( void *data );
void lm_unlock_fn( void *data );
int initLm555Settings(void);
void play();
void afterPlaying(void*);
void init_play();
void StartRTPProcess(void);
How to fix them...
The problem is that the compiler does not know what RetEntry is in the expression:
(RetEntry*)(*p_lm_lock_fn)( void *data );
You must either include the header that provides the definition or else declare it in your current header. From the code below it seems to be a typedef to tagRetBuffer, so to declare it in the current translation unit you will need to declare the struct and the typedef:
struct tagRetBuffer;
typedef tagRetBuffer RetEntry;
Now, I would suggest that you avoid those typedef's altogether, in C++ the compiler will search for types if needed. You can just do:
struct RetEntry
{
char *pBuffer;
int nDataLn;
};
And then use RetEntry anywhere as if it was a typedef'ed name. Note that there are differences, but that in 99% of the cases you will not notice. And when you do notice it might even be to your advantage (for example, you cannot declare a typedef, so you need to declare the typedef'ed name and then redefine the typedef as per the first suggestion here).