错误清单

这篇博客列举了两个编程中遇到的问题及其解决方法。第一个问题是关于编译错误86.10,错误原因在于将BookStore b()误解为函数,修正方法是将它改为BookStore b。第二个问题是文件在构造函数中打开不完整,可能是由于char类型不足导致,调整后程序已调试完成。
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<iomanip>

using namespace std;

class Book{
	public:
		char name[20];
		char author[6];
		char publisher[20];
		char ptime[5];
		int price;
		int number;
	
		Book(){}
		friend istream &operator>>(istream &sm,Book &b)
		{
			sm>>b.name>>b.author>>b.publisher>>b.ptime>>b.price>>b.number;
			return sm;
		}
		friend ostream &operator<<(ostream &sm,Book &b)
		{
			sm<<setiosflags(ios_base::left)<<setw(20)<<b.name;
			sm<<setiosflags(ios_base::left)<<setw(20)<<b.author;
			sm<<setiosflags(ios_base::left)<<setw(30)<<b.publisher;
			sm<<setiosflags(ios_base::left)<<setw(20)<<b.ptime;
			sm<<setiosflags(ios_base::left)<<setw(10)<<b.price;
			sm<<setiosflags(ios_base::left)<<setw(10)<<b.number;
			cout<<endl;
			return sm;
		}
};

class BookStore{
	private:
		int number;
		Book* list;
	public:
		BookStore()
		{
			fstream file;
			file.open("books.txt",ios::in);
			
			file>>number;
			list=new Book[number];	

			for(int i=0;i<number;i++)
			{
				file>>list[i].name>>list[i].author>>list[i].publisher>>list[i].ptime>>list[i].price>>list[i].number;
			}
			file.close();
		}
		~BookStore(){} 
		 void show()
		 {
		 	for(int i=0;i<number;i++)
		 	{
		 		cout<<list[i];
		 	}
		 }
};

int main()
{
	cout<<"---------------------------"<<endl;
	cout<<"|******图书管理系统*******|"<<endl;
	cout<<"|    1.显示库存图书       |"<<endl;
	cout<<"|    2.按出版社统计库存   |"<<endl;
	cout<<"|    3.按书名统计库存     |"<<endl;
	cout<<"|    4.按指定项目统计     |"<<endl;
	cout<<"|    0.退出               |"<<endl;
	cout<<"|*************************|"<<endl;
	cout<<"---------------------------"<<endl;
	
	int flag=1;
	while(flag)
	{
		BookStore b();
		cin>>flag;
		switch(flag)
		{
			case 1:
		    {
		    	b.show();
		    }
		}
	}
	
	 
	
	return 0;
} 

此时出现了个三问题;

1. 86.10 [Error] request for member 'show' in 'b', which is of non-class type 'BookStore()' 

    编译器将 BookStore b() 理解为了一个 返回类型为BookStore 的名为 b 的函数,所以它找不到这个成员函数。

将其改为 BookStore b 即可。

2.打开的文件不完整

   首先是基于设计的原因最好不要在构造函数中打开文件,其次文件不完整是因为我的char类型都不够大。


   以下是调试完成的程序

#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<iomanip>
#include<string.h>

using namespace std;

class Book{
	public:
		char name[20];
		char author[8];
		char publisher[24];
		int ptime;//[4];
		double price;
		int number;
	
