我正在编译我的代码并将其与g ++链接。我创建了一个通用的TimeSpan<T>
类,现在在TimeMachine.cpp
中实例化它。这是该课程:
#include <iostream>
#include "TimeSpan.h"
using namespace std;
int main() {
TimeSpan<int> t;
return 0;
}
运行g++ --std=c++14 TimeMachine.cpp
时收到错误消息:
Undefined symbols for architecture x86_64:
"TimeSpan<int>::TimeSpan()", referenced from:
_main in TimeMachine.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
但是在TimeSpan.h中,我定义了TimeSpan()
符号。请帮忙。
编辑:这是TimeSpan.h:
#ifndef CSS342LAB1_TIMESPAN_H
#define CSS342LAB1_TIMESPAN_H
#include <iostream>
using namespace std;
template<class T>
class TimeSpan
{
friend ostream& operator <<(ostream &outStream, const TimeSpan &duration);
friend istream& operator >>(istream &inStream, TimeSpan &duration);
public:
//
// Constructors and destructors
TimeSpan();
TimeSpan(T seconds);
TimeSpan(T minutes, T seconds);
TimeSpan(T hours, T minutes, T seconds);
//~TimeSpan();
//
//getters and setters
int getHours() const;
int getMinutes() const;
int getSeconds() const;
bool setTime(int hours, int minutes, int seconds);
//
// operator overload
bool operator==(const TimeSpan &duration) const;
bool operator!=(const TimeSpan &duration) const;
friend ostream& operator <<(ostream &outStream, const TimeSpan &duration);
friend istream& operator >>(istream &inStream, TimeSpan &duration);
private:
int seconds;
int minutes;
int hours;
};
#endif //CSS342LAB1_TIMESPAN_H