#pragma once #include"boy.h" class Ring { public: Ring(); Ring(int a); ~Ring(); Boy gethonorees(int m, int n); protected: Boy no1; Boy no2; Boy no3; private: void countupto(int m); Boy* pFirst; Boy* pcurrent; };#include"Ring.h" #include"boy.h" #include<iostream> using namespace std; Ring::Ring(int n) { pFirst = pcurrent = new Boy(NULL, 1); Boy* pb = pFirst; for (int i = 2; i <= n; i++) { pb = new Boy(pb, i); } } void Ring::countupto(int m) { Boy* plast = pcurrent; for (int i = m; i > 1; i--) { plast=pcurrent; pcurrent = pcurrent->next(); } pcurrent->leave(plast); delete pcurrent; pcurrent = plast->next(); } Ring::~Ring() {}; Boy Ring::gethonorees(int m, int n) { while (n > 3) { countupto(m); n--; } Boy* plast = pcurrent; for (int i = m; i > 1; i--) { plast = pcurrent; pcurrent = pcurrent->next(); } Boy no1(*pcurrent); pcurrent->leave(plast); delete pcurrent; pcurrent = plast->next(); for (int i = m; i > 1; i--) { plast = pcurrent; pcurrent = pcurrent->next(); } Boy no2(*pcurrent); pcurrent->leave(plast); delete pcurrent; pcurrent = plast->next(); Boy no3(*pcurrent); no1.print(); no2.print(); no3.print(); return no1; } int n;Ring(n);报错,函数重载
时间: 2023-06-14 22:05:49 浏览: 100
#pragma用法讲解.pdf
这段代码有一些问题:
1. 在 Ring 类中,构造函数和析构函数的实现都没有被写出来,只有声明,这会导致编译错误。
2. 在 Ring 类的构造函数中,变量 n 没有被定义,所以 Ring(n) 会报错。
3. 在 Ring 类的 gethonorees 函数中,no1、no2、no3 三个对象都是在函数内部定义的局部变量,函数返回后就会被销毁,所以在函数外部调用这个函数时,no1、no2、no3 对象的值是未定义的。
函数重载的问题应该是代码中没有体现出来的,如果有,也需要具体看报错信息才能确定原因。
阅读全文