#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 15:05:49 浏览: 88
这段代码有一些问题:
1. 在 Ring 类中,构造函数和析构函数的实现都没有被写出来,只有声明,这会导致编译错误。
2. 在 Ring 类的构造函数中,变量 n 没有被定义,所以 Ring(n) 会报错。
3. 在 Ring 类的 gethonorees 函数中,no1、no2、no3 三个对象都是在函数内部定义的局部变量,函数返回后就会被销毁,所以在函数外部调用这个函数时,no1、no2、no3 对象的值是未定义的。
函数重载的问题应该是代码中没有体现出来的,如果有,也需要具体看报错信息才能确定原因。
相关问题
publicStructAW100.h:7:9: warning: unterminated '#pragma pack (push, ...)' at end of file
这个警告是由于在头文件 publicStructAW100.h 的最后一个 #pragma pack(push, ...) 没有匹配的 #pragma pack(pop) 导致的。#pragma pack 是用来告诉编译器如何对齐结构体的,push 表示保存当前的对齐方式,pop 表示恢复之前的对齐方式。在头文件中使用 #pragma pack(push, ...) 时,一定要保证匹配的 #pragma pack(pop) 出现在相应的位置,否则会出现警告或错误。你可以检查一下 publicStructAW100.h 的最后是否有 #pragma pack(pop)。如果没有,可以在最后添加上去解决这个警告。
./common.h:12:10: error: the current #pragma pack alignment value is modified in the included file [-Werror,-Wpragma-pack] #include "def_struct.h" ^ ./def_struct.h:8:9: note: previous '#pragma pack' directive that modifies alignment is here #pragma pack(1) ^ common.c:6:10: error: the current #pragma pack alignment value is modified in the included file [-Werror,-Wpragma-pack] #include "common.h" ^ ./def_struct.h:8:9: note: previous '#pragma pack' directive that modifies alignment is here #pragma pack(1)
这是一个编译错误,出现这个错误的原因是在头文件中使用了 #pragma pack(n) 指令来指定结构体成员的对齐方式,但是在包含该头文件的源文件中也使用了 #pragma pack(m) 指令指定不同的对齐方式,导致冲突。
解决这个问题的方法是在头文件中使用 #pragma pack(push, n) 指令将当前的对齐方式压入栈中,然后在结尾处使用 #pragma pack(pop) 指令恢复原来的对齐方式。这样可以保证头文件中的对齐方式不会影响到包含该头文件的源文件。
正确的写法如下:
```
#ifndef DEF_STRUCT_H
#define DEF_STRUCT_H
#pragma pack(push, 1)
// 定义结构体
#pragma pack(pop)
#endif // DEF_STRUCT_H
```