Reputation: 1059
First post so be gentle with me...
I am trying to implement a derived class and am having problems and no matter what i try am getting compilation errors. I am sure it is something simple i have missed but am very new to this and all my research has given me no help (or i have just missed it cause i dont know what I am doing!).
This is my header file:
#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
This is an excerpt from the .cpp file that i am trying to implement:
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(){
}
At present I am getting the following error:
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
If i change things around in the .cpp file i get different errors - obviously.
Basically i really don't know how to do this and have struggled trying to find the correct way...
Anyway if someone can point me in the right direction it would be greatly appreciated...
Thanks
Upvotes: 1
Views: 15367
Reputation: 96810
The member-initializer list takes a comma separated list of data members and initializes it with the parameter you supply. int x
is not a value, it's actually a syntax error in this case. However, x
would be a value.
WeekDay::WeekDay(int y, int m, int d, int h, int min, int s)
: DateTime(y, m, d, h, min, s)
Upvotes: 0
Reputation: 33046
The offending line is this one:
: DateTime(int y, int m, int d, int h, int min, int s),{
First you have a trailing comma before the ,
, then you should remove the int
you put in this line: you are not defining the superclass constructor, but calling it. Think of this as an ordinary (unbound) function: you call functions with f(x)
, not f(int x)
.
Upvotes: 0
Reputation: 110658
You're using the member initialization list incorrectly. If you want to pass the values of the arguments passed to the WeekDay
constructor to the constructor of DateTime
, you need to remove the types:
WeekDay::WeekDay(int y, int m, int d, int h, int min, int s)
: DateTime(y, m, d, h, min, s) {
}
Consider it like calling a function (because actually, that's what it's doing). If you have a function like void foo(int x);
, you don't call it by writing foo(int 5)
, do you?
Upvotes: 10
Reputation: 5699
this line:
: DateTime(int y, int m, int d, int h, int min, int s),{
should be:
: DateTime(y, m, d,h, min,s) {
Upvotes: 0
Reputation: 1033
: DateTime(int y, int m, int d, int h, int min, int s),{
remove all the "int"s from that line.
Upvotes: 0