ace7
ace7

Reputation: 71

Xcode linker command failed with exit code 1 c++

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?

    #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;
}

Upvotes: 7

Views: 69177

Answers (2)

mock_blatt
mock_blatt

Reputation: 965

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.

Note: You have the same problem with 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.

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 global variables. 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.

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.

Upvotes: 4

Steve Barnes
Steve Barnes

Reputation: 28360

You have several problems:

numGen - You cannot return two separate values this way

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

Says that you have 3 functions all of which return an int and are called with no parameters - which is how you call them.

So the linker is looking for functions with a fingerprint of int_questSol_void and 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_int and int_questSol_int_int.

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

Upvotes: 0

Related Questions