C++ C代码在像C++;
开发者们好!我正在学习算法从算法设计手册的斯基纳。我有以下代码:C++ C代码在像C++;,c++,c,pointers,C++,C,Pointers,开发者们好!我正在学习算法从算法设计手册的斯基纳。我有以下代码: #include <stdio.h> #include <stdlib.h> typedef int item_type; typedef struct{ item_type item; struct list* next; }list; void insert_list(list **l, item_type x){ list *p; p = mall
#include <stdio.h>
#include <stdlib.h>
typedef int item_type;
typedef struct{
item_type item;
struct list* next;
}list;
void insert_list(list **l, item_type x){
list *p;
p = malloc(sizeof(list));
p->item = x;
p->next = *l;
*l = p;
}
int main(){
return 0;
}
#包括
#包括
typedef int item_type;
类型定义结构{
项目类型项目;
结构列表*下一步;
}名单;
无效插入列表(列表**l,项目类型x){
列表*p;
p=malloc(sizeof(list));
p->item=x;
p->next=*l;
*l=p;
}
int main(){
返回0;
}
它在编译时给我警告:
gcc-Wall-o“test”“test.c”(在目录中:
/home/akacoder/Desktop/Algorithm\u Design\u Manual/chapter2)test.c:In
函数“插入列表”:test.c:15:警告:从
不兼容的指针类型编译已成功完成
但是当我用C++重写这段代码时:
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef int item_type;
typedef struct{
item_type item;
struct list* next;
}list;
void insert_list(list **l, item_type x){
list *p;
p = malloc(sizeof(list));
p->item = x;
p->next = *l;
*l = p;
}
int main(){
return 0;
}
#包括
#包括
#包括
使用名称空间std;
typedef int item_type;
类型定义结构{
项目类型项目;
结构列表*下一步;
}名单;
无效插入列表(列表**l,项目类型x){
列表*p;
p=malloc(sizeof(list));
p->item=x;
p->next=*l;
*l=p;
}
int main(){
返回0;
}
它提供了以下内容:
g++-Wall-o“chapter2”chapter2.cpp(在目录中:
/主页/akacoder/桌面/算法\设计\手册/第2章)
第2章cpp:15:错误:声明“typedef结构列表”冲突
list'chapter2.cpp:14:错误:“struct list”具有以前的声明
作为'struct list'第2.cpp章:在函数'void insert_list(list**,
“项目类型”:第2.cpp章:在函数“无效插入”列表(列表**,
项目(类型):第2章。cpp:19:错误:从“void*”转换无效
至“列表*”
有人能解释为什么会这样吗?我如何在C++中重写?< p>这是因为C++相对于类型转换更严格,而不是C。 代码中还有许多其他错误。请注意,只需将C源代码重命名为<代码> .CPP < /C> >编译>使用代码> G++< /C> >不将C源代码作为C++。
如果您正在用
c++
编写程序,请使用new
&而不是malloc
,这样您就不需要显式键入cast,因为malloc
c++不允许任意指针转换,而c不允许。但由于这被认为是不好的样式,编译器会发出警告
只需添加一个cast,即可解决这两条消息:
p = (list*)malloc(sizeof(list));
或者如果你只想成为C++:
p = new list;
但是,您也应该声明构造函数,等等。
< P>只有C警告,C++可能会考虑错误。 这是一种编程文化。C非常宽容,没有强制执行它的打字系统。C++仍然很宽容,但是你在C中做了一些事情,即使C++也不会原谅。 当您malloc该内存块时,将其转换为指向列表的指针。这将把地址(指针)转换为正确类型的指针如果没有该强制转换,您可以拥有任意大小的malloc,并且无法确定它是要由列表指针还是其他指针引用。在这两种情况下,您的问题都在于结构定义:
struct list*next
不引用您正在声明的结构。请尝试以下方法:
typedef struct list {
item_type item;
struct list* next;
} list;
<> P>此外,在C++中,必须将struct list*
是指向结构的指针,因此它不必抱怨自己还不知道“struct list”是什么。但是,稍后,当您尝试从类型为“list*”的指针(类型为“指向匿名结构的指针”)分配此“struct list*”时,它会抱怨不匹配
在C++中,“结构”声明或多或少等价于“类”声明(主要区别在于成员的默认可见性)。除此之外,这意味着C++中的结构或多或少都是自动类型化的。因此,当编译器看到“struct list*next”时,它将其作为名为“list”的类的前向声明;然后,当它完成语句并处理typedef时,会抛出一个错误,因为您试图将某个内容typedef到一个已经(正向)声明为其他内容的标识符。然后,由于前面的错误,它实际上不知道“列表”可能是什么,因此会发出进一步的错误。您需要更改此类:
typedef struct{
item_type item;
struct list* next;
}list;
为此:
struct list {
item_type item;
list* next;
};
说明:在第一个示例中,您有一个匿名结构,其中struct list
是向前声明的。因此,当编译器在下一行看到Type的时候,它发现了名称冲突,因为TyPulf与C++中的Stult声明不一样。
引述:
< C++ >程序员使用C<强/> /P>
结构和枚举
你必须
在要创建的结构类型的名称之前包含struct关键字
声明一个结构:在C++中,你可以这样做< /P>
struct a_struct{
intx;}代码>
a_struct_实例代码>
并有一个名为struct\u instance
的结构的新实例。在C中,
但是,在声明时必须包含struct
关键字
struct\u实例
:
struct a_struct_实例代码>
事实上,声明枚举时也存在类似的情况:在C中,您可以
必须包含关键字enum
;在C++中,你不必这样做。作为一方
注意,大多数C程序员通过使用typedef来解决这个问题:
类型定义结构名称{
/*变量*/}struct\u name\u t
typedef struct struct_name {
struct struct_name instance;
struct_name_t instance2; /* invalid! The typedef isn't defined
yet */ } struct_name_t;
typedef struct list_ {
item_type item;
struct list_* next;
} list;
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef int item_type;
struct list{
item_type item;
list* next;
};
void insert_list(list **l, item_type x){
list *p;
p = (list*)malloc(sizeof(list));
p->item = x;
p->next = *l;
*l = p;
}
int main(){
return 0;
}