c++ - 无法从 "void"转换为 "int"

标签 c++

无法在 C++ 中从“void”转换为“int” - 有人知道为什么吗?有我必须使用的功能吗?

int calc_tot,go2;
go2=calculate_total(exam1,exam2,exam3);
calc_tot=read_file_in_array(exam);

最佳答案

go2=calculate_total(exam1,exam2,exam3);
calc_tot=read_file_in_array(exam);

我的猜测是这两个函数之一返回 void,因此您不能将该值分配给 int。由于“void”函数不返回任何内容,因此您不能将其返回值分配给 int。

我希望这样的代码会给你这样的错误:

void voidfunc () {
  // Do some things
}

int main () {
    int retval = voidfunc();
    return 0;
}

虽然我的编译器给出:

$ g++ main.c
main.c: In function ‘int main()’:
main.c:6: error: void value not ignored as it ought to be

关于c++ - 无法从 "void"转换为 "int",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2747420/

相关文章:

c++ - 如何获得指向颜色按钮的指针

c++ - 2 位输入的表达式树

c++ - 维基代码?任何人都可以查看和编辑代码的地方?

c++ - popen()如何工作以及如何在Linux上将其实现为C++代码?

c++ - 为什么代码行即使从未运行也会出错?

c++ - std::default_random_engine 生成介于 0.0 和 1.0 之间的值

c++ - 如何通过分隔符标记字符串?

c++ - 可以在类外使用类中定义的名称吗?

c++ - 从构造函数参数的组合生成变量类型的对象

c++ - 为 Mac 10.9 10.11 下编译