[英]Xcode linker command failed with exit code 1 c++
我寫了一系列簡單的函數。 當我嘗試調用最后一個函數時,我收到“鏈接器命令”錯誤。 語法是正確的但我的程序不會編譯。 我錯過了什么或者這是一個IDE問題嗎?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>
using namespace std;
// Function Prototypes
int numGen ();
int questSol ();
int questAns ();
int main() {
// Store values of functions in variables
int ans = questAns();
int sol = questSol();
if (ans == sol){
cout << "Very good! Press Y to continue" << endl;
questAns();
} else {
cout << "Incorrect. Please try again" << endl;
cin >> ans;
if(ans == sol){
questAns();
}
}
return 0;
};
//Generates two random numbers between zero and ten and returns those numbers
int numGen () {
srand(time(0));
int one = rand() % 10;
int two = rand() % 10;
return one;
return two;
};
//Takes in the random numbers, multiplies them, and returns that result
int questSol (int one, int two) {
int solution = one * two;
return solution;
}
//Takes in random numbers, displays them in cout statement as question, receives and returns user answer to
//question
int questAns (int one, int two) {
int answer;
cout << "How much is " << one << " times " << two << "? \n";
cin >> answer;
return answer;
}
你轉發聲明一個函數:
int questAns ();
然后定義一個帶簽名的函數:
int questAns (int one, int two);
在C ++中,函數可以具有相同的名稱但具有不同的參數(重載函數),因此您實際上從未實際定義了您轉發的questAns然后嘗試調用。
注意:questSol遇到同樣的問題。
看起來你不太了解局部變量的范圍。
在numGen里面你定義了兩個整數,一個和兩個。 塊內定義的變量(花括號:{})僅存在於該塊內。 它們是當地的。 標識符僅在其定義的最內部塊中有效,並且一旦退出,則釋放該內存。 像你正在嘗試的那樣返回兩個整數也是不可能的。
看起來您期望這些整數可用於您的其他兩個功能。
你可以做的最小的改變是使一個和兩個全局變量成為int。 這意味着您可以在任何塊之外定義它們(通常位於代碼的最頂部)。 然后從函數定義中刪除參數列表,因為所有函數都可以看到全局變量。 這通常被認為是錯誤的編程實踐,因為在更復雜的程序中,全局變量會對代碼造成嚴重破壞,但是在這個簡單的程序中,它會起作用並讓您有機會練習理解變量范圍。
另一個解決方案,更符合您的嘗試,是定義兩個整數的ARRAY,然后返回。 然后將該數組傳遞給另外兩個函數。 這是一個更好的方法,讓你有機會了解數組。
你有幾個問題:
numGen
- 您不能以這種方式返回兩個單獨的值
// Function Prototypes
int numGen ();
int questSol ();
int questAns ();
說有3個函數,所有函數都返回一個int
並且沒有參數調用 - 這就是你調用它們的方式。
因此鏈接器正在尋找具有int_questSol_void
和int_questAns_void
指紋的函數 - 然后您聲明兩個返回int的函數並將其作為輸入3個整數 - 這些函數具有int_questAns_int_int
和int_questSol_int_int
指紋。
結果鏈接器呻吟着你正在調用它找不到的函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.