【问题标题】:Parametric Construction Via Initializer List通过初始化列表进行参数构造
【发布时间】:2014-02-03 07:06:20
【问题描述】:

我得到了以下代码来开发我自己的约会簿应用程序:

#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 构造函数的一些参数来构造date 成员它包含在Appointment 文件中的Appointment 类:

Appointment(string description, int month, int day, int yr, int hr, int min)

这是我到目前为止在Appointment.h vis-a-vis 中所做的,以请求的方式定义构造函数:

#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.hDate.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

日期.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;
}

【问题讨论】:

    标签: c++ class c++11 virtual header-files


    【解决方案1】:

    只需从 Appointment 构造函数中删除 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
    

    【讨论】:

      【解决方案2】:

      构造函数定义错误:

      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;。这不是命名空间的用途。

      当您对向量使用动态分配时,会出现另一个潜在问题:

      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_ptrstd::shared_ptr(按此顺序)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-15
        • 2016-07-19
        • 1970-01-01
        • 2012-03-19
        • 1970-01-01
        • 1970-01-01
        • 2017-06-13
        相关资源
        最近更新 更多