Reputation: 43
I tried to create my own class which contain three variable: day, month and year. And I add two operator to compare. here is my header file and cpp file:
Header:
#ifndef DATE_H
#define DATE_H
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <array>
using namespace std;
class Date {
public:
int day;
int month;
int year;
Date(int m, int d, int y);
bool operator< (const Date &) const;
bool operator== (const Date &) const;
}
#endif
CPP:
#include "stdafx.h"
#include "date.h"
Date::Date(int m, int d, int y)
:day(d),month(m),year(y){}
bool Date::operator< (const Date & d2) const
{
bool result;
if(year<d2.year){
result=true;
}
else if (year==d2.year&&month<d2.month){
result=true;
}
else if (month==d2.month&&day<d2.day){
result = true;
}
else{
result = false;
}
return result;
}
bool Date::operator== (const Date & d2) const
{
return (year==d2.year)&&(month==d2.month)&&(day==d2.day);
}
The error is
error C2533: 'Date::{ctor}' : constructors not allowed a return type
Thank you for the help!
Upvotes: 1
Views: 175
Reputation: 145429
The class definition lacks a semicolon at the end.
Other comments:
To avoid name collisions (e.g. with std::distance
), do not put a using namespace std;
in the global namespace in a header.
<stdafx.h>
is a non-standard header, defined in your Visual Studio project, which makes that code dependent on Visual Studio. You can avoid it by turning off "precompiled headers" in the project settings.
Upvotes: 2