User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,596 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,441 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1461 | Replies: 2
Reply
Join Date: Sep 2007
Posts: 2
Reputation: toonces is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
toonces toonces is offline Offline
Newbie Poster

Header files, C++ vs C compiler question

  #1  
Sep 7th, 2007
I am doing cross compiling between C++ and C compiler for header files and desperately need help on figuring out namespace issue error generated by C++ compiler.

Here are two header files:abc1.h, abc2.h and a C/C++ file containing the main().

When I compile this code on C, it works fine. However whenever I compile this on C++ compiler. It gives me the following error:
g++ -g -Wall cpptest.c -o cpptest
"abc2.h:3: error: using typedef-name ‘abc’ after ‘struct’
abc1.h:9: error: ‘abc’ has a previous declaration here"

I need to have both data structure available (struct _abc and struct abc)
someone typedef the _abc to abc inside abc1.h and I cannot change this file. I can only change abc2.h and the C/C++ file.
Anyone can explain to me why C++ compiler cannot accept this?
Anyone know of solution without changing abc1.h? (a work around)

/*********** Header file abc1.h************/
#ifndef __ABC1__
struct _abc
{
    int a;
    int b;
}/*abc*/;
/* if I comment out The line below and uncommented the abc above, it works well. 
    typedef seems to be an issue in C++ compiler. However I can not touch this header file. */
typedef struct _abc    abc;
#endif

/*********** Header file abc2.h************/
#ifndef __ABC2__
struct abc
{
    char *c;
    void *d;
};
#endif

/* C/C++ file named abc.c or abc.cpp */
#include "abc1.h"
#include "abc2.h"
#include <stdio.h>
int main()
{
    printf("testing \n");
    return 0;
}
Last edited by toonces : Sep 7th, 2007 at 10:55 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Cambridge, MA
Posts: 1,307
Reputation: Rashakil Fol has a spectacular aura about Rashakil Fol has a spectacular aura about 
Rep Power: 7
Solved Threads: 41
Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Salamander Man

Re: Header files, C++ vs C compiler question

  #2  
Sep 7th, 2007
You're defining abc in two places, and they're getting put into the same source file. I don't know what you expected to happen.
You can adblock jbennet's avatar.
Reply With Quote  
Join Date: Sep 2007
Posts: 2
Reputation: toonces is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
toonces toonces is offline Offline
Newbie Poster

Re: Header files, C++ vs C compiler question

  #3  
Sep 7th, 2007
I want to know if there is a work around and the reason why C++ compiler generates error while C compiler is fine with this.

I removed the comment from the abc1.h header file since it is not right.

/*********** Header file abc1.h************/
#ifndef __ABC1__
struct _abc
{
int a;
int b;
};
typedef struct _abc abc;
#endif
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:54 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC