我是C ++的新手(我来自C#)。
我在命名空间中有这个结构:
#pragma once
namespace utils {
struct astTime
{
int hour;
int min;
double secs;
};
double round(double number, int decPlace);
}
我还有一个实现round
功能的源文件。
为了在Boost测试中使用该结构,我在Boost测试文件(.cpp)中定义了这两个运算符:
namespace utils {
bool operator ==(utils::astTime const &left, utils::astTime const &right)
{
return(
left.secs == right.secs
&& left.min == right.min
&& left.hour == right.hour);
}
std::ostream& operator<<(std::ostream& os, const utils::astTime& dt)
{
os << dt.hour << "h " << dt.min << "m " << dt.secs << "s" << std::endl;
return os;
}
}
我必须在哪里声明这两个运算符(以及如何声明)?
我已将其移至头文件(因为我不知道在何处进行声明),因此将它们从增强测试源文件中删除:
#pragma once
#include <iostream>
namespace utils {
struct astTime
{
int hour;
int min;
double secs;
};
bool operator ==(utils::astTime const &left, utils::astTime const &right)
{
return(
left.secs == right.secs
&& left.min == right.min
&& left.hour == right.hour);
}
std::ostream& operator<<(std::ostream& os, const utils::astTime& dt)
{
os << dt.hour << "h " << dt.min << "m " << dt.secs << "s" << std::endl;
return os;
}
double round(double number, int decPlace);
}
我收到以下错误:
警告LNK4006:“类std :: basic_ostream>&__cdecl utils :: operator <<(类std :: basic_ostream>&,struct utils :: astTime const&)”(?? 6utils @@ YAAAV?$ basic_ostream @ DU?$ Utils.obj中已定义的char_traits @ D @ std @@@ std @@ AAV12 @ ABUastTime @ 0 @@ Z);第二个定义被忽略
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句