简体   繁体   中英

Error LNK2019 C++ Unbearably simple program

I was writing a program to search fro a range of prime numbers, and about halfway through to check my progress I decided to build it to make sure everything is working okay, I keep getting error LNK2019! It says it is an unresolved external.I did some research but I don't understand much of anything. Here is the code.

#include <iostream>

using namespace std;

int singlePrime(int subjectNumber);

int main() {
    cout<<"Would you like to find a single prime number(1), or a range(2)?"<<endl;

    int methodchoice;
    cin>>methodchoice;

    if(methodchoice ==1) {
        int subjectNumber;
        cout<<"Which number would you like to test for primeness?"<<endl;
        cin>>subjectNumber;
        int singlePrime(subjectNumber);
    }

    if(methodchoice==2) {
        int  lowRange;
        int highRange;

        cout<<"Input the low value for your range."<<endl;
        cin>> lowRange;

        cout<<"Input the high value for your range"<<endl;
        cin>> highRange;

        for (int index=lowRange; index<highRange;index++) {
            if (index=highRange) {
                break;
            }

            singlePrime(index);
        }
    }
}

Here you declare a function that you never define :

int singlePrime(int subjectNumber);

The linker complains because you invoke this function, but its body is found nowhere.

To verify that this is the problem, replace the declaration with a definition containing some dummy implementation:

int singlePrime(int subjectNumber)
{
    return 0;
}

Also notice, that you have a useless initialization of an integer called singlePrime here:

if (methodchoice ==1) {
    int subjectNumber;
    cout<<"Which number would you like to test for primeness?"<<endl;
    cin>>subjectNumber;
    int singlePrime(subjectNumber);
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Why this?
}

You probably meant this line to do something else (most likely invoke the singlePrime() function), since singlePrime won't be visible outside that block's scope.

It's probably flagging this function prototype:

int singlePrime(int subjectNumber);

You haven't defined a body for the function. You need to implement it (or at least give it a dummy implementation).

Well, my psychic debugging skills have pinpointed the problem. The following code:

int singlePrime(int subjectNumber);

tells the compiler that there exists a function called singlePrime which takes an int and returns an int .

Of course, you then never provide the code for that function... The compiler assumes it's in some other .cpp file and says "oh, well, the linker will take care of that."

And when the linker comes along, it sees that it's supposed to find a function called singlePrime which accepts an int and returns an int . But that function is nowhere to be found.

Simple fix, change:

int singlePrime(int subjectNumber);

into

int singlePrime(int subjectNumber)
{
  // some code here to do whatever singlePrime is supposed to do
  // be sure to return the correct number. For now, return the 
  // number of the beast!

  return 666;
}

Further down in your code, you seem to try to call this function:

if (methodchoice ==1) {
    int subjectNumber;
    cout<<"Which number would you like to test for primeness?"<<endl;
    cin>>subjectNumber;
    int singlePrime(subjectNumber); // What?
}

But this isn't how you call functions in C or C++. You should take a closer look at your book or class notes. You would do something like this:

// call singlePrime and store the result in a variable called
// ret so that we can use it.
int ret = singlePrime(subjectNumber);

And for future reference, it would help if you posted the complete error message you get. You know, in case our crystal balls are malfunctioning because of solar flares.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM