错误 LNK2019:未解析的外部符号

4

好的,我遇到了一个问题,试图找出我的代码中的问题。我有很多代码,所以我只会发布与编译时出错相关的部分。我在一个类的内部有以下函数,它可以编译并且一切都运行得很好,直到我调用"CalculateProbabilityResults"函数并运行其中的第7行代码。我在程序中已经"取消注释"了这行代码,以便您更容易找到它。我相信我需要正确的#include指令,因为当不调用该函数时,它可以编译通过,那么这可能不是问题吗?我知道我的一些命名符号需要一点帮助,所以请谅解。感谢大家提前的帮助。

int SQLServer::CalculateProbabilityResults(int profile, int frame, int time_period, int TimeWindowSize) {
    ofstream ResultFile;
    stringstream searchFileName;
    stringstream outputName;
    vector<vector<int>> timeFrameItemsets;
    int num = getTimeFrameFile(frame*TimeWindowSize, TimeWindowSize);
    cout << num << endl;

    //outputName << "Results" << getTimeFrameFile((frame*TimeWindowSize), TimeWindowSize) << ".csv";
    cout << outputName.str() << endl;
    outputName.clear();
    //ResultFile.open(outputName.str().c_str());
    ResultFile.close();
    result.resize(0);
    return 0;
}

int getTimeFrameFile(int timeInHours, int timeFrameSize) {
    int fileNum = 0;
    int testWin;
    if (timeInHours > 24) {
        while (timeInHours >24)
            timeInHours -= 24;
    }
    for (testWin = 0; testWin < 24/timeFrameSize; testWin++) {
        if (timeInHours >= testWin*timeFrameSize && timeInHours < (testWin+1)*timeFrameSize)
            fileNum = testWin+1;
    }
    if (fileNum == 0)
        fileNum = testWin+1;
    return fileNum;
}

通话记录
1>------ Rebuild All started: Project: MobileSPADE_1.3, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'MobileSPADE_1.3', configuration 'Debug|Win32'
1>Compiling...
1>main.cpp
1>MobileSPADE.cpp
1>SQLServer.cpp
1>Generating Code...
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>Linking...
1>LINK : C:\Users\JoshBradley\Desktop\MobileSPADE_1.3\MobileSPADE_1.3\Debug\MobileSPADE_1.3.exe not found or not built by the last incremental link; performing full link
1>SQLServer.obj : error LNK2019: unresolved external symbol "public: int __thiscall SQLServer::getTimeFrameFile(int,int)" (?getTimeFrameFile@SQLServer@@QAEHHH@Z) referenced in function "public: int __thiscall SQLServer::CalculateProbabilityResults(int,int,int,int)" (?CalculateProbabilityResults@SQLServer@@QAEHHHHH@Z)
1>C:\Users\JoshBradley\Desktop\MobileSPADE_1.3\MobileSPADE_1.3\Debug\MobileSPADE_1.3.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Users\JoshBradley\Desktop\MobileSPADE_1.3\MobileSPADE_1.3\MobileSPADE_1.3\Debug\BuildLog.htm"
1>MobileSPADE_1.3 - 2 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
2个回答

7
编译器认为 getTimeFrameFile 是一个 SQLServer 方法:
未解决的外部符号 "public: int __thiscall SQLServer::getTimeFrameFile(int,int)"
但你将其定义为自由函数:
int getTimeFrameFile(int timeInHours, int timeFrameSize) {

将这个自由函数改为类方法可以解决问题:
int SQLServer::getTimeFrameFile(int timeInHours, int timeFrameSize)

谢谢...解决了!我个人更偏向于理论,所以偶尔会犯错误,忘记像告诉编译器函数属于哪个类这样的语法问题,哈哈。 - Josh Bradley

1
将函数getTimeFrameFile放在SQLServer::CalculateProbabilityResults之上。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接