// a headfile
#ifndef _LIST_H
#define _LIST_H
class List
{
private:
enum{MAX=10};
double Alist[MAX];
int top;
public:
List();
List(double Ali[]);
void add(double n);
bool isempty();
bool isfull();
void visit(void(*pf)(double &));
void show();
};
void fun(double &n);
#endif
// impletement list
#include<iostream>
#include"list.h"
List::List()
{
Alist[0] = '\0';
top = 0;
}
List::List(double Ali[])
{
int i = 0;
for ( i = 0; Ali[i] != '\0'; i++)
Alist[i] = Ali[i];
top = i;
}
void List::add(double n)
{
Alist[++top] = n;
}
bool List::isempty()
{
return top == 0;
}
bool List::isfull()
{
return top ==MAX;
}
void List::visit(void(*pf)(double &))
{
for (int i = 0; Alist[i] != '\0'; i++)
{
std::cout << Alist[i] << std::endl;
(*pf)(Alist[i]);
std::cout << Alist[i]<<std::endl;
}
}
void List::show()
{
for (int i = 0; Alist[i] != '\0'; i++)
std::cout << Alist[i] << std::endl;
}
void fun(double &n)
{
n += 5.0;
}
//using list
#include<iostream>
#include"list.h"
using namespace std;
int main()
{
double arr[4] = { 4.5, 6.0, 8.5, 10.5 };
List one(arr);
cout << one.isfull() << endl;
one.show();
cout << one.isempty() << endl;
one.visit(fun);
system("pause");
return 0;
}
但是每次执行都错误一堆!!!
自己是在找不到错误啊,是不是visit()函数中的参数pf函数不能调用私有变量啊!!
#ifndef _LIST_H
#define _LIST_H
class List
{
private:
enum{MAX=10};
double Alist[MAX];
int top;
public:
List();
List(double Ali[]);
void add(double n);
bool isempty();
bool isfull();
void visit(void(*pf)(double &));
void show();
};
void fun(double &n);
#endif
// impletement list
#include<iostream>
#include"list.h"
List::List()
{
Alist[0] = '\0';
top = 0;
}
List::List(double Ali[])
{
int i = 0;
for ( i = 0; Ali[i] != '\0'; i++)
Alist[i] = Ali[i];
top = i;
}
void List::add(double n)
{
Alist[++top] = n;
}
bool List::isempty()
{
return top == 0;
}
bool List::isfull()
{
return top ==MAX;
}
void List::visit(void(*pf)(double &))
{
for (int i = 0; Alist[i] != '\0'; i++)
{
std::cout << Alist[i] << std::endl;
(*pf)(Alist[i]);
std::cout << Alist[i]<<std::endl;
}
}
void List::show()
{
for (int i = 0; Alist[i] != '\0'; i++)
std::cout << Alist[i] << std::endl;
}
void fun(double &n)
{
n += 5.0;
}
//using list
#include<iostream>
#include"list.h"
using namespace std;
int main()
{
double arr[4] = { 4.5, 6.0, 8.5, 10.5 };
List one(arr);
cout << one.isfull() << endl;
one.show();
cout << one.isempty() << endl;
one.visit(fun);
system("pause");
return 0;
}
但是每次执行都错误一堆!!!
自己是在找不到错误啊,是不是visit()函数中的参数pf函数不能调用私有变量啊!!
