提问人:petergx 提问时间:1/21/2017 更新时间:1/21/2017 访问量:292
未解决的外部符号错误,仅在标头中定义函数时有效
unresolved extern symbol error, only works when define function in the header
问:
大家,我在这里的第一篇帖子,如果我的风格惹恼了你,请让我知道我正在寻找学习如何在这里发帖。我希望有人可以帮助我解决这个错误LNK2019
我有两个源文件,战舰.cpp和测试器.cpp,
该函数位于 Leetcode.cpp 中,程序不会编译并给出错误,而如果我将类函数定义放在头文件中,程序实际上会编译(到目前为止,该程序适用于原型)——尽管如此,我不确定这是否是一种好的做法,因为这些函数不是模板函数,我无法证明将它们放在文件中是合理的main()
LNK1120
LNK2019
Solution
battleship.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;
}
答:
0赞
Nikos Kazazakis
1/21/2017
#1
代码中存在多个问题:
Battleship.h
不包括在Battleship.cpp
- 您已经定义了两次
class Solution
- 您已在实现 () 文件中定义
class Solution
.cpp
- 您尚未包含在
tester.h
tester.cpp
错误消息的字面意思是编译器找不到已声明的函数的定义。这是真的; 不包括在内,因此编译器看不到定义。但是,即使已包含它,您仍然会收到错误(请参阅上面的编号项)。Battleship.cpp
评论
0赞
petergx
1/21/2017
看来我大错特错了,让我们改变它并回来
0赞
Nikos Kazazakis
1/21/2017
发生在我们最好的人身上:)
0赞
petergx
1/21/2017
您好,感谢您的帮助!删除了类的重复定义,基本上我遵循了你答案中的提示 1,2,3,而不是代码编译,但我不确定为什么要包含在 ,我的意思是,通常我们只是包含文件对吗?Solution
battleship.cpp
tester.cpp
tester.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。很高兴知道您的代码现在可以编译和执行。
评论
battleship.cpp
battleship.h
battleship.h
battleship.cpp
tom::Solution: class type definition
.cpp