C++ Xcode链接器命令失败,退出代码为1 c++;
我已经编写了一系列简单的函数。当我试图调用最后一个函数时,我得到了“linker command”错误。语法正确,但我的程序无法编译。我是否遗漏了什么,或者这是IDE问题C++ Xcode链接器命令失败,退出代码为1 c++;,c++,xcode,C++,Xcode,我已经编写了一系列简单的函数。当我试图调用最后一个函数时,我得到了“linker command”错误。语法正确,但我的程序无法编译。我是否遗漏了什么,或者这是IDE问题 #include <iostream> #include <cstdlib> #include <ctime> #include <time.h> using namespace std; // Function Proto
#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
//功能原型
int numGen();
int questSol();
int questAns();
int main(){
//将函数值存储在变量中
int ans=questAns();
int sol=questSol();
如果(ans==sol){
您是否可以向前声明一个函数:
int questAns ();
然后定义一个带有签名的函数:
int questAns (int one, int two);
<>在C++中,函数可以有相同的名称,但有不同的参数(重载函数),因此,您从未真正定义过向前声明并尝试调用的Quests.
注意:questSol也有同样的问题
看起来您不太了解局部变量的范围
在numGen中定义了两个整数,一个和两个。在一个块中定义的变量(大括号:{})仅存在于该块中。它们是该块的本地变量。标识符仅在其定义的最内部块中有效,一旦退出该块,内存将被释放。返回两个您正在尝试的整数也是不可能的
看起来您希望其他两个函数可以使用这些int
您可以做的最小更改是将int设置为一个和两个全局变量。这意味着您可以在任何块之外(通常在代码的最顶端)定义它们。然后从函数定义中删除参数列表,因为所有函数都可以看到全局变量。这通常被认为是不好的编程实践,因为在更复杂的程序中,全局变量会对代码造成严重破坏,但在这个简单的程序中,它会起作用,让您有机会练习理解变量范围
另一种更符合您尝试的解决方案是定义一个由两个整数组成的数组,并返回该数组。然后将该数组传递给其他两个函数。这是一种更好的方法,让您有机会了解数组。您有几个问题:
numGen
-不能以这种方式返回两个单独的值
// Function Prototypes
int numGen ();
int questSol ();
int questAns ();
表示您有3个函数,所有函数都返回一个int
,并使用no参数调用-这就是您调用它们的方式
因此,链接器正在寻找指纹为int\u questSol\u void
和int\u questAns\u void
的函数-然后声明两个函数,返回一个int并将3个int作为输入-它们的指纹分别为int\u questAns\u int
和int\u questSol\u int
结果,链接器抱怨说您正在调用它找不到的函数。现在我"我不知道这应该如何工作。全局变量应该是完美的,除非它们不允许用于此练习。数组还没有被涵盖。最初的赋值要求我在应用程序开始时以及在用户得到正确答案后调用函数。然后我需要在循环中使用这些值,以便重新生成一个问题以获得正确的响应,或者等待用户得到正确的响应。这个旧的答案对我帮助很大!最重要的提示是,确保您的函数名和签名匹配!