Xcode链接器命令失败,退出代码为1 c ++

ace*_*ce7 7 c++ xcode

我写了一系列简单的函数.当我尝试调用最后一个函数时,我收到"链接器命令"错误.语法是正确的但我的程序不会编译.我错过了什么或者这是一个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;
}
Run Code Online (Sandbox Code Playgroud)

moc*_*att 4

您转发声明一个函数:

int   questAns ();
Run Code Online (Sandbox Code Playgroud)

然后定义一个带有签名的函数:

int questAns (int one, int two);
Run Code Online (Sandbox Code Playgroud)

在 C++ 中,函数可以具有相同的名称,但具有不同的参数(重载函数),因此您从未真正定义过您转发声明然后尝试调用的 questAns。

注意:您对 questSol 也有同样的问题。

看来你不太了解局部变量的范围。

在 numGen 中,您定义了两个整数,一和二。块内定义的变量(花括号:{})仅存在于该块内。他们是当地的。该标识符仅在其定义的最内层块内有效,一旦退出该块,内存就会被释放。像您尝试的那样返回两个整数也是不可能的。

看来您希望这些整数可用于其他两个函数。

您可以做的最小更改是将 int 1 和 2 设为全局变量。这意味着您可以在任何块之外定义它们(通常在代码的最顶部)。然后从函数定义中删除参数列表,因为所有函数都可以看到全局变量。这通常被认为是不好的编程实践,因为在更复杂的程序中,全局变量会对您的代码造成严重破坏,但在这个简单的程序中,它会起作用,并给您一个练习理解变量范围的机会。

另一个解决方案更符合您的尝试,是定义两个整数的数组,然后返回它。然后将该数组传递给其他两个函数。这将是一个更好的方法,并且让您有机会了解数组。