Hi all;
I'm new to C++ and OOP.

My problem is:

I ' ve created the header file time1.h and included it in my project but when I tried to compile it gave me linker errors in VC++ 6.0.

Here is the code:

My header file time1.h

#ifndef TIME1_H
#define TIME1_H

class Time{

public :

    Time();
    void SetTime(int,int,int);
    void showTime();

private :

    int second,minute,hour;

};

#endif

time1.cpp

#include <iostream>
#include "time1.h"

using namespace std;

Time :: Time()
{
    second = minute = hour = 0;
}

void Time :: SetTime(int h,int m,int s)
{
    if((h >= 0 && h < 24) && (m >= 0 && m < 60) && (s >= 0 && s < 60))
    {
        hour = h;
        minute = m;
        second = s;
    }

    else
    {

        hour = minute = second = 0;
    }
}

void Time :: showTime()
{
    cout<<(hour < 10 ? "0" : "")<<hour<<":"<<(minute < 10 ? "0" : "")<<minute<<":"<<(second < 10 ? "0" : "")<<second<<endl;
}

And my main.cpp

#include <iostream>
#include "time1.h"

using namespace std;

int main()
{

    Time time1,time2;

    time1.SetTime(12,58,9);

    time1.showTime();

    time2.SetTime(56,1,2);

    time2.showTime();

    return 0;

}

Here is the given errors :

In VC++

Linking...
Cpp2.obj : error LNK2001: unresolved external symbol "public: void __thiscall Time::showTime(void)" (?showTime@Time@@QAEXXZ)
Cpp2.obj : error LNK2001: unresolved external symbol "public: void __thiscall Time::SetTime(int,int,int)" (?SetTime@Time@@QAEXHHH@Z)
Cpp2.obj : error LNK2001: unresolved external symbol "public: __thiscall Time::Time(void)" (??0Time@@QAE@XZ)
Debug/Cpp2.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.

Recommended Answers

All 3 Replies

Did you add time1.cpp to your project?

It compiled and linked ok for me with that compiler. Must be the way you set up the project. First, create a new empty console project. Copy the *.cpp and *.h files into the project folder, then use menu Project --> Add To Project --> Files, and select the files you added above.

Thanks for your helps...
I've solved the problem with your suggestions...

My problem was:

I've copied the three files in the same directory and tried to compile "main.cpp"

After I've added to these files in my project it worked...

Thanks again...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.