gpt4 book ai didi

c++ - 在基类中创建派生类的对象

转载 作者:行者123 更新时间:2023-11-30 04:42:00 24 4
gpt4 key购买 nike

这是我的代码。我试图从基类中的派生类创建对象,但它有一些错误。

#include <iostream>
#include<Windows.h>
#include <string>
#include <ctime>

using namespace std;

class PrgDevice {
private:
tm startTime;
tm stopTime;
int choice;
int choice1;
char c;
public:
int dateTime() {
cout << "Enter start date and start time: ";
cin >> startTime.tm_mday >> startTime.tm_mon >> startTime.tm_year >> startTime.tm_hour >> startTime.tm_min >> startTime.tm_sec;
cout << "Enter stop date and stop time: ";
cin >> stopTime.tm_mday >> stopTime.tm_mon >> stopTime.tm_year >> stopTime.tm_hour >> stopTime.tm_min >> stopTime.tm_sec;
}

void mainMenu() {
while (choice != 3) {
cout << "Main menu options:";
cout << " 1. Select a device to program (contains a submenu)" << endl;
cout << " 2. Display current status of all devices" << endl;
cout << " 3. Exit" << endl;
cout << "Enter your option => ";
cin >> choice;

if (choice == 1) {
subMenu();
}
else if (choice == 2) {
cout << choice;
}
else {

}
}
system("pause");
}
void subMenu() {
cout << "Select a device:" << endl;
cout << " 1. PVR" << endl;
cout << " 2. Camera DVR" << endl;
cout << " 3. Oven" << endl;
cout << "Enter your option => ";
cin >> choice1;

if (choice1 == 1) {
PVR n1;
}
else if (choice1 == 2) {
DVR n2;
}
else {
Oven n3;
}
}
void newDevice() {
if (c == 'Y' || c == 'y') {
subMenu();
}
else {
mainMenu();
}
}
};

class PVR : public PrgDevice {
private:
int channel;
public:
PVR() {
cout << "Select the channel ==> ";
cin >> channel;
cout << endl;
dateTime();
cout << endl;
cout << "Another device to program Y/N ? => ";
newDevice();
}
};

class DVR : public PrgDevice {
private:
string position;
public:
DVR() {
cout << "Select the position ==> ";
getline(cin, position);
cout << endl;
dateTime();
cout << endl;
cout << "Another device to program Y/N ? => ";
newDevice();
}
};

class Oven : public PrgDevice {
private:
string food;
public:
Oven() {
cout << "What do you want to bake? ==> ";
getline(cin, food);
cout << endl;
dateTime();
cout << endl;
cout << "Another device to program Y/N ? => ";
newDevice();
}
};

int main() {
PrgDevice obj1;
obj1.mainMenu();



system("pause");
return 0;
}

错误如下:

error C2065: 'PVR': undeclared identifier error C2146: syntax error: missing ';' before identifier 'n1' error C2065: 'n1': undeclared identifier error C2065: 'DVR': undeclared identifier error C2146: syntax error: missing ';' before identifier 'n2' error C2065: 'n2': undeclared identifier error C2065: 'Oven': undeclared identifier error C2146: syntax error: missing ';' before identifier 'n3' error C2065: 'n3': undeclared identifier 1>Done building project "Project1.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

请帮帮我。谢谢。

最佳答案

不清楚您为什么要在这里使用继承。本质上,PVR DVROven 这三个对象不需要派生自 PrgDevice。一旦它们不派生,您可以将它们移动到 PrgDevice 之前,以便您可以在那里使用它们。

class PVR {...
};
class DVR {...
};
class Oven {...
};
class PrgDevice {...
};

因为 PVR DVROven 构造函数都做

dateTime();
cout << endl;
cout << "Another device to program Y/N ? => ";
newDevice();

我们可以将其移动到 PrgDevice::submenu 函数中。

void subMenu() { ...
if (choice1 == 1) {
PVR n1;
}
else if (choice1 == 2) {
DVR n2;
}
else {
Oven n3;
}
dateTime();
cout << endl;
cout << "Another device to program Y/N ? => ";
newDevice();
}

但这并不能解决您所有的问题。你继续递归:

  • mainMenu 调用 subMenu
  • submenu 调用 newDevice
  • newDevice 调用 submenunewDevice

接下来需要修复。新设备实际上应该留在子菜单返回 到主菜单。我们通过添加一个 do while 循环来做到这一点

void submenu()
{
do {
cout << "Select a device:" << endl;
...
cout << "Another device to program Y/N ? => ";

cin >> c;
}
while(c == 'Y' || c == 'y');
}

我留下了一个看似有效的版本,其中注释掉了 datetime 函数 here

吃饱了

echo -e "1 1 1 y 2 2\n n 3"

输出

Main menu options: 
1. Select a device to program (contains a submenu)
2. Display current status of all devices
3. Exit
Enter your option => Select a device:
1. PVR
2. Camera DVR
3. Oven
Enter your option => Select the channel ==>

Another device to program Y/N ? => Select a device:
1. PVR
2. Camera DVR
3. Oven
Enter your option => Select the position ==>

Another device to program Y/N ? => Main menu options:
1. Select a device to program (contains a submenu)
2. Display current status of all devices
3. Exit
Enter your option =>

关于c++ - 在基类中创建派生类的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58982013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com