-
Class files (.h and .cpp
Ok I must be missing something. I'm trying to make a simple class and I keep getting linker errors. I made the .h file and put it in include ( I'm using Borland 5) the .cpp I just put in a folder. I read through some of the threads and they talk about compiling the files but not linking them to create an .obj file. I'm not sure how to do this. The error i'm getting is
[linker error] unresolved external referenced from c:/(where my .cpp is)
any help would be great
-
-
OK its straight out of the book Just trying to get it to work
//DateType.h
//--------------------------------------------------------------------------------
class DateType
{
public:
void Initialize( int newMonth, int newDay, int newYear);
int YearIs() const;
int MonthIs() const;
int DayIs() const;
private:
int year;
int month;
int day;
}; // I put this file in the include folder
//--------------------------------------------------------------------------------
//DateType.cpp
//--------------------------------------------------------------------------------
#include"DateType.h"
void DateType::Initialize(int newMonth, int newDay, int newYear)
{
year=newYear;
month=newMonth;
day=newDay;
}
//---------------------------------------------------------------------------------
int DateType::MonthIs() const
{
return month;
}
//---------------------------------------------------------------------------------
int DateType::YearIs() const
{
return year;
}
//---------------------------------------------------------------------------------
int DateType::DayIs() const
{
return day;
}
// this file I just put in a folder
//---------------------------------------------------------------------------------
//Main
#include"DateType.h"
#include<iostream>
#include<conio>
using namespace std;
int main()
{
DateType today;
today.Initialize(9, 20, 2001);
cout<<"Today is "<<today.MonthIs()<<todayy.DayIs()<<today.YearIs() <<endl;
getch();
return 0;
}
-
try use #include <DataType.h> or #include <DataType> rather than #include "DataType".
The double quote syntax is to be used when the header file is in the same directory as the executable file it is linked to. The angled brackets are for when that isn't the case, although I think they can be used even then. I think the angled brackets tell the linker to look first in the directory of the exe file and if you don't find the header there look beyond the current directory, whereas the double brackets say look just in the directory containing the exe file.
The other choice is to move both the header and cpp file to the same directory as the exe file.
-
If I moved them both into the exe directory how would I tell main to look for it there?
-
either syntax works under those conditions. I think, but the traditional syntax would be to use the double quotes--which often is used to distinquish a user defined header as opposed to a predefined header, although it is really location of files that should distinguish which syntax to use, not origin.
-
filename.h is already included in your cpp file. Therefore, all you have to include is the .cpp file to the main.
-
None of this works I've tried them all in the same file but it still gives me linker errors . Its finding all the files but it just won't link them?