我正在将现有代码的一部分从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)