【发布时间】:2014-02-25 18:18:48
【问题描述】:
double add_physics_vector(vector<double> v1, vector<double> v2)
{
double horizontal_1 = v1[1] * cos(v1[0]);
double vertical_1 = v1[1] * sin(v1[0]);
double horizontal_2 = v2[1] * cos(v2[0]);
double vertical_2 = v2[1] * sin(v2[0]);
double horizontal_total = horizontal_1 + horizontal_2;
double vertical_total = vertical_1 + vertical_2;
double final_magnitude = sqrt(pow(horizontal_total, 2) + pow(vertical_total, 2));
double final_direction = atan2(vertical_total, horizontal_total);
double final_set[2] = {final_direction, final_magnitude};
return final_set;
}
当我尝试在我的 main() 函数中返回 final_set 时,编译器 (g++-4.7) 给了我这个错误:
error: cannot convert ‘double*’ to ‘double’ in return
warning: control reaches end of non-void function [-Wreturn-type]
【问题讨论】:
-
你不能返回一个数组。使用向量,或
std::array。 -
@BrianBi 不知道!向量不是一种数组吗?你知道这两者的结构具体是什么让它们在返回语句中有所不同吗?
-
不,向量有点像数组,但不是真的。它实际上是一个类。
标签: c++ arrays return return-type