使用c ++类设计链表的问题 [英] problem in design of linklist using c++ class

查看:97
本文介绍了使用c ++类设计链表的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include"stdafx.h"
#include<iostream>
#include<ctime>
#include<cstddef>
#include<fstream>

using namespace std;

class person
{
	char *name;
public:
	person(){};
	void set_name(char *nam)
	{
		name=nam;
	}
	char* get_name()
	{
		return name;
	}
};
class student:public person
{
	 int year_of_joining;
	 int rollno;
	 char *stream;
public:
	student(int year,int roll,char *str,char *nam);
	student *next,*prev;
};
student::student(int year,int roll,char *str,char *nam)
{
	year_of_joining=year;
	rollno=roll;
	stream=str;
	this->set_name(nam);  //call to the person class to define name
	next=NULL;
	prev=NULL;
}
class linklist
{
	files *f;
	student *start,*temp;  //pointer to the first and the last member of the linklist respectively
public:
	linklist(int year,int roll,char *str,char *nam);
	void append(int year,int roll,char *str,char *nam);
	char* show_name();
};
char* linklist::show_name()
{
	return temp->get_name();
}
linklist::linklist(int year,int roll,char *str,char *nam)
{
	student *ptr=new student(year,roll,str,nam);
	start=ptr;
	temp=ptr;
}
void linklist::append(int year,int roll,char *str,char *nam)
{
	student *ptr1=new student(year,roll,str,nam);
	temp->next=ptr1;
	ptr1->prev=temp;
	temp=ptr1;
}
class files
{
	ifstream *in;
	ofstream *out;
public:
	files();
};

int main()
{
	linklist l(2009,1809317,"cse","bhawin");
	l.append(2009,1809315,"cse","anshul");
	return 0;
}







在上面的代码中,第一个私有成员有问题链接列表

错误说明:缺少类型说明符 - 假设为int。注意:C ++不支持default-int

,第二个错误是 - 语法错误:缺少';'之前'*'



i只是无法理解为什么我不能将类对象文件类添加为linklist类的私有成员




in the above code there is a problem at first private member of class linklist
the error says:missing type specifier - int assumed. Note: C++ does not support default-int
and the second error is - syntax error : missing ';' before '*'

i just can't understand why i cant add class object file class as a private member of linklist class

推荐答案

为了解决您的问题,您可以向 files class,即:



To fix your problem you may add a forward declaration to the files class, namely:

class files; // forward declaration of the class 'files'
class linklist
{
  files *f;
  // ..

您有一个不允许的前向引用。需要在 linklist 类之前定义文件类。
You have a forward reference which is not allowed. The files class needs to be defined before the linklist class.


这篇关于使用c ++类设计链表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