vc++的高手们,麻烦看看这个错误,应该怎么改正啊!!
源程序:#include<iostream.h>#include<malloc.h>#include<string.h>#defineNULL0#defineLENsiz...
源程序:
#include <iostream.h>
#include<malloc.h>
#include <string.h>
#define NULL 0
#define LEN sizeof(struct headtitle)
class headname
{
private:
struct headtitle
{
char name[20];
struct headtitle *next;
};
public:
int n;
struct headtitle *input()
{
struct headtitle *head;
struct headtitle *p1,* p2;
n=0;
p1=p2=(struct headtitle*) malloc (sizeof(struct headtitle));
cout<<"Please Input What Titles You Need!(输入0代表结束)"<<"\n";
cin>>p1->name;
head=NULL;
while (strcmp(p1->name, "0") != 0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct headtitle *)malloc (sizeof(struct headtitle));
cin>>p1->name;
}
p2->next=NULL;
return(head);
}
void showtitle(struct headtitle *head)
{
struct headtitle *p;
p=head;
if(head!=NULL)
do
{
cout<<p->name<<"\t";
p=p->next;
}while(p!=NULL);
}
void freetitle(struct headtitle *head)
{
delete head;
cout<<"It's Succeed to Delete!";
}
};
int main()
{
struct headtitle *hd;
cout<<"Start Testing"<<"\n";
headname head1;
hd=head1.input();
head1.showtitle(hd);
head1.freetitle(head1.input());
cout<<"\n"<<"Text Over!"<<"\n";
return 0;
}
错误1:F:\Visual C++ 6.0 File\heads2\head.cpp(62) : error C2664: 'showtitle' : cannot convert parameter 1 from 'struct main::headtitle *' to 'struct headname::headtitle *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
错误2:F:\Visual C++ 6.0 File\heads2\head.cpp(62) : error C2664: 'showtitle' : cannot convert parameter 1 from 'struct main::headtitle *' to 'struct headname::headtitle *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
急啊!!!麻烦各位大虾啦!!!
如果我想把headtitle结构体放在headname类里面,那么我该怎么做才能避免这样的错误? 展开
#include <iostream.h>
#include<malloc.h>
#include <string.h>
#define NULL 0
#define LEN sizeof(struct headtitle)
class headname
{
private:
struct headtitle
{
char name[20];
struct headtitle *next;
};
public:
int n;
struct headtitle *input()
{
struct headtitle *head;
struct headtitle *p1,* p2;
n=0;
p1=p2=(struct headtitle*) malloc (sizeof(struct headtitle));
cout<<"Please Input What Titles You Need!(输入0代表结束)"<<"\n";
cin>>p1->name;
head=NULL;
while (strcmp(p1->name, "0") != 0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct headtitle *)malloc (sizeof(struct headtitle));
cin>>p1->name;
}
p2->next=NULL;
return(head);
}
void showtitle(struct headtitle *head)
{
struct headtitle *p;
p=head;
if(head!=NULL)
do
{
cout<<p->name<<"\t";
p=p->next;
}while(p!=NULL);
}
void freetitle(struct headtitle *head)
{
delete head;
cout<<"It's Succeed to Delete!";
}
};
int main()
{
struct headtitle *hd;
cout<<"Start Testing"<<"\n";
headname head1;
hd=head1.input();
head1.showtitle(hd);
head1.freetitle(head1.input());
cout<<"\n"<<"Text Over!"<<"\n";
return 0;
}
错误1:F:\Visual C++ 6.0 File\heads2\head.cpp(62) : error C2664: 'showtitle' : cannot convert parameter 1 from 'struct main::headtitle *' to 'struct headname::headtitle *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
错误2:F:\Visual C++ 6.0 File\heads2\head.cpp(62) : error C2664: 'showtitle' : cannot convert parameter 1 from 'struct main::headtitle *' to 'struct headname::headtitle *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
急啊!!!麻烦各位大虾啦!!!
如果我想把headtitle结构体放在headname类里面,那么我该怎么做才能避免这样的错误? 展开
2个回答
展开全部
改成这样子 结构体的声明放在类的外部
如果在类中用结构体的变量直接用就行了
结构体也是一种数据类型 不要放在类里
#include <iostream.h>
#include<malloc.h>
#include <string.h>
#define NULL 0
#define LEN sizeof(struct headtitle)
struct headtitle
{
char name[20];
struct headtitle *next;
};
class headname
{
private:
headtitle *head;//当用的时候直接用就行了
headtitle *p1,* p2;//同上
public:
int n;
struct headtitle *input()
{
headtitle *head;
headtitle *p1,* p2;
n=0;
p1=p2=(struct headtitle*) malloc (sizeof(struct headtitle));
cout<<"Please Input What Titles You Need!(输入0代表结束)"<<"\n";
cin>>p1->name;
head=NULL;
while (strcmp(p1->name, "0") != 0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct headtitle *)malloc (sizeof(struct headtitle));
cin>>p1->name;
}
p2->next=NULL;
return(head);
}
void showtitle(struct headtitle *head)
{
struct headtitle *p;
p=head;
if(head!=NULL)
do
{
cout<<p->name<<"\t";
p=p->next;
}while(p!=NULL);
}
void freetitle(struct headtitle *head)
{
delete head;
cout<<"It's Succeed to Delete!";
}
};
int main()
{
struct headtitle *hd;
cout<<"Start Testing"<<"\n";
headname head1;
hd=head1.input();
head1.showtitle(hd);
head1.freetitle(head1.input());
cout<<"\n"<<"Text Over!"<<"\n";
return 0;
}
如果在类中用结构体的变量直接用就行了
结构体也是一种数据类型 不要放在类里
#include <iostream.h>
#include<malloc.h>
#include <string.h>
#define NULL 0
#define LEN sizeof(struct headtitle)
struct headtitle
{
char name[20];
struct headtitle *next;
};
class headname
{
private:
headtitle *head;//当用的时候直接用就行了
headtitle *p1,* p2;//同上
public:
int n;
struct headtitle *input()
{
headtitle *head;
headtitle *p1,* p2;
n=0;
p1=p2=(struct headtitle*) malloc (sizeof(struct headtitle));
cout<<"Please Input What Titles You Need!(输入0代表结束)"<<"\n";
cin>>p1->name;
head=NULL;
while (strcmp(p1->name, "0") != 0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct headtitle *)malloc (sizeof(struct headtitle));
cin>>p1->name;
}
p2->next=NULL;
return(head);
}
void showtitle(struct headtitle *head)
{
struct headtitle *p;
p=head;
if(head!=NULL)
do
{
cout<<p->name<<"\t";
p=p->next;
}while(p!=NULL);
}
void freetitle(struct headtitle *head)
{
delete head;
cout<<"It's Succeed to Delete!";
}
};
int main()
{
struct headtitle *hd;
cout<<"Start Testing"<<"\n";
headname head1;
hd=head1.input();
head1.showtitle(hd);
head1.freetitle(head1.input());
cout<<"\n"<<"Text Over!"<<"\n";
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询