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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33338566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:15:27  来源:igfitidea点击:

Xcode linker command failed with exit code 1 c++

c++xcode

提问by ace7

I've written a simple series of functions. When I try to call the last function I get the "linker command" error. The syntax is correct but my program won't compile. Am I missing something or is this an IDE issue?

我已经编写了一系列简单的函数。当我尝试调用最后一个函数时,出现“链接器命令”错误。语法正确,但我的程序无法编译。我错过了什么还是这是一个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;
}

采纳答案by mock_blatt

You forward declare a function:

你转发声明一个函数:

int   questAns ();

And then later define a function with a signature:

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

int questAns (int one, int two);

In C++, functions can have the same name but have different parameters (overloaded functions), so you've never actually defined the questAns that you forward declare and then try to call.

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

Note: You have the same problem with questSol.

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

It looks like you don't quite understand the scope of local variables.

看起来你不太了解局部变量的作用域。

Inside numGen you define two ints, one and two. Variables defined within a block (curly braces: {}) exist only within that block. They are local to it. The identifier is only valid within the inner-most block it's defined in, and once you exit it that memory is freed. Returning two ints like you're trying is also impossible.

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

It looks like you're expecting those ints to be available to your other two functions.

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

The smallest change you could make is to make int one and two globalvariables. This means you define them outside of any block (usually at the very top of your code). Then remove the parameter lists from your function definitions, because all the functions can see the global variables. That's generally considered bad programming practice because in more complex programs globals wreak havoc on your code, but in this simple program it'd work and give you a chance to practice understanding variable scope.

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

Another solution, more in line with what you were trying, is to define an ARRAY of two ints, and return that. Then pass that array to the other two functions. That'd be a better way to do it, and give you a chance to learn about arrays.

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

回答by Steve Barnes

You have several problems:

你有几个问题:

numGen- You cannot return two separate values this way

numGen- 您不能以这种方式返回两个单独的值

// Function Prototypes
int   numGen   ();
int   questSol ();
int   questAns ();

Says that you have 3 functions all of which return an intand are called with noparameters - which is how you call them.

说,你有3个功能,所有这些返回的int,被称为与没有参数-这就是你怎么称呼他们。

So the linker is looking for functions with a fingerprint of int_questSol_voidand int_questAns_void- you then declare two functions that return an int and take as inputs 3 ints - these have fingerprints of int_questAns_int_intand int_questSol_int_int.

所以链接器正在寻找指纹为int_questSol_voidand 的函数int_questAns_void——然后你声明两个函数,它们返回一个 int 并接受 3 个 int 作为输入——它们的指纹为int_questAns_int_intand int_questSol_int_int

As a result the linker is moaning that you are calling to functions that it can't find.

结果,链接器抱怨您正在调用它找不到的函数。