我有一个在大学时做的类项目,“有效”,但构建不正确。
它有一个 Date 类和一个 Person 类。
在我的“工作”代码中,所有类数据(构造函数、成员和函数)都包含在每个类的单个头文件中(每个类都有一个单独的文件)。
程序将文本文件数据(人物、总统和 Actor )加载到 vector 中,对数据进行排序、过滤并在控制台上打印,然后将其保存到文件中。
在 Person 类构造函数(和函数)中,Date 类被实例化为“生日”对象。然后在程序中使用生日。
我遇到的问题是这样的:
如果所有类代码都在每个类的头文件中,我可以轻松地在 Person 构造函数和成员中实例化“Date”,并且我将 Date 作为 Person 的“父”类。但如果我不做这两件事,那么“Person”就不知道有关“Date”的任何信息,并且无法实例化 Date 中的任何内容。
如果我在 Person.h 中使用 #include "Date.h",这会产生更多问题并且也不起作用。
当然,“Date”不是“Person”的正确父类,但这是我可以破解代码以使其正常工作的唯一方法,哈哈。当我几年前在大学第一次编码时,我从未弄清楚如何正确划分类代码以便它能够编译和运行。头文件中包含所有类代码的“工作代码”是我的技巧。我想学习如何以正确的方式做到这一点。
我在下面粘贴了最简单的示例,说明什么“有效”,什么无效。任何使此代码能够与分为头文件和 .cpp 文件的类一起使用的提示将不胜感激。
什么有效:
class Date {
private:
int month;
int day;
int year;
public:
Date::Date() {}
virtual ~Date() {}
Date( int aMonth, int aDay, int aYear ) {
this->month = aMonth;
this->year = aYear;
this->day = aDay;
}
// "Getter" functions for the Date class
int getMonth(){return this->month;}
int getYear() {return this->year;}
int getDay() {return this->day;}
// "Setter" functions for the Date class
void setDay( int aDay ){ this->day = aDay; }
void setMonth( int aMonth ) { this->month = aMonth; }
void setYear( int aYear ) { this->year = aYear; }
};
class Person : public Date{
private:
string title;
string firstName;
string lastName;
Date birthDate;
public:
Person::Person() {}
Person(string title, string firstName, string lastName, Date birthDay);
virtual ~Person() {}
//"Getter" functions for the Person class
string getTitle() { return title; }
string getFirstName() { return firstName; }
string getLastName() { return lastName; }
Date getBirthDay() { return birthDate; }
//"Setter" functions for the Person class
void setTitle(string Title) { this->title = Title; }
void setFirstName(string fName) { this->firstName = fName; }
void setLastName (string lName) { this->lastName = lName; }
void setBirthday (Date aBirthday) { this->birthDate = aBirthday; }
};
什么不起作用(无法编译):
日期.h
class Date {
private:
int month;
int day;
int year;
public:
Date();
virtual ~Date() {}
Date( int aMonth, int aDay, int aYear );
//Getters and setters
int getMonth();
int getYear() ;
int getDay();
void setDay( int aDay );
void setMonth( int aMonth ) ;
void setYear( int aYear ) ;
};
日期.cpp
#include "Date.h"
Date::Date() {}
Date::Date( int aMonth, int aDay, int aYear ) {
this->month = aMonth;
this->year = aYear;
this->day = aDay;
}
int Date::getMonth(){return this->month;}
int Date::getYear() {return this->year;}
int Date::getDay() {return this->day;}
//"Setter" functions for the Date class
void Date::setDay( int aDay ){ this->day = aDay; }
void Date::setMonth( int aMonth ) { this->month = aMonth; }
void Date::setYear( int aYear ) { this->year = aYear; }
Person.h
#include <string>
class Person{
private:
string title;
string firstName;
string lastName;
Date birthDate;
public:
//Person::Person() {}
Person(string title, string firstName, string lastName, Date birthDay);
//"Getter" functions for the Person class
string getTitle() { return title; }
string getFirstName() { return firstName; }
string getLastName() { return lastName; }
Date getBirthDay() { return birthDate; }
//"Setter" functions for the Person class
void setTitle(string Title) { this->title = Title; }
void setFirstName(string fName) { this->firstName = fName; }
void setLastName (string lName) { this->lastName = lName; }
void setBirthday (Date aBirthday) { this->birthDate = aBirthday; }
};
Person.cpp
#include "Person.h"
#include <iostream>
using namespace std;
//Person class constructor with 0 arguments
Person::Person(string atitle, string afirstName, string alastName, Date abirthDay) {
title = atitle,
firstName = afirstName,
lastName = alastName,
birthday = abirthDay)
}
//"Getter" functions for the Person class
string Person::getTitle() { return title; }
string Person::getFirstName() { return firstName; }
string Person::getLastName() { return lastName; }
Date Person::getBirthDay() { return birthDate; }
//"Setter" functions for the Person class
void Person::setTitle(string Title) { this->title = Title; }
void Person::setFirstName(string fName) { this->firstName = fName; }
void Person::setLastName (string lName) { this->lastName = lName; }
void Person::setBirthday (Date aBirthday) { this->birthDate = aBirthday; }
操作系统说明
我在 Windows 7 PC 上使用 MS Visual Studio 2012 Update 4。
最佳答案
为了防止重新定义错误,请在头文件中添加包含防护。为了便于携带,请使用:
#if !defined(THE_HEADER_NAME_H)
#define THE_HEADER_NAME_H
// Usual code and declarations here.
#endif
在 Visual Studio 中,您可以使用:
#pragma once
// Usual code and declarations here.
在 header 中,不要写入(在全局范围级别)
using namespace XXXXX
因为这将导致包括您的文件在内的所有文件也使用他们可能不希望强加给它们的 namespace XXXXX。如果您使用命名空间中的类型,请使用命名空间键入整个类型名称,因此请使用:
std::string getTitle()
(在 cpp 文件中,您可以在任何 #includes 之后添加“using namespace XXXXX”)
关于c++ - 我正在尝试学习如何正确分离类头和代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28200438/