0

私は一連の素数を検索するプログラムを書いていましたが、進行状況を確認するために途中で、すべてが正常に機能していることを確認するためにビルドすることにしました.LNK2019エラーが発生し続けます! それは未解決の外部です.私はいくつかの調査を行いましたが、私は何も理解していません. これがコードです。

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

3 に答える 3

3

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.

于 2013-03-07T22:27:15.523 に答える
1

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).

于 2013-03-07T22:27:14.473 に答える