		Book(/*char n[],char a[],char pu[],char pt[],int pri,int nu*/)
		{
		//	name=n;author=a;publisher=pu;ptime=pt;price=pri,number=nu;
		}
		Book(Book &b)
		{
			strcpy(name,b.name);strcpy(author,b.author);strcpy(publisher,b.publisher);ptime=b.ptime;price=b.price;number=b.number;
		}
	//	friend void operator=(Book &b)
//		{
//			strcpy(name,b.name);strcpy(author,b.author);strcpy(publisher,b.publisher);ptime=b.ptime;price=b.price;number=b.number;
//		}
		friend istream &operator>>(istream &sm,Book &b)
		{
			sm>>b.name>>b.author>>b.publisher>>b.ptime>>b.price>>b.number;
			return sm;
		}
		friend ostream &operator<<(ostream &sm,Book &b)
		{
			sm<<setiosflags(ios_base::left)<<setw(20)<<b.name;
			sm<<setiosflags(ios_base::left)<<setw(20)<<b.author;
			sm<<setiosflags(ios_base::left)<<setw(30)<<b.publisher;
			sm<<setiosflags(ios_base::left)<<setw(20)<<b.ptime;
			sm<<setiosflags(ios_base::left)<<setw(10)<<b.price;
			sm<<setiosflags(ios_base::left)<<setw(10)<<b.number;
//			sm<<b.name<<endl<<b.author<<endl<<b.publisher<<endl<<b.ptime<<endl<<b.price<<endl<<b.number;
			cout<<endl;
			return sm;
		}
		
};

class BookStore{
	private:
		int number;
		Book* list;
	public:
		BookStore(int n,Book* bb)
	    {
	    	number=n;
			list = new Book[n];
			for(int i=0;i<n;i++)
			{
				strcpy(list[i].name,bb[i].name);strcpy(list[i].author,bb[i].author);
				strcpy(list[i].publisher,bb[i].publisher);list[i].ptime=bb[i].ptime;
				list[i].price=bb[i].price;list[i].number=bb[i].number;
			//	cout<<list[i];
			}
			
			//cout<<number<<endl;
		}
		~BookStore(){} 
		 void show()
		 {
		 	for(int i=0;i<number;i++)
		 	{
		 		cout<<list[i];
		 	}
		 	//system("pause");
		 }
		 int find(char x[],int flag)
		 {
			int sum=0;
			if(flag==2)
			{
				for(int i=0;i<number;i++)
				{
					if(strcmp(x,list[i].publisher)==0)
						sum++;
				}	
			}
			if(flag==3)
			{
				for(int i=0;i<number;i++)
				{
					if(strcmp(x,list[i].name)==0)
						sum++;
				}
			}
			
		//	cout<<"sum="<<sum<<endl;
			return sum;
		}
};



int main()
{
	cout<<"---------------------------"<<endl;
	cout<<"|******图书管理系统*******|"<<endl;
	cout<<"|    1.显示库存图书       |"<<endl;
	cout<<"|    2.按出版社统计库存   |"<<endl;
	cout<<"|    3.按书名统计库存     |"<<endl;
	cout<<"|    0.退出               |"<<endl;
	cout<<"|*************************|"<<endl;
	cout<<"---------------------------"<<endl;
	int number=0;	
	fstream file;
	file.open("books.txt",ios::in);   //打开文件 
	if(!file)
	{
		cout<<"!!!!!!!!!!!!!!!!"<<endl;
	} 
	
	file>>number;    //读取书本数目 
//	cout<<"number="<<number<<endl;
	Book bb[number];
	for(int i=0;i<number;i++)   //循环读取每一个书的信息 
	{
		//file>>bb[i];
		//cout<<bb[i];
		file>>bb[i].name>>bb[i].author>>bb[i].publisher>>bb[i].ptime>>bb[i].price>>bb[i].number;
	//	cout<<bb[i];
	}
	
	file.close();
	int flag=1;
	while(flag)
	{
		BookStore b(number,bb);
		cin>>flag;
		int sum=0;
		switch(flag)
		{
			case 1:
		    {
		    	b.show();
		    }
		    case 2:
		    {
		    	cout<<"请输入出版社名称"<<endl; 
		    	char x_publisher[24];
		    	cin>>x_publisher;
		    	sum=0; 
		    	sum=b.find(x_publisher,2);
		    	cout<<x_publisher<<"的库存有:"<<sum<<"本"<<endl;
		    }
		    case 3:
		    {
		    	cout<<"请输入书名"<<endl;
				char x_name[20]; 
				cin>>x_name;
				sum=0;
				sum=b.find(x_name,3);
				cout<<x_name<<"的库存有:"<<sum<<"本"<<endl; 
		    }
		}
	}
	
	 
	
	return 0;

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值