1

Error 1 error LNK2019: unresolved external symbol "bool __cdecl prefix(unsigned int,unsigned int)" (?prefix@@YA_NII@Z) referenced in function _main c:\Users\Work\documents\visual studio 2012\Projects\Book\Project5\Project5\Source.obj Project5

Error 2 error LNK1120: 1 unresolved externals c:\users\work\documents\visual studio 2012\Projects\Book\Project5\Debug\Project5.exe 1 1 Project5

I just..I dont even know what I have to ask you guys. How do I fix this?

This is the code:

#include <iostream>
#include <string>
#include <vector>
#include <math.h>


using namespace std;

void citire(vector<unsigned int> myVector,int &nrElem);
bool prefix(unsigned int nr1,unsigned int nr2);

int main(){
    int nrElem={0};
vector<unsigned int> myVector;


//citire(myVector,nrElem);
cout << prefix(123,1234);

system("pause");
return 0;
}

void citire(vector<unsigned int> myVector,int &nrElem){
    cout << "NumarElemente=" ;  
    cin >> nrElem ;

    for(int i=0;i<nrElem;i++){
        unsigned int nrCitit;
        cout << "Elem #" << i+1 <<"=";
        cin >> nrCitit;
        myVector.push_back(nrCitit);
    };

    for(int i=0;i<nrElem;i++){
        cout << myVector.at(i);
    };
}

bool prefix(unsigned int &nr1,unsigned int &nr2){
    unsigned int nr1copy=nr1;
    unsigned int nr2copy=nr2;
    int digitsNr1 = 0; while (nr1copy != 0) { nr1copy /= 10; digitsNr1++; }
    int digitsNr2 = 0; while (nr2copy != 0) { nr2copy /= 10; digitsNr1++; }
    if ( nr2/_Pow_int(10,digitsNr2-digitsNr1)==nr1) {return true;}
    else return false;
}
Xaqq
  • 4,308
  • 2
  • 25
  • 38
  • Duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?: Declared but did not define a variable or function (Function return type, parameter number and types, and calling convention do not all exactly agree)](http://stackoverflow.com/a/12574403/902497) – Raymond Chen Aug 16 '13 at 11:01

3 Answers3

1
bool prefix(unsigned int nr1,unsigned int nr2);

is not same as

bool prefix(unsigned int& nr1,unsigned int &nr2);

In forward forward declaration, you are taking the parameters by value but in the definition it is by reference. Keep the argument types same in the declaration and the definition.

unresolved external symbol "bool __cdecl prefix(unsigned int,unsigned int)"

Usually when you see these kind of linker errors, the first thing you need to check is if the function's declaration and definition signatures match or not. In this case, it is clearly not.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • Everytime somebody answers my questions I'm facepalming myself. Thank you. –  Aug 13 '13 at 17:12
1

Declaration:

bool prefix(unsigned int nr1,unsigned int nr2);

definition:

bool prefix(unsigned int &nr1,unsigned int &nr2){ ... }

See the difference? Both should be the same. Looking at your code, it looks like you should keep the version in the declaration.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

You have a prototype for the prefix() function:

bool prefix(unsigned int nr1,unsigned int nr2);

which has a signature that differs from the implementation given below:

bool prefix(unsigned int &nr1,unsigned int &nr2) {
                        ^^^               ^^^
    ....
}

Note that in the prototype, the nr1 and nr2 parameters are passed by value; instead, in the implementation signature, they are passed by reference (note the &).

Both prototype and implementation signatures should match. Fix the wrong one.
(Note: since you can't pass literals like the 123 in main() as non-const reference, I think the wrong one is the implementation signature, i.e. drop the & in the implementation signature).

Mr.C64
  • 41,637
  • 14
  • 86
  • 162