C++代码倒数第二个大括号报expected unqualified-id错误无法编译求解
编译错误原因及修复方案
直接导致expected unqualified-id报错的原因
你在multivalues函数的实现行末尾多余添加了分号:
void multivalues(int &val, int &sq, int &cb, int &fth); // 此处分号为多余语法 { // 函数实现逻辑 }
C++语法规定函数声明末尾需要加分号,但函数定义的头部后不能加,多余的分号会让编译器判定这是一次新的函数声明,后续的大括号失去了对应的函数标识,直接触发语法错误。
其他需要修复的语法问题
- 注释格式错误:你写的单行注释被拆分为两行,第二行没有加
//前缀,会被编译器识别为非法代码:
要么把注释合并为一行,要么第二行开头也补充//This program will calculate the area of a circle and the volume of the sphere using a given radius value// // 该行无`//`前缀,属于非法代码语句//前缀。 - 类型匹配隐患:
cmath库的pow函数返回值为double类型,直接赋值给int变量会触发精度丢失警告,更推荐直接用整数乘法实现幂次计算,逻辑更简单也不会有精度问题。
修复后的完整代码
#include <iostream> #include <cmath> // This program will calculate the area of a circle and the volume of the sphere using a given radius value using namespace std; void multivalues(int & , int & , int &, int&) ; int main() { int value, squared, cubed, fourth; value = 2; multivalues(value,squared,cubed,fourth); cout << "The value of the squared is " << squared << endl; cout << "The value of the cubed is " << cubed << endl; cout << "The value of the fourth is " << fourth << endl; return 0; } void multivalues(int &val, int &sq, int &cb, int &fth) { sq = val * val; cb = val * val * val; fth = val * val * val * val; }
内容的提问来源于stack exchange,提问作者Doctor Petunia
相关产品推荐
相关产品推荐

