pel*_*gos 0 c++ inheritance class
第一篇帖子对我很温柔......
我正在尝试实现派生类,并且遇到问题,无论我尝试编译错误.我确信这是一个简单的我错过了,但我很新,我的所有研究都没有给我任何帮助(或者我错过了它因为我不知道我在做什么!).
这是我的头文件:
#ifndef WEEKDAY_H
#define WEEKDAY_H
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
class DateTime{
public:
DateTime(int y, int m, int d, int h = 0, int min = 0, int s = 0);
void display();
protected:
string get_string_component(char option, tm* dateStruct);
int get_year_days(tm* dateStruct);
struct tm DTstruct;
private:
bool validate_data( int y, int m, int d, int h, int min, int s);
};
class WeekDay : public DateTime{
public:
WeekDay(int y, int m, int d, int h = 0, int min = 0, int s = 0);
void display();
};
#endif
Run Code Online (Sandbox Code Playgroud)
这是我试图实现的.cpp文件的摘录:
WeekDay::WeekDay(int y, int m, int d, int h, int min, int s)
: DateTime(int y, int m, int d, int h, int min, int s),{
}
void WeekDay::display(){
}
Run Code Online (Sandbox Code Playgroud)
目前我收到以下错误:
weekday.cpp: In constructor 'WeekDay::WeekDay(int, int, int, int, int, int)':
weekday.cpp:58:13: error: expected primary-expression before 'int'
weekday.cpp:58:20: error: expected primary-expression before 'int'
weekday.cpp:58:27: error: expected primary-expression before 'int'
weekday.cpp:58:34: error: expected primary-expression before 'int'
weekday.cpp:58:41: error: expected primary-expression before 'int'
weekday.cpp:58:50: error: expected primary-expression before 'int'
weekday.cpp:60:1: error: expected identifier before '{' token
Run Code Online (Sandbox Code Playgroud)
如果我在.cpp文件中改变了一些东西,我会得到不同的错误 - 显然.
基本上我真的不知道该怎么做,并努力寻找正确的方法......
无论如何,如果有人能指出我正确的方向,我将不胜感激......
谢谢
您错误地使用了成员初始化列表.如果要将传递给WeekDay构造函数的参数的值传递给构造函数DateTime,则需要删除类型:
WeekDay::WeekDay(int y, int m, int d, int h, int min, int s)
: DateTime(y, m, d, h, min, s) {
}
Run Code Online (Sandbox Code Playgroud)
考虑它就像调用一个函数(因为实际上,这就是它正在做的事情).如果你有类似的功能void foo(int x);,你不要通过写作来调用它foo(int 5),是吗?