2015-11-09 18 views
0

我正在編程類中的一個程序,涉及計算一維,二維和三維之間距離的繼承功能。基本上如果用戶選擇在一個維度上進行計算,它應該在數字線上進行計算(x)。如果用戶選擇了兩個維度,用戶輸入一對(x,y)座標,如XY笛卡爾座標平面,兩點之間計算距離。如果用戶選擇三維計算,則用戶應輸入一對x,y和z座標,並計算兩者之間的距離。然而,我目前遇到的問題是,我的函數定義中總共有18種不同類型的錯誤,主要來自於聲明函數調用與參數列表不匹配。有沒有辦法簡單地解釋這些,並給我一個關於如何解決我的錯誤的想法?這裏是我的代碼,首先盯着我的一個維度定義,然後是二維定義,三維定義和最後我的主要。有錯誤的行已被正確添加。謝謝。C++繼承維度計算器程序錯誤:「函數調用缺少參數列表」

//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; 
} 
+1

你應該列出你有錯誤。儘管關於函數錯誤參數的錯誤幾乎總是非常簡單的。 –

+0

「我目前遇到的問題是我的函數定義中總共有18個錯誤,說明函數調用與參數列表不匹配。」那些錯誤是......?在這裏複製它們!包括行號,甚至更好,在代碼中標出行,以便給出錯誤! –

+0

我不知道爲什麼我看到人們在自己的頭文件中定義的'main.cpp'中實現類。問題是,你有一個接受'OneDimension','TwoDimension'或'ThreeDimension'的函數,並且你傳遞一個整數。 –

回答

0

當你調用

OneDimension.CalcDistance(point2); 

您傳遞點2是int類型的。但是,在您定義函數的地方

OneDimension::CalcDistance(OneDimension point) 

您指定函數參數應該是OneDimension類型。由於類型不匹配,這會產生錯誤。您與TwoDimension和ThreeDimension有同樣的問題。

+0

所以糾正我,如果我錯了,但你的話基本上是說,而不是第2點被宣佈爲一個int,我必須通過它作爲OneDimension類型的對象? – Nathan

+0

是的,這是正確的。函數定義語法'OneDimension :: CalcDistance(OneDimension point)'定義函數,它指定函數的調用方式以及如何使用它。這意味着「函數CalcDistance接受OneDimension類型的單個參數」。參數描述是()之間的部分。 – EkcenierK