-1

An error appeared while I was testing the driver code. The compiler raised such an error:

undefined reference to Time::Time(int, int, int)

AND

undefined reference to Time::setTime(int, int, int)

Why did the Code::Blocks IDE raise this error? Is it due to the main or any declaration or definition errors? Please point out my error.


TIME.H

# ifndef TIME1_H
# define TIME1_H

class Time
{
public:
    Time(int, int, int); // Default constructor
    void setTime(int, int, int); // Set hour, minute, second
    void printMilitary();
    void printStandard();

private:
    int hour;
    int minute;
    int second;

};
# endif

TIME.CPP

# include <iostream>
# include "TIME.H"
using namespace std;

// Implementing the functions

void Time::setTime(int hour, int minute, int second)
{
    setTime(hour, minute, second);
}

void Time::printMilitary()
{
    if(hour >= 1200 && hour <= 2359)
    {
        hour-1200;
        cout << hour << ":" << minute;
    }

    if(hour < 1200 && hour >= 100)
    {
        cout << hour << ":" << minute << ":" << second << " AM";
    }
    else
    {
         cout << hour << ":" << minute << ":" << second;
    }
}

void Time::printStandard()
{
    if(hour)
    {
        cout << hour << ":" << minute << ":" << second;
    {
    }
}

MAIN.CPP

# include <iostream>
# include "TIME.H"
using namespace std;

int main()
{
    Time a(0, 0, 0);
    a.setTime(12, 23, 18);
    return 0;
}

Are there any links to help?


When I tried the working demo posted by @Annop Raya, everything was fine when I ran the code. But when I try this code in Code::Blocks, it raised a few errors at the implementation file. The errors are shown below:

Line 6

error: 'Time' does not name a type; did you mean 'time'?

Line 11

error: 'Time' has not been declared

Line 17

error: 'Time' has not been declared

Line 17

In function 'void printMilitary()':

Line 19

'hour' was not declared in this scope

All the private member variables gets the same error, what's wrong?


In the main file, the compiler still returns the same error, so is there a solution for this?

1 Answers1

3

The problem is that you've not implemented the constructor Time::Time(int, int, int) in your source file.

To solve this just add a definition for Time::Time(int, int, int).

TIME.CPP

//implmentation for constructor that was not there before
Time::Time(int phour, int pminute, int psecond): hour(phour), minute(pminute), second(psecond)
{
    
}
//other code 

Working demo

Jason
  • 36,170
  • 5
  • 26
  • 60
  • 1
    It is not the only cause of the error messages though. Probably `TIME.CPP` isn't being compiled at all either. – user17732522 Jun 21 '22 at 09:44
  • 1
    *One* of the problems... – Paul Sanders Jun 21 '22 at 09:57
  • @PaulSanders No, read the title again. It says *"Undefined Reference to Time::Time(int,int,int)"* and in the given example there is no definition for `Time::Time(int, int, int)` but there is definition for other members. So it is natural to answer accordingly. Ofcourse there may be other problem but i answer according to the question being asked. – Jason Jun 21 '22 at 10:01
  • 1
    @user17732522 Maybe. But the title asks specifically asks about the constructor and there is missing definition for the constructor. So i answered accordingly. Ofcourse there may be other problem with the code which i haven't covered as i want to address only the question being asked. – Jason Jun 21 '22 at 10:03
  • 1
    _I want to address only the question being asked_ That is not a philosopy I agree with. And if you read the body of the question, there are two linker errors (with two different underlying causes), not one. – Paul Sanders Jun 21 '22 at 10:51
  • @PaulSanders That's my point.I answered according to my understanding(and philosophy) of the question. This is what i did(or always always always do) in steps. 1) I went to onlinegdb.com 2) then i just copy/paste the code from OP's question to the online compiler 3) Then i fix the errors there 4) I report back the errors that i fixed there& 5) Finally i paste the link to the online compiler. This is all i do when i see an MRE. There's nothing wrong in doing that. And IMO it is the best and fastest way to solve a problem.Ofcourse there can be other logical errors but i am not addressing them. – Jason Jun 21 '22 at 11:03
  • @PaulSanders As i said, ofcourse there can be other logical errors in the program but when i have a MRE available and a specific error(also stated in the question by OP) from that MRE then i just answer that particular question. – Jason Jun 21 '22 at 11:05