未解决的外部符号错误,仅在标头中定义函数时有效

unresolved extern symbol error, only works when define function in the header

提问人:petergx 提问时间:1/21/2017 更新时间:1/21/2017 访问量:292

问:

大家,我在这里的第一篇帖子,如果我的风格惹恼了你,请让我知道我正在寻找学习如何在这里发帖。我希望有人可以帮助我解决这个错误LNK2019

我有两个源文件,战舰.cpp测试器.cpp, 该函数位于 Leetcode.cpp 中,程序不会编译并给出错误,而如果我将类函数定义放在头文件中,程序实际上会编译(到目前为止,该程序适用于原型)——尽管如此,我不确定这是否是一种好的做法,因为这些函数不是模板函数,我无法证明将它们放在文件中是合理的main()LNK1120LNK2019Solutionbattleship.h.h

错误信息:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol "public: int __thiscall tom::Solution::boardPrinter(class std::vector<class std::vector<char,class std::allocator<char> >,class std::allocator<class std::vector<char,class std::allocator<char> > > > &)" (?boardPrinter@Solution@tom@@QAEHAAV?$vector@V?$vector@DV?$allocator@D@std@@@std@@V?$allocator@V?$vector@DV?$allocator@D@std@@@std@@@2@@std@@@Z) referenced in function "int __cdecl tom::battleshipTester(void)" (?battleshipTester@tom@@YAHXZ)  Leetcode    C:\Users\Shin\documents\visual studio 2015\Projects\Leetcode\Leetcode\tester.obj    1   

战列舰代码 .cpp

#include "stdafx.h"


namespace tom{
    class Solution{
    public:
        int countBattleships(std::vector<std::vector<char>>& board) {
            return 0;
        }

        int boardPrinter(std::vector<std::vector<char>>& board)
        {   
            return 0;
        }

        int Solution::simpleBoardBuilder(std::vector<std::vector<char>>& board)
        {
            return 0;
        }
    };
}

battleship.h 的代码

#pragma once
#include "stdafx.h"


namespace tom {
    class Solution {
    public:
        int countBattleships(std::vector<std::vector<char>>& board);
        int boardPrinter(std::vector<std::vector<char>>& board);
        int simpleBoardBuilder(std::vector<std::vector<char>>& board);
    };
}

测试人员代码 .cpp

#include "stdafx.h"
#include "battleship.h"

namespace tom {
    int battleshipTester(void)
    {
        //called in main function
        //call countBattleships
        std::vector<std::vector<char>> board;
        Solution baseline;
        baseline.countBattleships(board);
        baseline.boardPrinter(board);
        baseline.simpleBoardBuilder(board);
        return 0;
    }
}

tester.h 的代码

#pragma once
#include "stdafx.h"

namespace tom {
    int battleshipTester(void);
    //int simple


}

Leetcode.cpp 的代码也是主要功能所在

// Leetcode.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "tester.h"

int main()
{   
    tom::battleshipTester();
    return 0;
}
C++ 模板 标头 LNK2019 未解析的外部

评论

0赞 Sam Varshavchik 1/21/2017
这是未定义的行为。头文件定义给定名称的类。cpp 文件定义了另一个具有相同名称的类。
0赞 WhozCraig 1/21/2017
我觉得你的不包括有点奇怪.不是吗?battleship.cppbattleship.h
0赞 Abhinav 1/21/2017
链接库问题与未链接到项目的库有关,您是否将库链接到 Leetcode.cpp 所在的项目?
0赞 petergx 1/21/2017
@SamVarshavchik @WhozCraig你们俩都是对的,如果我在文件中包含,控制台会说这就是我删除它的原因,但事实是我也不确定我应该在文件本身包含什么标题?我的意思是通常我会这样做,但我不知道为什么,你能告诉我吗?battleship.hbattleship.cpptom::Solution: class type definition.cpp
0赞 petergx 1/21/2017
@SamVarshavchik顺便说一句,什么是未定义的?

