【发布时间】:2020-03-17 19:17:54
【问题描述】:
这是我的代码。我正在尝试从基类中的派生类创建对象,但它有一些错误。
#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 跳过 ==========
请帮助我。谢谢。
【问题讨论】:
-
你不能使用那里的类,因为在你使用它们时它们还没有被定义。您应该将这些函数的定义从声明中分离出来,并将它们定义在您要使用的类下面。
-
一个类应该负责一件事。你所有的课程都做得太多了。您正在混合用户输入和逻辑。基类通常不应该知道它的派生类。修复你的代码需要大量的重构,希望你很幸运,并且有人可以在这里看到更大的画面
标签: c++ inheritance