Class files (.h and .cpp

This is a discussion on Class files (.h and .cpp within the C++ Programming forums, part of the General Programming Boards category; Ok I must be missing something. I'm trying to make a simple class and I keep getting linker errors. I ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    11

    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

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Some code would help....

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    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:ayIs() 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;
    }

  4. #4
    Unregistered
    Guest
    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.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    If I moved them both into the exe directory how would I tell main to look for it there?

  6. #6
    Unregistered
    Guest
    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.

  7. #7
    °Amo fķtbol!
    Join Date
    Dec 2001
    Posts
    2,136
    filename.h is already included in your cpp file. Therefore, all you have to include is the .cpp file to the main.

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get .cpp and .h files to communicate...
    By yaya in forum C++ Programming
    Replies: 6
    Last Post: 11-24-2008, 11:45 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 11:21 AM
  3. .cpp and .h files - organization
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2006, 10:40 PM
  4. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 05:37 PM
  5. need help with my .h, .cpp, and a class
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 04-27-2002, 12:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21