# 错误C2065：未声明的标识符error C2065: undeclared identifier

error C2065: 'v' : undeclared identifier
error C2065: 'a' : undeclared identifier

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;
int r;
int computeSphere(int r) {
double a,v;
a = 4*3.14* pow(r,2);
v = (4/3) * 3.14 * pow(r,3);

return a,v;
}

int main() {
cout << "Enter the radius: ";
cin >> r;
cout << fixed << setprecision(2);

computeSphere(r);

cout << "The area of a sphere of radius " << r << " is " << a << " and its ";
cout << "volume is ";
cout << v;
cout << endl;

return 0;
}

## 3 个回复3

### ===============>>#1 票数：4 已采纳

• 您不能从一个函数返回多个值。
• 您不会在调用函数的返回值上做任何事情。

struct SphereStuff
{
double a;
double v;
};

SphereStuff computeSphere(double r)
{
SphereStuff stuff;
stuff.a = ...;
stuff.v = ...;
return stuff;
}

int main()
{
SphereStuff s = computeSphere(42);        // ***
std::cout << s.a << ", " << s.v << "\n";
}

### ===============>>#2 票数：0

double Volume (int r) { return (4/3) * 3.14 * pow(r,3); }
double Area (int r) { return 4*3.14* pow(r,2); }

double v = Volume(r);
double a = Area(r);

### ===============>>#3 票数：-1

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;
int r;

double calc_a(int r){
return 4*3.14* pow(r,2);
}

double calc_v(int r){
return (4/3) * 3.14 * pow(r,3);
}

int main()
{
double a,v;

cout << "Enter the radius: ";
cin >> r;

a = calc_a(r);
v = calc_v(r);

cout << fixed << setprecision(2);
cout << "The area of a sphere of radius " << r << " is " << a << " and its ";
cout << "volume is ";
cout << v;
cout << endl;

return 0;
}