Dear all,
When I wrote codes like shown below in C++
*********************************
struct node
{
int item; node *next;
node (int x, node *t)
{item=x; next=t;}
};
typedef node *link;
**********************************
However, the compiler warned that "typedef struct node*link redeclared
as different kind of symbol". I cannot figure out what's wrong with my
last line as it looked to me all right when I meant to define a pointer
type named link referring to the structure of node which I set up in
the first line.
Thank you very much for you help and have a nice day!
Cheers
Joe
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
tony_zhouzhou (1)
|
5/1/2005 3:02:33 PM |
|
I don't see anything wrong with the snippet you posted. But the
compiler is saying that you have defined 'link' as another type
somewhere else. What other files are you including in your project?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
gaz
|
5/2/2005 8:19:20 AM
|
|
Joe wrote:
> When I wrote codes like shown below in C++
>
> *********************************
> struct node
> {
> int item; node *next;
>
> node (int x, node *t)
> {item=x; next=t;}
Prefer initialisation over assignment. See FAQ.
> };
>
> typedef node *link;
> **********************************
>
> However, the compiler warned that "typedef struct node*link redeclared
> as different kind of symbol". I cannot figure out what's wrong with my
> last line as it looked to me all right when I meant to define a
> pointer type named link referring to the structure of node which I
> set up in the first line.
There is nothing in the code as you've shown that would lead to such
message. Apparently, the symbol 'link' has been defined as something
else elsewhere and you're not showing us the entire code.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Victor
|
5/2/2005 8:22:47 AM
|
|
Thanks!Gaz and V,
The complete programme is listed here, which is a simple exercise to
play with Josephus algorithm using linked list.
---"node.h"-------------------------------------------------------------------------
struct node
{
int item; node *next;
node (int x, node *t)
{
item=x;
next=t;
};
};
typedef node *link;
---main-----------------------------------------------------------------------------------------
#include <iostream.h>
#include <stdlib.h>
#include "node.h"
int main (int argc, char *argv[])
{
int i, N=atoi(argv[1]), M=atoi(argv[2]);
link t= new node (1,0);
t->next= t;
link x= t;
for (i=2; i<=N; i++)
{
x= (x->next= new node (i, t));
}
while (x!= x->next)
{
for (i=1; i<M; i++)
{
x= x->next;
}
x->next= x->next->next;
}
cout << x->item <<endl;
}
-------------------------------------------------------------------------------------------------
I don't think I have redefined "link" as other types in my main. I only
defined t and x as the type "link".
The error msgs were:
1, node.h:12: error: `typedef struct node*link' redeclared as different
kind of
symbol
2, /usr/include/unistd.h:722: error: previous declaration of `int
link(const
char*, const char*)'
Thank you very much for your help and time!
Joe
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Joe
|
5/3/2005 8:25:07 AM
|
|
> 2, /usr/include/unistd.h:722: error: previous declaration of `int
link(const char*, const char*)'
There you go - the message says it all! Apparently some of the files
you included ended up including "unistd.h" which has a function with
this prototype:
int link(const char*,const char*);
Hence, the redeclaration. So, what do you do? Change your "link" to
some other name like "list_link" or something ... or even better, get
used to using namespaces. Specifically, the headers should be changed
to
#include<iostream>
#include<cstdlib>
#include"node.h"
then add this inside main():
using std::cout,std::endl,std::atoi;
This should solve your problem - the header filenames you are using
were abandoned quite a few years back
Samee
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Samee
|
5/3/2005 11:00:16 AM
|
|
Samee Zahur wrote:
> using std::cout,std::endl,std::atoi;
Unfortunately, this doesn't work: you have to use separate using
declarations for each of them.
using std::cout;
using std::endl;
using std::atoi;
I cannot explain why, but this is what the grammar says.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Seungbeom
|
5/5/2005 11:12:59 AM
|
|
In article <d5clb4$755$1@news.Stanford.EDU>, Seungbeom Kim
<musiphil@bawi.org> writes
>Samee Zahur wrote:
>> using std::cout,std::endl,std::atoi;
>
>Unfortunately, this doesn't work: you have to use separate using
>declarations for each of them.
>
> using std::cout;
> using std::endl;
> using std::atoi;
>
>I cannot explain why, but this is what the grammar says.
Yes, and that what the grammar means. We consciously and deliberately
restricted using declarations to one name per statement. Note that many
coding guidelines also restrict variable declarations to one per
statement. If you really hate typing using repetitively just hot key it.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Francis
|
5/5/2005 12:42:13 PM
|
|
"Francis Glassborow" <francis@robinton.demon.co.uk> wrote in message
news:QDkreqXGmgeCFwBY@robinton.demon.co.uk...
> >
> >Unfortunately, this doesn't work: you have to use separate using
> >declarations for each of them.
> >
> > using std::cout;
> > using std::endl;
> > using std::atoi;
> >
> >I cannot explain why, but this is what the grammar says.
>
> Yes, and that what the grammar means. We consciously and deliberately
> restricted using declarations to one name per statement.
I'm cool with the conscious and deliberate part...
> Note that many
> coding guidelines also restrict variable declarations to one per
> statement. If you really hate typing using repetitively just hot key it.
..... but I can't help thinking that the wrong problem got fixed.
The reason for recommending against multiple declarations is to
avoid confusion with _pointer_ declarations (or other situations
where a declaration modifier such as "const" or "volatile" could
intuitively apply to a narrower -vs- broader portion of a comma
separated list). However, there would not have been a similar
chance for confusion in "using" statement.
- Risto -
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Risto
|
5/8/2005 5:43:53 PM
|
|
> If you really hate typing using repetitively just hot key it.
Hehe ... I know it doesn't work ... but since I don't have it hot keyed
into my newsreader ... oh well, I was expecting the OP to look up what
using is anyway ... but guess I should get my fingers moving a little
more!
Anyway, thanks for pointing it out!
> Yes, and that what the grammar means. We consciously and deliberately
> restricted using declarations to one name per statement.Note that
many
> coding guidelines also restrict variable declarations to one per
> statement.
Yes, and it always got me wondering why - does it really have THAT bad
an effect on code legibility ? Or is it something else more important
here... can you help me out ?
Samee
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Samee
|
5/8/2005 5:44:55 PM
|
|
|
8 Replies
146 Views
(page loaded in 0.147 seconds)
|