答:

0赞 Nikos Kazazakis 1/21/2017 #1

代码中存在多个问题:

  1. Battleship.h不包括在Battleship.cpp
  2. 您已经定义了两次class Solution
  3. 您已在实现 () 文件中定义class Solution.cpp
  4. 您尚未包含在tester.htester.cpp

错误消息的字面意思是编译器找不到已声明的函数的定义。这是真的; 不包括在内,因此编译器看不到定义。但是,即使已包含它,您仍然会收到错误(请参阅上面的编号项)。Battleship.cpp

评论

0赞 petergx 1/21/2017
看来我大错特错了,让我们改变它并回来
0赞 Nikos Kazazakis 1/21/2017
发生在我们最好的人身上:)
0赞 petergx 1/21/2017
您好,感谢您的帮助!删除了类的重复定义,基本上我遵循了你答案中的提示 1,2,3,而不是代码编译,但我不确定为什么要包含在 ,我的意思是,通常我们只是包含文件对吗?Solutionbattleship.cpptester.cpptester.h.h
0赞 Nikos Kazazakis 1/21/2017
对不起,错别字,情况正好相反,您需要.cpp中包含的 .h
0赞 Nikos Kazazakis 1/21/2017
您仍然需要为 Solution 类方法提供定义,否则它们仍未定义。如果在实施所有更改后它仍然不起作用,请随时创建另一个问题(因为它可能看起来太不同了):)
0赞 Rishi 1/21/2017 #2

除了 nikaza 的回答之外,请参阅以下评论。

countBattleships(std::vector<std::vector<char>>& board)

是错误的。在下面这样之后,您应该有一个额外的空间<char>

countBattleships(std::vector<std::vector<char> >& board)

只有这样,您的代码才应该被编译。

虽然您到处都使用过,但我在任何文件中都看不到任何文件。您可能希望在 .h 文件中vector#include <vector>#include <vector>

战舰 .cpp 应如下所示:

#include "battleship.h"
namespace tom{
    int Solution::countBattleships(std::vector<std::vector<char> >& board) {
        return 0;
    }

    int Solution::boardPrinter(std::vector<std::vector<char> >& board)
    {
        return 0;
    }

    int Solution::simpleBoardBuilder(std::vector<std::vector<char> >& board)
    {
        return 0;
    }
}

tester.cpp 应该看起来像这样:

#include "tester.h"
#include "battleship.h"

namespace tom {
    int battleshipTester(void)
    {
        //called in main function
        //call countBattleships
        std::vector<std::vector<char> > board;
        Solution baseline;
        baseline.countBattleships(board);
        baseline.boardPrinter(board);
        baseline.simpleBoardBuilder(board);
        return 0;
    }
}

battleship.h 应如下所示:

#pragma once
#include <vector>
namespace tom {
    class Solution {
    public:
        int countBattleships(std::vector<std::vector<char> >& board);
        int boardPrinter(std::vector<std::vector<char> >& board);
        int simpleBoardBuilder(std::vector<std::vector<char> >& board);
    };
}

tester.h 应如下所示:

#pragma once

namespace tom {
    int battleshipTester(void);
    //int simple
}

最后,leetcode.cpp 应该如下所示:

#include "tester.h"

int main()
{
    tom::battleshipTester();
    return 0;
}

评论

0赞 petergx 1/22/2017
感谢您的回复,经过一些修改,我的代码现在看起来确实像您的代码。我包含在我可能使用的其他库中,因为它只是一个小项目,我不是在寻找加快编译时间,但仍然不确定这是否是一种好的做法?(虽然看起来工作正常)<vector>stdafx.h
0赞 Rishi 1/23/2017
有关stdafx.h的更多信息,请访问:stackoverflow.com/questions/2976035/purpose-of-stdafx-h。很高兴知道您的代码现在可以编译和执行。