C++ 未解析的外部符号时间类
所以我的错误是错误1错误LNK2019:未解析的外部符号“class std::basic_ostream>和&uu_cdecl运算符这也不是主文件的全部,只是我到目前为止编写的代码的一部分。因此,链接器说它没有看到来自“time.cpp”的任何代码。也许,你没有把它放在你的项目中,是吗?关于实现“operator>>”,这是您的家庭作业。从查看一些标准库标题开始,找出哪一个预定义函数可以帮助您。很抱歉,如果我是在询问代码,但我只是想问是否有人可以帮助我开始/更好地向我解释它的含义和用途,所以我不仅仅是看我书中的代码,并试图让它看起来一样,是的,所有3个都在项目中。我创建了一个新的,并做了同样的事情,但它工作了这次,基本上感谢,C++运算符重载可以用于任何你想要的。纯C运算符>只是位移位,但是C++标准库使用运算符>输入。因此,让操作符>>从istream读取数据、解析数据并在Time()类中存储新的日、小时、分钟等值是有意义的。为了使事情保持一致,您的操作员>>至少应该能够输入操作员需要的内容C++ 未解析的外部符号时间类,c++,class,linker,C++,Class,Linker,所以我的错误是错误1错误LNK2019:未解析的外部符号“class std::basic_ostream>和&uu_cdecl运算符这也不是主文件的全部,只是我到目前为止编写的代码的一部分。因此,链接器说它没有看到来自“time.cpp”的任何代码。也许,你没有把它放在你的项目中,是吗?关于实现“operator>>”,这是您的家庭作业。从查看一些标准库标题开始,找出哪一个预定义函数可以帮助您。很抱歉,如果我是在询问代码,但我只是想问是否有人可以帮助我开始/更好地向我解释它的含义和用途,所以我
#include <iostream>
#include "time.h"
using namespace std;
int main()
{
Time t1, t2, t3(123456), t4(-5);
cout << "t1 = " << t1 << '\n';
cout << "t2 = " << t2 << '\n';
cout << "t3 = " << t3 << '\n';
cout << "t4 = " << t4 << '\n';
return 0;
}
#include <iostream>
using namespace std;
class Time {
public:
// constructors
Time(); //default constructor
Time(int s); // conversion constructor
Time(int d, int h, int m, int s); // days, hours, minutes,
friend ostream& operator<<(ostream& out, const Time& t);
private:
//member variables
int day;
int hour;
int minute;
int second;
};
#endif
#include <iostream>
#include "time.h"
using namespace std;
Time::Time()
//default constructor. Initialize everything to start at 0
{
day = 0;
hour = 0;
minute = 0;
second = 0;
}
Time::Time(int s)
{
if (s < 0)
{
day = hour = minute = second = 0;
}
else
{
second = s;
day = second / 86400;
hour = (second / 3600) % 24;
minute = (second / 60) % 60;
second = second % 60;
}
}
Time::Time(int d, int h, int m, int s)
{
day = d;
hour = h;
minute = m;
second = s;
if (h < 0){
h = 0;
}
if (d < 0){
d = 0;
}
if (m < 0){
m = 0;
}
if (s < 0) {
s = 0;
}
}
ostream& operator<<(ostream& out, const Time& t)
{
out << t.day << "~";
if (0 <= t.hour && t.hour <= 10)
{
out << "0" << t.hour << ":";
}
else
out << t.hour << ":";
if (0 <= t.minute && t.minute <= 10)
{
out << "0" << t.minute << ":";
}
else
out << t.minute << ":";
if (0 <= t.second && t.second <= 10)
{
out << "0" << t.second;
}
else
out << t.minute;
return out;
}
friend istream& operator>>(istream& in, const Time& t);
istream& operator>>(istream& in, const Time& t)
{
in >> t.day;
in >> t.hour;
in >> t.minute;
in >> t.second;
return in;
}
cout << "Enter first Time object (DAYS~HH:MM:SS): ";
cin >> t1;