这是我的代码。我正在尝试从基类的派生类创建对象,但是它有一些错误。
#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;
}
以下是错误:
错误C2065:“ PVR”:未声明的标识符 错误C2146:语法错误:缺少';'在标识符“ n1”之前 错误C2065:“ n1”:未声明的标识符 错误C2065:“ DVR”:未声明的标识符 错误C2146:语法错误:缺少';'在标识符“ n2”之前 错误C2065:“ n2”:未声明的标识符 错误C2065:“烤箱”:未声明的标识符 错误C2146:语法错误:缺少';'在标识符“ n3”之前 错误C2065:“ n3”:未声明的标识符 1>完成的建筑项目“ Project1.vcxproj”-失败。 ===========构建:0成功,1失败,0最新,跳过0 ==========
请帮助我。谢谢。
答案 0 :(得分:1)
目前还不清楚为什么在这里使用继承。本质上,三个对象PVR
DVR
和Oven
不需要从PrgDevice
派生。一旦没有派生出来,您可以将它们移到PrgDevice
之前,以便在那里使用它们。
class PVR {...
};
class DVR {...
};
class Oven {...
};
class PrgDevice {...
};
由于PVR
DVR
和Oven
的构造函数都这样做
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
呼叫submenu
或newDevice
接下来需要修复。新设备实际上应该停留在submenu
或返回回到主菜单。为此,我们添加了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 =>
答案 1 :(得分:0)
在使用对象类型之前,需要先定义它们。与其将代码内联到类定义中,不如将它们分隔在代码的顶部(或包含在其中),然后当您真正开始使用它们时,编译器就会知道您在做什么。
class A {
// Define stuff, but don't inline your methods
}
class B: public A {
// Define stuff, but don't inline your methods
};
void A::subMenu() {
// All your code
}