Joel Bastien
Joel Bastien

Reputation: 121

Why am i getting an "undeclared identifier" error in my c++ code?

So we're assigned at school to create a class time. They want us to seperate the class with a time.h header file, a time.cpp cpp file and a main.cpp cpp file. I've got the following code, but for some reason I keep getting an "undeclared identifier" error. All 3 files are included in my project right now.

Here is the code:

time.h

class time
{
private:
    int hours;
    int minutes;
    int seconds;
public:
    time();
    time(int sec);
    time(int h, int min, int sec);
    int getTime();
    void setHours(int h);
    void setMinutes(int min);
    void setSeconds(int sec);
    bool equals(time t);
    void addTime(time t);
    void printTime();
    void normalize();

};

time.cpp

#include "time.h"
#include <iostream>
#include <iomanip>

using namespace std;

time::time()
{}
time::time(int sec)
{}
time::time(int h, int min, int sec)
{}
int time::getTime()
{
    return (hours * 60 * 60) + (minutes * 60) + seconds;
}
void time::setHours(int h)
{
    hours = h;
}
void time::setMinutes(int min)
{
    minutes = min;
}
void time::setSeconds(int sec)
{
    seconds = sec;
}
bool time::equals(time t)
{
    if (hours == t.hours && minutes == t.minutes && seconds == t.seconds)
        return true;
    else return false;
}
void time::addTime(time t)
{
    hours += t.hours;
    minutes += t.minutes;
    seconds += t.seconds;
}
void time::printTime()
{
    cout << setfill('0') << setw(2) << hours
        << ":" << setfill('0') << setw(2) << minutes
        << ":" << setfill('0') << setw(2) << seconds;
}
void time::normalize()
{
    seconds %= 60;
    minutes = minutes + (seconds / 60);
    hours = hours + (minutes / 60);
    minutes = minutes % 60;
}

main.cpp

#include "time.h"
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    time time1;
    int seconds1;
    cout << "Enter the amount of seconds: ";
    cin >> seconds1;
    time time2(seconds1);
    int hours2, minutes2, seconds2;
    cout << "Enter the amount of hours: ";
    cin >> hours2;
    cout << "Enter the amount of muinutes: ";
    cin >> minutes2;
    cout << "Enter the amount of seconds: ";
    cin >> seconds2;
    time time3(hours2, minutes2, seconds2);
    time1.equals(time2);

}

Upvotes: 0

Views: 1130

Answers (1)

user4581301
user4581301

Reputation: 33932

Compiling the given code I receive a whole raft of error messages, the most telling being

warning: statement is a reference, not call, to function 'time'
     time time1;
          ^

This leads to time1 being reported as not declared later because it isn't.

The Standard library contains a header time.h and a function time. To ensure the program included the correct time.h and used the correct time, I renamed the header to mytime.h and the time class to mytime. All errors went away (a few warnings about unused parameters remain) as soon as the potential for ambiguity was removed.

I recommend using something a little less banal than mytime, but feel free to use whatever you wish so long as the name is descriptive and there are no more collisions.

Upvotes: 1

Related Questions