C++ compiler error in CodeBlocks & Xcode

23 views Asked by At

I'm currently learning to become a programmer and while I was learning I came across a bit of a problem. This program doesn't run and gives me an error

clang: error: linker command failed with exit code 1 (use -v to see invocation)

Why is this and how do I fix it and prevent it from happening again?

#include <iostream>
using namespace std;

void Fav();
int main()
{
    Fav();
    return 0;
}
void Fav(int x)
{
    cout<<"Troy's Favorite Number is \n"<<x;
}
1

There are 1 answers

1
Cory Kramer On

The declared function and defined function are different. They are therefore different functions, the former of which is never defined, even though it is called in main

void Fav();      // Declared
void Fav(int x)  // Defined

You need to change the signature of the declared function to matche the declared and called function

void Fav(int x);
int main()
{
    int x;
    cin >> x;
    Fav(x);
    return 0;
}

void Fav(int x)
{
    cout<<"Troy's Favorite Number is \n" << x;
}