2013-05-19 24 views
-1

好的,所以我環顧四周,我仍然不明白爲什麼我會得到這個錯誤,我的代碼包含在下面。然後我決定做,所以你可以在應用程序每次打開時執行多個計算,在修復了其他幾個錯誤之後,這個錯誤突然出現了,在我意識到我需要「圍繞y」的時候彈出了這個錯誤。錯誤LNK2019:無法解析的符號「char_cdecl st(void)

#include "stdafx.h" 
#include <cstdio> 
#include <cstdlib> 
#include <iostream> 

using namespace std; 
int main(int nNumberofArgs, char* pszArgs[]) 

{ 

    int x; 
    int y; 
    int result; 
    char z; 
    char a = 'y'; 

    char st(); 
    { 
     cout << ("Do another calculation?: y/n"); 
     cin >> a; 
     if (a = 'n') 
     { 
      system("PAUSE"); 
      return 0; 
     } 
    } 


    while (a = 'y'); 
    { 
     cout << "Symbol here: " <<endl; 
     cin >> z; 
     cout << "Number 1: " <<endl; 
     cin >> y; 
     cout << "Number 2: " <<endl; 
     cin >> x; 
     if (z == '*') 
     { 
      result = x * y; 
      cout << "Answer:" << result <<endl; 
      st(); 
     } 
     else if (z == '/') 
     { 
      result = x * y; 
      cout << "Answer:" << result <<endl; 
      st(); 
     } 
     else if (z == '-') 
     { 
      result = x/y; 
      cout << "Answer:" << result <<endl; 
      st(); 
     } 
     else if (z == '+') 
     { 
      result = x + y; 
      cout << "Answer:" << result <<endl; 
      st(); 
     } 
     else if (z == '%') 
     { 
      result = y % x; 
      cout << "Answer:" << result <<endl; 
      st(); 
     } 
    } 
} 

回答

1

你在char st()結束分號。此聲明的功能,但沒有定義它,接着是後聲明成爲主要部分,並得到執行的第一次啓動的代碼。這就是你可能的原因沒有注意到它。

您需要將st()main中移出,因爲本地函數定義在C++中是非法的。

using namespace std; 

char st() 
{ 
    cout << ("Do another calculation?: y/n"); 
    cin >> a; 
    if (a = 'n') 
    { 
     system("PAUSE"); 
     return 0; 
    } 
} 


int main(int nNumberofArgs, char* pszArgs[]) 
{ 
    int x; 
    int y; 

    // ... rest of your code 

    return 0; 
} 
+0

謝謝!那幫了一噸!但是,現在當我啓動應用程序時(調試器看到沒有錯誤),它只是顯示一個普通的黑屏。你知道這是爲什麼嗎? –

+0

是的,當你發送數據到'std :: cout'時,你需要在末尾傳遞'std :: endl'來刷新輸出緩衝區。 'std :: cout <<「一些數據」<< std :: endl;' –

相關問題