小编Ezi*_*zio的帖子

C++类型将int转换为union

我正在将现有代码的一部分从C转移到C++.我只需要将文件移动到.cc,制作并修复错误.现有代码与此类似,

/* a.h */
typedef union foo_ {
    int var;
}foo;

void fun(foo a)
{
   printf("%d\n", a.var);
}


/* a.cc or a.c */
#include<stdio.h>
#include"a.h"

int main()
{
    int a = 0x10;
    foo x;
    x = (foo)a; // Error when the file is .cc but works with .c
    fun(x);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

将main函数中的int变量'a'转换为'foo'在C中运行正常,但在C++中显示以下错误,

a.cc: In function int main():
a.cc:8:14: error: no matching function for call to foo_::foo_(int&)
a.cc:8:14: note: candidates are:
a.h:2:15: note: foo_::foo_()
a.h:2:15: note:   candidate expects 0 arguments, …
Run Code Online (Sandbox Code Playgroud)

c c++ casting

1
推荐指数
1
解决办法
771
查看次数

标签 统计

c ×1

c++ ×1

casting ×1