Search code examples
c++templatesheaderlnk2019unresolved-external

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


everyone, my first post here, if my style annoys your please let me know I am looking to learn how to post here. I hope someone could help me with this LNK2019 Error

I got two source files, battleship.cpp and tester.cpp, the main() function is inside Leetcode.cpp the program will not compile and give error LNK1120 and LNK2019 while if I put the Solution class function definition in the header file battleship.h the program actually compile(and so far works well for the prototype) -- still, I am not sure if this is a good practice as these functions are not template functions and I can not justify putting them in the .h file

Error Message:

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   

and code for battleship.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;
        }
    };
}

and code for 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);
    };
}

code for tester.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;
    }
}

code for tester.h

#pragma once
#include "stdafx.h"

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


}

code for Leetcode.cpp also where main function is

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

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

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

Solution

  • You have multiple problems in your code:

    1. Battleship.h is not included in Battleship.cpp
    2. You have defined class Solution twice
    3. You have defined class Solution in an implementation (.cpp) file
    4. You have not included tester.h in tester.cpp

    The error message literally means that the compiler cannot find the definition of a function you have declared. This is true; Battleship.cpp is not included so there is not definition visible to the compiler. However, even if it had been included, you would still get errors (see numbered items above).