2017-10-19 38 views
0

我介紹計算機科學類只是覆蓋在本週的功能,並檢查「未解決的外部符號」的主要線索後,我想,不是3未解決的外部符號(初學C++)

a)你聲明的功能,但從來沒有叫他們以後主要

b)你是缺少正確的庫

我只是不知道它是哪一個或如何正確地去了解它。另外,我認爲我的邏輯在calcSideC塊中有一些小缺陷,儘管我不確定

#include <iostream> 
#include <cmath> 

using namespace std; 

float getSide(); 
float calcSideC(float sideA, float sideB, float total); 
void displaySideC(float sideC); 

int main() 
{ 
    { 
     float sideA = 0.0; 
     float sideB = 0.0; 
     float total = sideA + sideB; 
     float sideC = sqrt(total); 
     sideA = getSide(); 
     sideB = getSide(); 
     sideC = calcSideC(sideA, sideB, total); 
     displaySideC(sideC); 

     return 0; 
    } 

    float getSide(); 
    { 
     float sideA; 
     cout << "Enter two sides of a right triangle.\n\n" << "Side A: \n" << "Please enter the dimension: "; 

     cin >> sideA; 

     return sideA; 
    } 
    float getSide(); 
    { 
     float sideB; 
     cout << "\n\n" << "Side B: \n" << "Please enter the dimension: "; 
     cin >> sideB; 

     return sideB; 
    } 
    float calcSideC(float sideA, float sideB, float total); 
    { 
     float sideA; 
     float sideB; 
     float total; 
     float sideC; 
     pow(sideA, 2); 
     pow(sideB, 2); 
     float sqrt(total); 
     return sideC; 
    } 
    void displaySideC(float sideC); 
    { 
     float sideC; 
     cout << "The dimension of Side C is: " << sideC; 
    } 
    system("pause"); 
    return 0; 
} 
+0

你永遠*定義*您的功能,如'getSide '和其他人。因此鏈接器錯誤。 – AnT

回答

2

無法定義的內部功能其他功能(不是說你正在做正確的反正,因爲你有額外的;,導致代碼行爲不同於你所期望的)。

您需要將函數定義從main()中移出。現在

,與語法錯誤固定的,你仍然在你的代碼相當多的邏輯錯誤,包括:

  • 聲明函數的局部變量,這些變量的名稱相同功能參數

  • sideA和之前忽略的std::pow()一個std::sqrt()

  • 計算total的返回值已被用戶分配值。

嘗試一些更喜歡這個:

#include <iostream> 
#include <cmath> 

using namespace std; 

float getSideA() 
{ 
    float sideA; 
    cout << "\n\n" << "Side A: \n" << "Please enter the dimension: ";  
    cin >> sideA; 
    return sideA; 
} 

float getSideB() 
{ 
    float sideB; 
    cout << "\n\n" << "Side B: \n" << "Please enter the dimension: "; 
    cin >> sideB; 
    return sideB; 
} 

float calcSideC(float sideA, float sideB) 
{ 
    return sqrt(pow(sideA, 2) + pow(sideB, 2)); 
} 

void displaySideC(float sideC) 
{ 
    cout << "The dimension of Side C is: " << sideC; 
} 

int main() 
{ 
    cout << "Enter two sides of a right triangle."; 
    float sideA = getSideA(); 
    float sideB = getSideB(); 

    float sideC = calcSideC(sideA, sideB); 
    displaySideC(sideC); 

    system("pause"); 
    return 0; 
} 

或者,如果你想以前瞻聲明功能:

#include <iostream> 
#include <cmath> 

using namespace std; 

float getSideA(); 
float getSideB(); 
float calcSideC(float sideA, float sideB); 
void displaySideC(float sideC); 

int main() 
{ 
    cout << "Enter two sides of a right triangle."; 
    float sideA = getSideA(); 
    float sideB = getSideB(); 

    float sideC = calcSideC(sideA, sideB); 
    displaySideC(sideC); 

    system("pause"); 
    return 0; 
} 

float getSideA() 
{ 
    float sideA; 
    cout << "\n\n" << "Side A: \n" << "Please enter the dimension: ";  
    cin >> sideA; 
    return sideA; 
} 

float getSideB() 
{ 
    float sideB; 
    cout << "\n\n" << "Side B: \n" << "Please enter the dimension: "; 
    cin >> sideB; 
    return sideB; 
} 

float calcSideC(float sideA, float sideB) 
{ 
    return sqrt(pow(sideA, 2) + pow(sideB, 2)); 
} 

void displaySideC(float sideC) 
{ 
    cout << "The dimension of Side C is: " << sideC; 
} 
+0

我不能非常感謝你們!你們都是巨大的幫助,如果我可以的話,我會讓你們兩個都高興,但我是太新了,絕對使用這些答案作爲功能的學習工具。 –

1

你在main中聲明瞭你的函數!這是錯誤的。將它們移到外面。 接下來,聲明行上的函數沒有分號。

IE:void func(); {}是錯誤的,因爲它應該是void func() {},沒有分號。

  • 你有一個函數getSide() TWICE ..
  • 您從主前system("pause");
  • calcSideC返回具有本地變量,都是一樣的參數(重聲明錯誤)。
  • displaySideC具有與參數相同的局部變量(重新聲明錯誤)。
  • calcSideC不使用sqrt(total);的結果。
  • calcSizeC利用勾股定理sideC,因爲你甚至不使用pow結果實際上並不計算..

正確的代碼是:

#include <iostream> 
#include <cmath> 

using namespace std; 

float getSideA(); 
float getSideB(); 
float calcSideC(float sideA, float sideB); 
void displaySideC(float sideC); 

int main() 
{ 
    float sideA = 0.0; 
    float sideB = 0.0; 
    float sideC = 0.0; 

    sideA = getSideA(); 
    sideB = getSideB(); 
    sideC = calcSideC(sideA, sideB); 
    displaySideC(sideC); 

    system("pause"); 
    return 0; 
} 


float getSideA() 
{ 
    float sideA; 
    cout << "Enter two sides of a right triangle.\n\n" << "Side A: \n" << "Please enter the dimension: "; 

    cin >> sideA; 

    return sideA; 
} 

float getSideB() 
{ 
    float sideB; 
    cout << "\n\n" << "Side B: \n" << "Please enter the dimension: "; 
    cin >> sideB; 

    return sideB; 
} 

float calcSideC(float sideA, float sideB) 
{ 
    float total = pow(sideA, 2); 
    total = total + pow(sideB, 2); 
    return sqrt(total); 
} 

void displaySideC(float sideC) 
{ 
    cout << "The dimension of Side C is: " << sideC; 
} 
+0

沒有必要用零來初始化'sideA','sideB'和'sideC',因爲它們將在之後立即被覆蓋 –

+0

@RemyLebeau;你100%正確,但我不想改變OP的代碼風格。就像main之前的函數聲明一樣。另一個是'total = total + blah' ..我不確定他們是否知道'+ ='等等。所以出於這個原因,我試圖保持他們的代碼完全一樣,但只是修復錯誤和編譯我猜。就我個人而言,我也擺脫'使用命名空間std',因爲它使我感到困擾.. :( – Brandon