jeudi 29 septembre 2022

Dev C++ [Error] undefined reference to and id returned 1 exit status

I'm new to this c++ and i'm working on a program using ostringstream to create string representations of the time in standard and universal time formats. But i keep getting some errors and will really apprectiate your help.

Time.h

#include <string>

#ifndef TIME_H
#define TIME_H

class Time{
    public:
        void setTime(int, int, int);
        std::string toUniversalString() const;
        std::string toStandardString() const;
    private:
        unsigned int hour{0};
        unsigned int minute{0};
        unsigned int second{0};
};

#endif

Time.cpp

#include <iomanip>
#include <stdexcept>
#include <sstream>
#include <string>
#include "Time.h"

using namespace std;

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{
        throw invalid_argument("hour, minute and/or second was out of range");
    }
}

string Time::toUniversalString() const{
    ostringstream output;
    output << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second;
    return output.str();
}

string Time::toStandardString() const{
    ostringstream output;
    output << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" << setfill('0') << setw(2) << minute << ":" << setw(2) << second << (hour < 12 ? " AM" : " PM");
    return output.str();
}

fig9.cpp

#include <iostream>
#include <stdexcept>
#include "Time.h"

using namespace std;

void displayTime(const string& message, const Time& time){
    cout << message << "\nUniversal time: " << time.toUniversalString() << "\nStandard time: " << time.toStandardString() << "\n\n";
}

int main(){
    Time t;
    
    displayTime("Initial time: ", t);
    t.setTime(13, 27, 6);
    displayTime("After setTime:  ", t);
    
    try{
        t.setTime(99, 99, 99);
    }
    catch(invalid_argument& e){
        cout << "Exception: " << e.what() << "\n\n";
    }
    
    displayTime("After attempting to set an invalid time:", t);
}

when i go to compile with my client program i get this error upon compilation

undefined reference to 'Time::toStandardString() const'
undefined reference to 'Time::toUniversalString() const'
undefined reference to 'Time::setTime(int,int,int)'
[Error] Id returned 1 exit status

Aucun commentaire:

Enregistrer un commentaire