[英]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.