返回信息流#include <iostream>
#include"list.h"
list::list()
{
top = 0;
}
bool list::isempty()
{
return top == 0;
}
bool list::isfull()
{
return top == MAX;
}
bool list::push(Item&item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
void list::visit(void(*pf)(Item&item))
{
for (int i = 0; i < top; i++)
(*pf)(items[i]);
}
这是一条镜像帖。来源:北邮人论坛 / cpp / #87839同步于 2015/7/9
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
Re: 弄不明白这个问题我睡不踏实呀,大家帮看看这个程序,为什么
herbice
2015/7/9镜像同步3 回复
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
#include<iostream>
#include"list.h"
void show(const Item&g);
int main()
{
using namespace std;
list movies;//创建对象,并默认构造
Item temp;//创建结构Item
if (movies.isfull())//如果list已满,则执行
{
cout << "The list is full.Bye\n";
exit(1);
}
cout << "Enter a movie name:";
while (cin.get(temp.title, Len) && temp.title != "\0")//若输入电影名称,且不为空格则进入循环
{
cout << "Enter your rating :";
cin >> temp.rating;
while (cin.get() != '\n')
continue;//输入数字后不能直接再读取字符串
if (movies.push(temp) == false)//方法与对象
{
cout << "The list is already full.\n";
break;
}
if (movies.isfull())
{
cout << "You have filled this list.\n";
break;
}
cout << "Enter next movie.\n";
}
if (movies.isempty())
cout << "No data\n";
else
{
movies.visit(show);
}
cout << "Bye!\n";
return 0;
}
void show(Item&g)
{
std::cout << "Movie:" << g.title << std::endl;
std::cout << "Rating:" << g.rating << std::endl;
}
void visit(void(*pf)(Item&item)); 其中pf与void show(const Item&g);不一致
【 在 herbice 的大作中提到: 】
: 编译结果为:1>c:\users\herbice\documents\visual studio 2013\projects\栈设计\栈设计\源1.cpp(38): error C2664: “void list::visit(void (__cdecl *)(Item &))”: 无法将参数 1 从“void (__cdecl *)(const Item &)”转换为“void (__cdecl *)(Item &)”
: 1> 在匹配目标类型的范围内没有具有该名称的函数
: ========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
: ...................