[英]Parametric Construction Via Initializer List
我得到了以下代碼來開發自己的約會簿應用程序:
#include<iostream>
#include<string>
#include<vector>
#include "Appointment.h"
#include "OneTime.h"
#include "Daily.h"
#include "Monthly.h"
#include "Yearly.h"
using namespace std;
void checkAppointments(vector<Appointment*>& apptbook){
// STATEMENTS
}
void addAppointment(vector<Appointment*>& apptbook){
// STATEMENTS
}
int main(){
vector<Appointment*> apptbook;
char option;
do {
cout << "********** Appointment Book Application ************" << endl<< endl;
cout << "(a) See all appointments on a given day." << endl;
cout << "(b) Add an appointment." << endl << endl;
cout << "Enter an option or 'q' to quit: ";
cin >> option;
switch(option){
case 'a':
checkAppointments(apptbook);
break;
case 'b':
addAppointment(apptbook);
break;
case 'q':
break;
default:
cout << "You entered an invalid option. Try again!";
}
cout << endl;
}
while(option != 'q');
// Cleaning up
for(int i = 0; i < apptbook.size(); i++){
delete apptbook[i];
}
apptbook.clear();
system("PAUSE");
return 0;
}
我被要求使用下面指定的Appointment
構造函數的一些參數來構造Appointment.h
文件中包含的Appointment
類的date
成員:
Appointment(string description, int month, int day, int yr, int hr, int min)
defining the constructor in the requested way: 以下是我在迄今所做Appointment.h
定義所請求的方式構造:
#ifndef APPOINTMENT_H
#define APPOINTMENT_H
#include<string>
#include<sstream>
#include "Date.h"
using namespace std;
class Appointment{
public:
Appointment(string description, int month, int day, int yr, int hr, int min);
Date date;
private:
int hour;
int minute;
string convertInt( int number ) const;
};
Appointment(string description, int month, int day, int yr, int hr, int min) : public date(month, day, yr)
{
this->description = description;
}
string Appointment::convertInt( int number ) const
{
stringstream ss;
ss << number;
return ss.str();
}
#endif
我想我的問題是這樣的:“如何使用Appointment
的某些參數來構造其初始值列表的日期成員?” 這個概念對我來說是陌生的,我遇到了麻煩。 這是Date.h
和Date.cpp
:
日期.h
#ifndef DATE_H
#define DATE_H
#include<string>
using namespace std;
class Date{
public:
Date(int month, int day, int year);
int getMonth() const;
int getDay() const;
int getYear() const;
private:
int month;
int day;
int year;
};
#endif
Date.cpp
#include "Date.h"
#include<string>
using namespace std;
Date::Date(int month, int day, int year) {
this->month = month;
this->day = day;
this->year = year;
}
int Date::getMonth() const{
return month;
}
int Date::getDay() const{
return day;
}
int Date::getYear() const{
return year;
}
只需從約會構造器中刪除public關鍵字,為什么將函數定義放在頭文件中?
#ifndef APPOINTMENT_H
#define APPOINTMENT_H
#include<string>
#include<sstream>
#include "Date.h"
using namespace std;
class Appointment{
public:
Appointment(string description, int month, int day, int yr, int hr, int min);
Date date;
private:
int hour;
int minute;
string convertInt( int number ) const;
};
Appointment(string description, int month, int day, int yr, int hr, int min) : date(month, day, yr)
{
this->description = description;
}
string Appointment::convertInt( int number ) const
{
stringstream ss;
ss << number;
return ss.str();
}
#endif
構造函數定義錯誤:
Appointment(string description, int month, int day, int yr, int hr, int min) : public date(month, day, yr)
{
this->description = description;
}
您應該添加Appointement::
作為前綴,並在其中刪除public
關鍵字:
Appointment::Appointment(string description, int month, int day, int yr, int hr, int min) : date(month, day, yr)
{
this->description = description;
}
另外,請學習使用std::
前綴,而不是using namespace std;
盲目添加using namespace std;
。 那不是命名空間的用途。
在以下情況下對向量使用動態分配時,會出現另一個潛在的問題:
vector<Appointment*> apptbook;
沒有必要。 您可以使用:
std::vector<Appointment> apptbook;
並像這樣在addAppointment
填充它:
void addAppointment(std::vector<Appointment>& apptbook){
// calculate arguments
apptbook.emplace_back(description, month, day, yr, hr, min);
}
這樣,您就可以擺脫無用的東西:
for(int i = 0; i < apptbook.size(); i++){
delete apptbook[i];
}
apptbook.clear();
共。
但是,如果您確實需要在這種情況下使用動態分配,則至少要學會使用std::unique_ptr
或std::shared_ptr
(按此順序)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.