I'm writing a simple program with seperate header, implimintation, and main files. I understand the concept of objects and classes. I just think I don't understand the syntax or which files to include can anyone see what is causeing my error? I think it has to do with the includes but I've tried different ways and can't get rid of the error.
Thanks and I hope it's not a problem that I posted all the code. Sorry if it is. Let me know and I won't do it next time.
1 2 3 4
error:
dayOfWeekImp.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall dayOfWeek::getDay(void)" (?getDay@dayOfWeek@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in dayOfWeekMain.obj
#include <iostream>
#include <string>
#include "dayOfWeek.h"
#include "dayOfWeekImp.cpp"
using std::cin;
using std::cout;
using std::string;
using std::endl;
int main()
{
//declare object (weekDay)
//and string variable for testing
dayOfWeek weekDay;
string today;
//call the setDay() function
weekDay.setDay("Mon");
//get the day with getDay() function
today = weekDay.getDay();
//Test the getDay() function
cout << "Testing the getDay() function. . . " << endl;
cout << "The function, getDay() returned " << today << " as the day." << endl;
//call printDay to print the private data member (day)
weekDay.printDay();
//hold screen
system("pause");
//end main
return 0;
}
//Implimentation file for dayOfWeek class functions
#include <iostream>
#include <string>
#include "dayOfWeek.h"
using std::cout;
using std::cin;
using std::string;
using std::endl;
void dayOfWeek::setDay(string d)
{
day = d;
}
string dayOfWeek::getDay()
{
return day;
}
void dayOfWeek::printDay()
{
cout << "The day is " << day << endl;
}
Thanks Return 0. That was so simple I feel kinda dumb now. So I guess the explanation is that because the functions are being called as part of the class "dayOfWeek" there's no need to include the implementation file in main. It's all really part of the class which is already included with the dayOfWeek.h file? Anyway, thanks for pointing that out. It all works perfectly now.