2011-12-05 122 views
0

当我尝试将它们链接在一起,我得到了这个错误。怎么了?我需要将#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 

主要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); 
} 
+6

**从来没有**把'using namespace std;'放在头文件中。 –

+2

你的命令行是什么?也就是说,你用来构建它的完整的g ++代码是什么? – chrisaycock

+0

错误消息表示无法找到'Time'类中的方法。我们需要了解你如何构建你的项目(即命令行),最重要的是你如何调用链接器。 –

回答

3
g++ main.cpp time.cpp 

应该修复链接错误,以便main可以链接到时间函数。如果时间成员函数在共享库中实现,您将不得不将其传递给linker。通常像-llibraryname