【问题标题】:How to avoid Conflict of defined seconds() method and ptime seconds(default) method?如何避免定义的 seconds() 方法和 ptime seconds(default) 方法的冲突?
【发布时间】:2018-08-22 07:49:32
【问题描述】:

这里unsigned long EVTime::seconds() 方法与ptime p(d,seconds(s)); 冲突。如果我将 ptime seconds(s) 更改为分钟/小时,那么它工作正常。

如果我将seconds(s) 更改为minutes(s)hours(s),那么只有它会起作用。我是 C++ 新手,请任何人帮助解决这个冲突。

evt.cpp

unsigned long EVTime::seconds()
{
  ptime t(date(1901,Jan,1),time_duration(0,0,0));
  time_duration td = utcdatetime - t;
  return (unsigned long)td.total_seconds();
}

EVTime::EVTime(unsigned long s)
{
  date d(1901,1,1);
  ptime p(d,seconds(s));
  utcdatetime=p;
}

evt.h

class EVTime
{
public:
  EVTime(unsigned long s);
  unsigned long seconds();
  ptime utcdatetime;
};

main.cpp

int main()
{
  EVTime t(222l);
  cout<<"seconds since 1901: "<<t.seconds()<<endl;
}

错误代码:

evtime.cpp: In constructor ‘EVTime::EVTime(long unsigned int)’:
evtime.cpp:35: error: no matching function for call to ‘EVTime::seconds(long 
unsigned int&)’
evtime.cpp:14: note: candidates are: long unsigned int EVTime::seconds()

【问题讨论】:

  • 看起来你的函数seconds() 没有参数,但是当你调用它时你发送它sptime p(d,seconds(s));(我认为*s 是为了强调?)
  • 请去掉强调的星号,它们会破坏代码并且对讨论没有任何贡献。如果您想强调代码中的某些内容,只需使用注释即可。无论如何,我认为你的问题是你依赖using namespace。这是个错误。如果需要使用boost::posix_time::seconds,每次都拼写boost::posix_time::seconds,或者使用命名空间别名。
  • 谢谢,“boost::posix_time::seconds”这条线对我有用

标签: linux c++11 boost


【解决方案1】:

您的函数seconds() 不带参数,但在调用它时发送sptime p(d,seconds(s));

这会导致错误“没有用于调用 EVTime::seconds(long unsigned int&amp;) 的匹配函数”,因为您没有匹配的函数。

调用ptime p(d,seconds()); 或更改seconds 函数签名以获取参数并对其进行处理。

【讨论】:

    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    相关资源
    最近更新 更多