Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的函数不返回类型点? 我正在学习C++语法,对于下面的函数CouthestPoCon,我得到了一个“未声明的标识符”,用于返回结果< /Cord>行>代码> CouthStEngEng/Cuff>函数。为什么在赋值变量result时在if语句中声明类型时会发生这种情况 #include <iostream> #include <vector> #include <cmath> using namespace std; class Point{ public: int x,y; Point(int x_position, int y_position){ x = x_position; y = y_position; } int calculate_distance(Point other_point); }; int Point::calculate_distance(Point p2){ int x_distance = abs(x-p2.x); int y_distance = abs(y-p2.y); int result = x_distance-y_distance; return result; } Point closest_coin(Point your_position, vector<Point> coin_positions){ int closest_distance = -1; int distance; for (Point coin : coin_positions){ distance = your_position.calculate_distance(coin); if (distance>closest_distance){ Point result = coin; closest_distance = distance; } } return result; } #包括 #包括 #包括 使用名称空间std; 类点{ 公众: int x,y; 点(整数x_位置,整数y_位置){ x=x_位置; y=y_位置; } int计算距离(点其他点); }; int Point::计算_距离(点p2){ int x_距离=abs(x-p2.x); int y_距离=abs(y-p2.y); int结果=x_距离-y_距离; 返回结果; } 指向最近的硬币(指向你的硬币位置,向量硬币位置){ int最近距离=-1; 整数距离; 用于(点数硬币:硬币位置){ 距离=你的位置。计算距离(硬币); 如果(距离>最近距离){ 积分结果=硬币; 最近距离=距离; } } 返回结果; }_C++ - Fatal编程技术网

为什么我的函数不返回类型点? 我正在学习C++语法,对于下面的函数CouthestPoCon,我得到了一个“未声明的标识符”,用于返回结果< /Cord>行>代码> CouthStEngEng/Cuff>函数。为什么在赋值变量result时在if语句中声明类型时会发生这种情况 #include <iostream> #include <vector> #include <cmath> using namespace std; class Point{ public: int x,y; Point(int x_position, int y_position){ x = x_position; y = y_position; } int calculate_distance(Point other_point); }; int Point::calculate_distance(Point p2){ int x_distance = abs(x-p2.x); int y_distance = abs(y-p2.y); int result = x_distance-y_distance; return result; } Point closest_coin(Point your_position, vector<Point> coin_positions){ int closest_distance = -1; int distance; for (Point coin : coin_positions){ distance = your_position.calculate_distance(coin); if (distance>closest_distance){ Point result = coin; closest_distance = distance; } } return result; } #包括 #包括 #包括 使用名称空间std; 类点{ 公众: int x,y; 点(整数x_位置,整数y_位置){ x=x_位置; y=y_位置; } int计算距离(点其他点); }; int Point::计算_距离(点p2){ int x_距离=abs(x-p2.x); int y_距离=abs(y-p2.y); int结果=x_距离-y_距离; 返回结果; } 指向最近的硬币(指向你的硬币位置,向量硬币位置){ int最近距离=-1; 整数距离; 用于(点数硬币:硬币位置){ 距离=你的位置。计算距离(硬币); 如果(距离>最近距离){ 积分结果=硬币; 最近距离=距离; } } 返回结果; }

为什么我的函数不返回类型点? 我正在学习C++语法,对于下面的函数CouthestPoCon,我得到了一个“未声明的标识符”,用于返回结果< /Cord>行>代码> CouthStEngEng/Cuff>函数。为什么在赋值变量result时在if语句中声明类型时会发生这种情况 #include <iostream> #include <vector> #include <cmath> using namespace std; class Point{ public: int x,y; Point(int x_position, int y_position){ x = x_position; y = y_position; } int calculate_distance(Point other_point); }; int Point::calculate_distance(Point p2){ int x_distance = abs(x-p2.x); int y_distance = abs(y-p2.y); int result = x_distance-y_distance; return result; } Point closest_coin(Point your_position, vector<Point> coin_positions){ int closest_distance = -1; int distance; for (Point coin : coin_positions){ distance = your_position.calculate_distance(coin); if (distance>closest_distance){ Point result = coin; closest_distance = distance; } } return result; } #包括 #包括 #包括 使用名称空间std; 类点{ 公众: int x,y; 点(整数x_位置,整数y_位置){ x=x_位置; y=y_位置; } int计算距离(点其他点); }; int Point::计算_距离(点p2){ int x_距离=abs(x-p2.x); int y_距离=abs(y-p2.y); int结果=x_距离-y_距离; 返回结果; } 指向最近的硬币(指向你的硬币位置,向量硬币位置){ int最近距离=-1; 整数距离; 用于(点数硬币:硬币位置){ 距离=你的位置。计算距离(硬币); 如果(距离>最近距离){ 积分结果=硬币; 最近距离=距离; } } 返回结果; },c++,C++,当我在if语句中声明类型时 当您在{和}中声明任何内容时,该变量仅在那里有效。如果出于同样的原因在if外部声明它,但在for循环内部声明它,那么它也不会起作用 您只需在最近的函数的开头声明结果变量,并仅在if中赋值即可 注意:您的点类没有默认构造函数。因此,在声明时必须设置一些值。您的最近的\u coint方法变为 指向最近的硬币(指向你的硬币位置,向量硬币位置){ int最近距离=-1; 整数距离; 得分结果(0,0); 用于(点数硬币:硬币位置){ 距离=你的位置。计算距离(硬币); 如果(

当我在if语句中声明类型时

当您在
{
}
中声明任何内容时,该变量仅在那里有效。如果出于同样的原因在
if
外部声明它,但在
for
循环内部声明它,那么它也不会起作用

您只需在
最近的
函数的开头声明
结果
变量,并仅在
if
中赋值即可

注意:您的
类没有默认构造函数。因此,在声明时必须设置一些值。您的
最近的\u coint
方法变为

指向最近的硬币(指向你的硬币位置,向量硬币位置){
int最近距离=-1;
整数距离;
得分结果(0,0);
用于(点数硬币:硬币位置){
距离=你的位置。计算距离(硬币);
如果(距离>最近距离){
结果=硬币;
最近距离=距离;
}
}
返回结果;
}

查看您在哪里声明
点结果
以及在哪里
返回结果是。这些是不同的范围,因此我需要在最近的{}括号内添加“Point result;”?这里是否有可能重复的定义为,
Point result而不初始化类?我知道这将抛出一个错误,正如我在这里写的。仅供将来参考,谢谢。编辑:没有完全阅读您的答案,默认构造函数..将阅读有关它们的内容。