c++ - 如果稍后定义返回对象的类,我应该如何从转换函数返回对象?

标签 c++ type-conversion return-type data-conversion

class time24;
class time12
{
operator time24()
 {
  ...
  return time24(temp)   // error
 }
}

class time24
{
 ...
};

错误 C2440:“”:无法从“int”转换为“time24”

我还能如何返回对象来克服这个错误

最佳答案

在实现文件中将实现移动到类定义之后:

//header.h
class time24;
class time12
{
    operator time24();
}

class time24
{
 ...
};

//implementation.cpp
#include "header.h"
time12::operator time24()
{
   return time24(temp)   // error
}

我假设您打算实现 operator time24()

关于c++ - 如果稍后定义返回对象的类,我应该如何从转换函数返回对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10172718/

相关文章:

java - 从方法返回错误代码

c++ - 使用 Windows .dll 中的 SWIG 包装函数

C++ Hooking kernel32.dll OpenProcess 走弯路

c# - 如何在 C# 中表示一个数字是长整数还是小数?

Java 将 StringBuilder 转换为 CharBuffer

arrays - 将 int/string 转换为长度为 n 的字节数组

c++ - 表达式 : String Subscript Out of Range

c++11 Singleton 正在创建新实例

c - 错误: incompatible types for a function that returns float in C

c++ - 返回地址是否确保非 NULL 返回值?