Я пытаюсь создать класс, который будет запускать эту основную программу, но я получаю ошибки:
Может ли кто-нибудь объяснить причину/решение указанных ошибок?
Вот моя главная:
int main()
{
clockType c1(15, 45, 30), c2(3, 20); // hour, min, sec
cout << "c1 is " << c1; // add whatever to beautify it
cout << "c2 is " << c2;
cout << "c1+c2 is " << c1+c2;
c2 = c1+c1;
cout << "c1+c1 is " << c2;
}
Вот мой заголовочный файл:
#ifndef CLOCKTYPE_H
#define CLOCKTYPE_H
#include <iostream>
#include <ostream>
class clockType
{
friend std::ostream& operator<<(std::ostream& os, const clockType& out);
friend clockType operator+(const clockType& one, const clockType& two);
public:
clockType();
clockType(int hours, int minutes, int seconds);
clockType(int hours, int minutes);
void setTime(int hours, int minutes, int seconds);
void getTime(int& hours, int& minutes, int& seconds);
void printTime();
void incrementhr();
void incrementmin();
void incrementsec();
private:
int hrs;
int mins;
int secs;
};
#endif // CLOCKTYPE_H
Вот мой файл cpp:
#include "clockType.h"
#include <iostream>
#include <iostream>
using namespace std;
clockType::clockType()
{
hrs = 0;
mins = 0;
secs = 0;
}
clockType::clockType(int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
}
clockType(int hours, int minutes)
{
hrs = hours;
mins = minutes;
secs = 0;
}
void clockType::setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hrs = hours;
else
hrs = 0;
if (0 <= minutes && minutes < 60)
mins = minutes;
else
mins = 0;
if(0 <= seconds && seconds < 60)
secs = seconds;
else
secs = 0;
}
ostream& operator<<(ostream& os, const clockType& out)
{
os << "Hour is " << out.hrs << "Minute is " << out.mins << "Seconds is " << out.secs;
return os;
}
clockType operator+(const clockType& one, const clockType& two)
{
clockType three;
three.hrs = one.hrs + two.hrs;
three.mins = one.mins + two.mins;
three.secs = one.secs + two.secs;
return three;
}
Ваш.ccp файл содержит ряд очевидных ошибок компиляции. Например, это отклонение
clockType(int hours, int minutes)
нарушается и не будет компилироваться. Предполагалось, что это
clockType::clockType(int hours, int minutes)
Тот факт, что вы не получаете/не сообщаете о каких-либо ошибках компиляции для вашего.cpp файла, означает, что вы просто забыли скомпилировать ваш.cpp файл как часть программы в целом.
Вот почему компилятор не может найти определение. Вы должны добавить свой файл.cpp в свою команду /makefile/command line (независимо от того, что вы используете). И тогда вам придется исправить ошибки компиляции, присутствующие в этом файле.
clockType.cpp
.