我正在编程课上编写一个程序,用于涉及继承的作业,该程序计算一维、二维和三维之间的距离。基本上如果用户选择在一维计算,它应该在数轴上进行计算 (x)。如果用户选择二维,用户输入一对 (x,y) 坐标,如 XY 笛卡尔坐标平面,计算两点之间的距离。如果用户选择 3 维计算,则用户应输入一对 x、y 和 z 坐标,并据此计算两者之间的距离。然而,我目前遇到的问题是,我在函数定义中总共遇到了 18 种不同类型的错误,这些错误主要来自声明函数调用与参数列表不匹配。有没有办法简单地解释这些并让我知道如何解决我的错误?这是我的代码,首先是我的一维定义,然后是二维定义,三维定义,最后是我的主要定义。有错误的行已正确添加。谢谢。
//definition of 1st dimension
#include <iostream>
#include "OneDimenson.h"
OneDimension::OneDimension(int x)
{ //sets x
x = x;
}
int OneDimension::GetX()
{ // returns x
return x;
}
int OneDimension::CalcDistance(OneDimension point2)
{ //calculates the distance between the instance(object created) and the 1d point
int distance = 0;
distance = point2.GetX - x; //2 ERRORS HERE: 1.) Function doesent match
// argument list and 2.) illegal, left operand has type int.
cout << "Distance Calculated." << endl;
return distance;
}
// definition of 2nd definiton
#include <iostream>
#include "OneDimenson.h"
#include "TwoDimension.h"
TwoDimension::TwoDimension(int x, int y):OneDimension(x)
{ // sets x and y
x = x;
y = y;
}
int TwoDimension::GetY()
{ // returns y
return y;
}
int TwoDimension::CalcDistance(OneDimension point2)
{ // calculates the distance between the 2nd distance and the 1d point.
int distance = 0;
distance = point2.GetX - x; // 2 ERRORS: 1.) function call missing from
// arguement. 2.) illegal, left operand has a type of int.
return distance;
}
int TwoDimension::CalcDistance(TwoDimension point2)
{ // calculates the distance between the instance and the 2nd point.
int distance = 0;
distance = point2.GetY - x; //ERROR HERE
return distance;
}
// definition of the 3d dimension
#include "OneDimenson.h"
#include "TwoDimension.h"
#include "ThreeDimension.h"
ThreeDimension::ThreeDimension(int x, int y, int z):TwoDimension(x,y)
{ //sets x, y, and z
cout << "please set the dimension for x." << endl;
cin >> x;
cout << "please set the dimension for y." << endl;
cin >> y;
cout << "please set the dimension for z." << endl;
cin >> z;
}
int ThreeDimension::GetZ()
{ //returns z
return z;
}
int ThreeDimension::CalcDistance(OneDimension point2)
{ // calculates the distance between the 3d instance and the 1d point.
int distance = 0;
distance = point2.GetX - z; //2 Errors: 1.) Function doesent match
// argument list and 2.) illegal operand has type int.
return distance;
}
int ThreeDimension::CalcDistance(TwoDimension point2)
{ // calculates the distance between the 2d instance and the 2d point.
int distance = 0;
distance = point2.GetY - y; //2 ERRORS: 1) Function call missing from
// arguement. 2) illegal, left operand has a type of int.
return distance;
}
int ThreeDimension::CalcDistance(ThreeDimension point2)
{ // calculates the distance between the instance and the 3d point.
int distance = 0;
distance = point2.GetZ - x; //2 ERRORS: 1) Function call missing from
// arguement and 2) illegal, left operand has type of int
return distance;
}
// This is the main
#include <iostream>
#include <string>
#include "OneDimenson.h"
#include "TwoDimension.h"
#include "ThreeDimension.h"
using namespace std;
int main()
{
int UserChoice = 0;
int x = 0;
int y = 0;
int z = 0;
int point2 = 0;
do
{
cout << "-----CONSOLE MENU----" << endl;
cout << "Would you like to calculate 1d, 2d or 3d?" << endl;
cout << "(PRESSING 0 ENDS PROGRAM)." << endl;
cout << "1.) 1d calculation" << endl;
cout << "2.) 2d calculation" << endl;
cout << "3.) 3d calculation" << endl;
cin >> UserChoice;
switch (UserChoice)
{
case 0:
break;
case 1:
// if user selects 1, then the user selects x, then point 2, to which the distance is calculated.
cout << "1d calculation chosen. Enter the x point: " << endl;
cin >> x;
OneDimension OneDimension(x);
cout << "Enter the instance Point: " << endl;
cin >> point2;
cout << "The distance between the two points (in 1d space) is: " << OneDimension.CalcDistance(point2);
cout << " " << endl;
case 2: // ERROR: ONE DIMENSION IS SKIPPED BY CASE LABEL
// if user selects 2, the user then selects the x and y coordinate. then the distance is calculated.
cout << "2d calculation chosen. Enter the x part of the point: " << endl;
cin >> x;
cout << "Enter the y part of the point." << endl;
cin >> y;
cout << "Enter the instance point." << endl;
cin >> point2;
TwoDimension TwoDimension(x,y);
cout << "The distance between the two points (in 2d space) is: " << TwoDimension.CalcDistance(point2);
cout << " " << endl;
case 3: //ERROR: TWO DIMESION AND ONE DIMENSION ARE SKIPPED BY CASE LABEL:
// if user selects 3, then the user selects x, y and z coordinate. Then the distance is calculated.
cout << "3d calculation chosen. Enter the x part of the point: " << endl;
cin >> x;
cout << "Enter the y part of the point." << endl;
cin >> y;
cout << "Enter the z part of the point." << endl;
cin >> z;
cout << "Enter the instance point." << endl;
cin >> point2;
ThreeDimension ThreeDimension(x, y, z);
cout << "The distance between the two points (in 3d space) is: " << ThreeDimension.CalcDistance(point2);
cout << " " << endl;
default: //ERROR ABOUT HOW ONEDIMENSION, TWODIMENSION, AND THREEDIMENSION ARE SKIPPED BY DEFAULT LABEL
break;
}
} while (UserChoice != 0);
return 0;
}
最佳答案
当你打电话时
OneDimension.CalcDistance(point2);
你传递给它的是 int 类型的 point2。然而,在你定义函数的地方
OneDimension::CalcDistance(OneDimension point)
您指定函数参数的类型应为 OneDimension。由于类型不匹配,因此会产生错误。 TwoDimension 和 ThreeDimension 也有同样的问题。
关于C++继承维度计算器程序错误: "function call missing argument list",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33600793/