Reputation: 2135
I am a beginner with OOP->I have a class Date with 3 private variable members and a should print the date in 2 ways:
The following code it gives the error:
date.obj : error LNK2019: unresolved external symbol "public: __thiscall Date::Date(void)" (??0Date@@QAE@XZ) referenced in function "public: void __thiscall Date::printDate(void)" (?printDate@Date@@QAEXXZ) What i am doing wrong? date.h
#include<iostream>
#include<string>
#ifndef DATE_H
#define DATE_H
class Date
{
private:
int day;
int month;
int year;
public:
Date();
Date(int d, int m, int y)
{
day=d;
month=m;
year=y;
}
int getDay() const {return day;}
int getMonth() const {return month;}
int getYear() const {return year;}
void printDate(void);
};
#endif
date.cpp
#include"date.h"
#include<iostream>
#include<string>
const int NR=12;
void Date::printDate()
{
Date newDate;
std::string Months[]={"January","February", "March" , "April", "May", "June", "July", "August", "September", "Octomber", "November", "December"};
int position;
std::string month;
position=newDate.getMonth();
for(int i=0;i<NR;i++)
{
if(i==position)
{
month=Months[i];
}
}
std::cout<<month<<" "<<newDate.getDay()<<" "<<newDate.getYear()<<std::endl;
}
main.cpp
#include "date.h"
#include <iostream>
int main()
{
int d;
int m;
int y;
std::cout<<"Enter day: ";
std::cin>>d;
std::cout<<"Enter month: ";
std::cin>>m;
std::cout<<"Enter years: ";
std::cin>>y;
Date newDate(d,m,y);
std::cout<<newDate.getMonth()<<"/"<<newDate.getDay()<<"/"<<newDate.getYear()<<std::endl;
newDate.printDate();
}
Upvotes: 0
Views: 299
Reputation: 74
Hmmmm, i think you might also be misunderstanding a lot of things here. Firstly, member data: Inside of your printDate() function you can directly reference the members variables of the date object. Secondly, you don't need that for loop, you can just say
months[position]
Thirdly, that const global is not the best way of storing the size of that array. If you need to know the size of the array you can just call
months.size()
Fourthly, that array of months can be member data so you don't need to declare it every time printDate is called. Lastly, you do not need to include anything in the cpp file that is already included in the header file.
So your new class should look like this:
Date.h:
#include<iostream>
#include<string>
#ifndef DATE_H
#define DATE_H
class Date
{
private:
int day;
int month;
int year;
const std::string months[] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
public:
Date(){}
Date(int d, int m, int y)
int getDay() const {return day;}
int getMonth() const {return month;}
int getYear() const {return year;}
void printDate(void);
};
#endif
then Date.cpp:
#include "Date.h"
Date::Date(int d, int m, int y)
{
day=d;
month=m;
year=y;
}
void Date::printDate(void)
{
std::cout<<months[month]<<" "<<day<<" "<<year<<std::endl;
}
I know most of this seems pointless for what you want to do but you can quickly get yourself in trouble with C++ because it just lets you do whatever you want for the most part so it is best to learn good coding practices and to learn the power of a language right when you start with it.
Upvotes: 1
Reputation: 21863
The error is quite clear: you declared constructors for your Date
class but didn't define them inside the cpp file.
You should add definitions for these constructors. They could just look like this:
Date::Date() {}
or maybe
Date::Date() {
d = 1;
m = 1;
y = 1970;
}
which at least wouldn't print nonsense if you call
Date myDate;
myDate.printDate();
EDIT:
As suggested by Mat, you should use constructor initializer lists when you can. Your other constructor which uses parameters would look like this with an initialization list:
Date(int d, int m, int y) :
day(d), month(m), year(y) {}
In your case, your constructor calls the empty constructor on day
, month
and year
and then assigns values to them, whereas when using initialization lists, the Date
constructor calls constructors with parameters for day
, month
and year
.
Upvotes: 3
Reputation: 87959
alestanis is right with the changes you need to make to get rid of the error. But your code is still very wrong. You're clearly struggling with object orientation.
In your printDate
method you should just print out the member variables of your Date class. You should not declare a new variable. Do it like this
void Date::printDate()
{
std::cout<<Months[m]<<" "<<d<<" "<<y<<std::endl;
}
Much simpler code than you wrote.
Upvotes: 0