未解析的外部符号

Unresolved external symbol

本文关键字:外部 符号      更新时间:2023-10-16

错误1错误LNK2019:未解析的外部符号"bool __cdeclprefix(unsigned int,unsigned整型)"(?prefix@@YA_NII@Z)中引用函数(_main)c:\Users\Work\documents\visual studio2012\项目\书籍\项目5\项目5\Source.obj项目5

错误2错误LNK1120:1未解析externals c:\users\work\documents\visualstudio2012\Projects\Book\Project5\Debug\Project5.exe 1 1 Project5

我只是。。我甚至不知道该问你们什么。我该如何解决这个问题?

这是代码:

#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;
}
bool prefix(unsigned int nr1,unsigned int nr2);

与不同

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

在正向-正向声明中,您按值获取参数,但在定义中是按引用获取参数。在声明和定义中保持参数类型相同。

未解析的外部符号"bool __cdecl前缀(无符号int,无符号int)"

通常,当您看到此类链接器错误时,首先需要检查的是函数的声明和定义签名是否匹配。在这种情况下,显然不是。

声明:

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

定义:

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

看到区别了吗?两者应该相同。查看您的代码,您似乎应该将版本保留在声明中。

prefix()函数有一个原型

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

其签名与下面给出的实现不同:

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

注意,在原型中,nr1nr2参数通过值传递;相反,在实现签名中,它们是通过引用传递的(注意&)。

原型签名和实现签名都应该匹配。修错了
(注意:由于不能将main()中的123等文本作为非const引用传递,我认为错误的是实现签名,即在实现签名中丢弃&)。