#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 14:05:49 浏览: 97
这段代码有一些问题:
1. 在 Ring 类中,构造函数和析构函数的实现都没有被写出来,只有声明,这会导致编译错误。
2. 在 Ring 类的构造函数中,变量 n 没有被定义,所以 Ring(n) 会报错。
3. 在 Ring 类的 gethonorees 函数中,no1、no2、no3 三个对象都是在函数内部定义的局部变量,函数返回后就会被销毁,所以在函数外部调用这个函数时,no1、no2、no3 对象的值是未定义的。
函数重载的问题应该是代码中没有体现出来的,如果有,也需要具体看报错信息才能确定原因。
相关问题
/boy.h #pragma once// 原有的Boy类定义 class Boy { public: Boy(Boy* pPosition, int id); void leave(Boy* pPosition); void print(); Boy* next(); protected: int code; Boy* pNext; }; //boy.cpp #include"Boy.h" #include<iostream> using namespace std; Boy::Boy(Boy* pPosition, int id)//pPosition为上一个小孩的指针 { code = id; if (!pPosition)//判断是否存在 { this->pNext = this; } else { this->pNext = pPosition->pNext; pPosition->pNext = this; } } void Boy::leave(Boy* pPosition) { pPosition->pNext = this->pNext; cout << "本轮淘汰者编号:" << code << endl; } void Boy::print() { cout << "选手的编号:" << code; } Boy* Boy::next() { return pNext;//下一个选手的首地址 } //Ring.h #pragma once #include"Boy.h" class Ring { public: Ring(); Ring(int n); ~Ring(); Boy getwinner(int m); private: void countUpTo(int m);//数间隔数 Boy* pFirst; Boy* pCurrent; }; #include<iostream> #include"Boy.h" #include"Ring.h" using namespace std; Ring::Ring(){}; Ring::Ring(int n) { pFirst = pCurrent = new Boy(NULL, 1); Boy* pB = pFirst;//pB始终为前一个男孩的指针,用于循环 for (int i = 2; i <= n; i++) { pB = new Boy(pB, i); } } Boy Ring::getwinner(int m)//m为间隔数 { while (pCurrent != pCurrent->next()) { countUpTo(m);//应要包含1.数操作;2.输出淘汰编号;3.让淘汰者离开 } Boy win(pCurrent);//拷贝构造 delete pCurrent; return win; } void Ring::countUpTo(int m) { Boy plast = pCurrent; for (int i = m; i>1; i--)//做了m-1轮 { plast = pCurrent; pCurrent = plast->next();//到下一个人 } pCurrent->leave(plast);//输出淘汰编号,让淘汰者上一个指针指向下一个的首地址 delete pCurrent; pCurrent->next(); } Ring::~Ring(){}; //Jose.h #pragma once #include"Boy.h" #include"Ring.h" #include<iostream> using namespace std; class Jose { public: Jose(int boys, int interval);//boys是小孩总数,interval是间隔数 ~Jose(); Boy gameBegin(); void nextcompetition(); private: int numberofboys; int m; Ring* ring;//含有其他类对象是啥方法? Boy* win; }; //Jose.cpp #include"Boy.h" #include"Jose.h" #include"Ring.h" #include<iostream> using namespace std; Jose::Jose(int boys, int interval) { numberofboys = boys; m = interval; ring = new Ring(boys); win = NULL; } Jose::~Jose() { delete ring; delete win; } Boy Jose::gameBegin() { if (!win) { win = new Boy(ring->getwinner(m)); } else { cout << "已进行过比赛结果,若要重复比赛,请将win调为空指针"; } return *win; } void Jose::nextcompetition() { this->win = NULL; } //main.cpp #include"Boy.h" #include"Jose.h" #include"Ring.h" #include<iostream> using namespace std; void main() { cout << "请输入参加小孩个数:"; int n; cin >> n; cout << endl; cout << "请输入间隔数:"; int m; cin >> m; cout << endl; Jose one(n, m); cout << "第一场胜利者:"; one.gameBegin().print(); system("pause"); }访问this指针报错
很抱歉,由于我是一个语言模型,无法直接运行代码,也无法确定你的具体报错信息。但是,根据你提供的代码,可能是以下几种情况:
1. 在类的成员函数中访问未初始化的指针变量。
2. 在使用指针变量时,没有进行空指针判断。
3. 在访问成员变量时,使用了错误的指针。
请检查代码,尤其是和指针相关的部分,查找问题所在。
阅读全文