当我尝试将它们链接在一起时,我收到了这个错误。怎么了?我是否需要将#include <iostream>
和using namespace std;
放在头文件和两个cpp文件中?有没有办法我只需要包含<iostream>
和using namespace std;
一次?提前谢谢。
错误:
/tmp/cczgScpr.o: In function `main':
time_overloaded_operators.cpp:(.text+0x45): undefined reference to `Time::Time(unsigned int const&, unsigned int const&, unsigned int const&, unsigned int const&)'
time_overloaded_operators.cpp:(.text+0x63): undefined reference to `operator*(Time const&, int const&)'
time_overloaded_operators.cpp:(.text+0x74): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Time const&)'
time_overloaded_operators.cpp:(.text+0x92): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Time const&)'
time_overloaded_operators.cpp:(.text+0xb0): undefined reference to `Time::~Time()'
time_overloaded_operators.cpp:(.text+0xc3): undefined reference to `Time::~Time()'
time_overloaded_operators.cpp:(.text+0xdc): undefined reference to `Time::~Time()'
time_overloaded_operators.cpp:(.text+0xf6): undefined reference to `Time::~Time()'
collect2: ld returned 1 exit status
main cpp:
// practice on overloading operators on Time variables
#include <iostream>
#include "Time.h"
using namespace std;
//main function
int main() {
Time time1(0,1,0,0);
Time time3 = time1 * 2;
cout << time1 << endl;
cout << time3 << endl;
return 0;
}
头文件:
#ifndef TIME_H
#define TIME_H
#include <iostream>
using namespace std;
class Time {
public:
//constructor
Time(const unsigned int& day, const unsigned int& hour,
const unsigned int& minute, const unsigned int& second);
//copy constructor
Time(const Time& time);
//assignment operator
Time& operator=(const Time& time);
//destructor
~Time();
//member functions
//overloaded operators
Time& operator+=(const Time& time);
Time operator+(const Time& time);
friend Time operator*(const Time& time, const int& integer_number);
friend ostream& operator<<(ostream& output, const Time& time);
friend istream& operator>>(istream& input, Time& time);
friend bool operator==(const Time& first_object, const Time& second_object);
friend bool operator!=(const Time& first_object, const Time& second_object);
private:
unsigned int day_;
unsigned int hour_;
unsigned int minute_;
unsigned int second_;
void ConvertSecondsToTime();
unsigned int TotalTimeInSeconds() const;
};
#endif
实施档案:
#include "Time.h"
#include <iostream>
// class constructors
Time::Time(const unsigned int& day = 0, const unsigned int& hour = 0,
const unsigned int& minute = 0, const unsigned int& second = 0)
: day_(day),
hour_(hour),
minute_(minute),
second_(second) {
}
Time::Time(const Time& time)
: day_(time.day_),
hour_(time.hour_),
minute_(time.minute_),
second_(time.second_) {
}
Time& Time::operator=(const Time& time) {
day_ = time.day_;
hour_ = time.hour_;
minute_ = time.minute_;
second_ = time.second_;
return *this;
}
Time::~Time() {}
//overloaded operators
unsigned int Time::TotalTimeInSeconds() const {
return (day_ * 24 * 60 * 60
+ hour_ * 60 * 60
+ minute_ * 60
+ second_);
}
void Time::ConvertSecondsToTime() {
while (second_ >= 60) {
second_ -= 60;
minute_ += 1;
}
while (minute_ >= 60) {
minute_ -= 60;
hour_ += 1;
}
while (hour_ >= 24) {
hour_ -= 24;
day_ += 1;
}
}
T ime& Time::operator+=(const Time& time) {
Time temp;
temp.second_ = TotalTimeInSeconds() + time.TotalTimeInSeconds();
*this = temp;
ConvertSecondsToTime();
return *this;
}
Time Time::operator+(const Time& time) {
Time temp(*this);
temp += time;
return temp;
}
Time operator*(const Time& time, const int& integer_number) {
Time temp;
temp.second_ = time.TotalTimeInSeconds() * integer_number;
temp.ConvertSecondsToTime();
return temp;
}
ostream& operator<<(ostream& output, const Time& time) {
output << "days: " << time.day_
<< " hours: " << time.hour_
<< " minutes: " << time.minute_
<< " seconds: " << time.second_ << endl;
return output;
}
istream& operator>>(istream& input, Time& time) {
input >> time.day_ >> time.hour_ >> time.minute_ >> time.second_;
if (!input) {
time = Time();
}
return input;
}
bool operator==(const Time& first_object, const Time& second_object) {
return first_object.TotalTimeInSeconds()
== second_object.TotalTimeInSeconds();
}
bool operator!=(const Time& first_object, const Time& second_object) {
return !(first_object == second_object);
}
答案 0 :(得分:3)
g++ main.cpp time.cpp
应修复链接错误,以便main可以链接到时间函数。如果时间成员函数在共享库中实现,则必须将其传递给linker。通常喜欢-llibraryname