-
question on classes
ok im working with classes trying to figure them out i have the idea down you make a class to create objects like int,string etc. to make your variable types and stuff. so i copied this program out of the book and it compiles fine no errors however when i goto execute it i get the following errors and any explanation on the syntax of this would be appreciated as well cant seem to grasp that part to well either:
--------------------Configuration: displaydate - Win32 Debug--------------------
Linking...
displaydate.obj : error LNK2001: unresolved external symbol "public: void __thiscall date::assigndate(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,
class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?assigndate@date@@QAEXV?$basic_string@DU?$char_tr aits@D@std@@V?$allocator@D@2@@std@@00@Z)
Debug/displaydate.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
displaydate.exe - 2 error(s), 0 warning(s)
Code:
#include <string>
using namespace std;
class date
{
public:
//intializes variables
date();
//assigns program values to variables
void assigndate(string,string,string);
//returns formatted date
string getdate();
private:
string month;
string day;
string year;
};
//pmplentation section
date::date()
{
//constructor
month = "";
day = "";
year = "";
} // end of assigndate function
string date::getdate()
{
return month + "/" + day + "/" + year;
}//end of getdate function
Code:
#include <iostream>
#include <string>
#include "date.h"
using namespace std;
int main()
{
//create object
date dateobj;
//declare variables
string hiremonth = "";
string hireday = "";
string hireyear = "";
//get month day and year
cout << "Enter the month: ";
getline(cin,hiremonth);
cout << "Enter the day: ";
getline(cin,hireday);
cout << "Enter the year: ";
getline(cin,hireyear);
//set the date
dateobj.assigndate(hiremonth,hireday,hireyear);
//display the date
cout << "Employee hire date " << dateobj.getdate() << endl;
return 0;
}
-
Code:
#include <string>
using namespace std;
class date
{
public:
//intializes variables
date();
//assigns program values to variables
void assigndate(string,string,string);
//returns formatted date
string getdate();
private:
string month;
string day;
string year;
};
//pmplentation section
date::date()
{
//constructor
month = "";
day = "";
year = "";
} // end of assigndate function
string date::getdate()
{
return month + "/" + day + "/" + year;
}//end of getdate function
Code:
#include <iostream>
#include <string>
#include "date.h"
using namespace std;
int main()
{
//create object
date dateobj;
//declare variables
string hiremonth = "";
string hireday = "";
string hireyear = "";
//get month day and year
cout << "Enter the month: ";
getline(cin,hiremonth);
cout << "Enter the day: ";
getline(cin,hireday);
cout << "Enter the year: ";
getline(cin,hireyear);
//set the date
dateobj.assigndate(hiremonth,hireday,hireyear);
//display the date
cout << "Employee hire date " << dateobj.getdate() << endl;
return 0;
}
You are trying to call a function that you haven't coded yet it seems. You have a declaration in the class, but no actual code for that function anywhere.
-
In your first file, you have the declaration of
void assigndate(string,string,string);
in the class but you don't have a implementation/definition of it.
Notice how date() and getdate() both have a declaration above and a definition below.
-
yes i seemed to of overlooked that somehow lol
Code:
void date::assigndate(string m, string d, string y)
{
month = m;
day = d;
year = y;
}
ok well with that solved im still confused with this syntax here. but let me see if i got it? or at least some idea to work with.
you create an object class date it then has its own things that it can do in this case display the date using the numbers the user inputs?
so to do this i have to get it to determine out of what is inputed what the day,the month, and the year is? so like any object like cin for example it has how many arguements er (var,var2,var3,etc) it can take.
i think that is a general round about idea of what it does though the syntax is still confusing? any help on clearing that up would be great!