声明表变量时未解析的外部符号
Unresolved external symbol when declaring a table variable
我正在尝试使用我的 table1.h 文件和我的 table1.template 文件,但每次我声明一个数据类型的表变量时,我都会收到此错误
1>DataStructuresLab21V2.obj : error LNK2019: unresolved external symbol "public: __thiscall table<char>::table<char>(void)" (??0?$table@D@@QAE@XZ) referenced in function _wmain
我已经检查了拼写,我已经在我的头文件中声明table()
并在我的 table1.template 文件中实现,标题和模板文件之间的大小写是正确的。我不知道这个错误在我的代码中的位置,任何帮助识别任何有错误的地方将不胜感激。编程语言是c ++,是在Visual Studio 2013中完成的。头文件和模板文件是由我的教授提供的,用于修改文件以实现双重哈希的作业。提前致谢
这是我的头文件,名为table1.h
// FILE: table1.h (part of the namespace main_savitch_12A)
// TEMPLATE CLASS PROVIDED: table<RecordType> (a table of records with keys).
// This class is a container template class for a table of records.
// The template parameter, RecordType, is the data type of the records in the
// table. It may be any of the bulit-in C++ types (int, char, etc.), or a
// class with a default constructor, an assignment operator, and an integer
// member variable called key.
//
// MEMBER CONSTANT for the table<RecordType> class:
// static const size_type CAPACITY = ________
// table<RecordType>::CAPACITY is the maximum number of records held by a table.
//
// CONSTRUCTOR for the table<RecordType> template class:
// table( )
// Postcondition: The table has been initialized as an empty table.
//
// MODIFICATION MEMBER FUNCTIONS for the table<RecordType> class:
// void insert(const RecordType& entry)
// Precondition: entry.key >= 0. Also if entry.key is not already a key in
// the table, then the table has space for another record
// (i.e., size( ) < CAPACITY).
// Postcondition: If the table already had a record with a key equal to
// entry.key, then that record is replaced by entry. Otherwise, entry has
// been added as a new record of the table.
//
// void remove(int key)
// Postcondition: If a record was in the table with the specified key, then
// that record has been removed; otherwise the table is unchanged.
//
// CONSTANT MEMBER FUNCTIONS for the table<RecordType> class:
// bool is_present(const Item& target) const
// Postcondition: The return value is true if there is a record in the
// table with the specified key. Otherwise, the return value is false.
//
// void find(int key, bool& found, RecordType& result) const
// Postcondition: If a record is in the table with the specified key, then
// found is true and result is set to a copy of the record with that key.
// Otherwise found is false and the result contains garbage.
//
// size_type size( ) const
// Postcondition: Return value is the total number of records in the
// table.
//
// VALUE SEMANTICS for the table<RecordType> template class:
// Assignments and the copy constructor may be used with table objects.
#ifndef TABLE1_H
#define TABLE1_H
#include <cstdlib> // Provides size_t
using namespace std;
template <class RecordType>
class table
{
public:
// MEMBER CONSTANT -- See Appendix E if this fails to compile.
static const size_t CAPACITY = 811;
// CONSTRUCTOR
table();
// MODIFICATION MEMBER FUNCTIONS
void insert(const RecordType& entry);
void remove(int key);
// CONSTANT MEMBER FUNCTIONS
bool is_present(int key) const;
void find(int key, bool& found, RecordType& result) const;
size_t size() const { return used; }
private:
// MEMBER CONSTANTS -- These are used in the key field of special records.
static const int NEVER_USED = -1;
static const int PREVIOUSLY_USED = -2;
// MEMBER VARIABLES
RecordType data[CAPACITY];
size_t used;
// HELPER FUNCTIONS
size_t hash(int key) const;
size_t hash2(int key) const;
size_t next_index(size_t index) const;
void find_index(int key, bool& found, size_t& index) const;
bool never_used(size_t index) const;
bool is_vacant(size_t index) const;
};
#endif
这是我的模板文件
#include "stdafx.h"
#include <cassert>
#include <cstdlib>
#include "table1.h"
using namespace std;
template<class RecordType>
size_t table<RecordType>::CAPACITY;
template <class RecordType>
const int table<RecordType>::NEVER_USED;
template <class RecordType>
const int table<RecordType>::PREVIOUSLY_USED;
template<class RecordType>
table<RecordType>::table()
{
size_t i;
used = 0;
for (i = 0; i < CAPACITY; i++)
data[i].key = NEVER_USED;
}
template<class RecordType>
void table<RecordType>::insert(const RecordType& entry)
{
bool already_present;
size_t index;
assert(entry.key >= 0)
find_index(entry.key, already_present, index);
if (!already_present)
{
assert(size() < CAPACITY)
index = hash(entry.key);
while (!is_vacant(index))
index = next_index(index);
++used;
data[index] = entry;
}
else if (already_present)
{
assert(size() < CAPACITY)
index = hash(entry.key) - hash2(entry.key);
while (!is_vacant(index))
index = next_index(index);
++used;
data[index] = entry;
}
}
template <class RecordType>
void table<RecordType>::remove(int key)
// Library facilities used: cassert
{
bool found; // True if key occurs somewhere in the table
std::size_t index; // Spot where data[index].key == key
assert(key >= 0);
find_index(key, found, index);
if (found)
{ // The key was found, so remove this record and reduce used by 1.
data[index].key = PREVIOUSLY_USED; // Indicates a spot that's no longer in use.
--used;
}
}
template <class RecordType>
bool table<RecordType>::is_present(int key) const
// Library facilities used: assert.h
{
bool found;
std::size_t index;
assert(key >= 0);
find_index(key, found, index);
return found;
}
template <class RecordType>
void table<RecordType>::find(int key, bool& found, RecordType& result) const
// Library facilities used: cassert.h
{
size_t index;
assert(key >= 0);
find_index(key, found, index);
if (found)
result = data[index];
}
template <class RecordType>
inline size_t table<RecordType>::hash(int key) const
{
return (key % CAPACITY);
}
template<class RecordType>
inline size_t table<RecordType>::hash2(int key) const
{
return(key % hash(key));
}
template <class RecordType>
inline size_t table<RecordType>::next_index(size_t index) const
// Library facilities used: cstdlib
{
return ((index + 1) % CAPACITY);
}
template <class RecordType>
void table<RecordType>::find_index(int key, bool& found, size_t& i) const
// Library facilities used: cstdlib
{
size_t count; // Number of entries that have been examined
count = 0;
i = hash(key);
while ((count < CAPACITY) && (data[i].key != NEVER_USED) && (data[i].key != key))
{
++count;
i = next_index(i);
}
found = (data[i].key == key);
}
template <class RecordType>
inline bool table<RecordType>::never_used(size_t index) const
{
return (data[index].key == NEVER_USED);
}
template <class RecordType>
inline bool table<RecordType>::is_vacant(size_t index) const
{
return (data[index].key == NEVER_USED) || (data[index].key == PREVIOUSLY_USED);
}
您不会#include
头文件中的 table1.template 文件。
头文件中#endif
之前的最后一行必须#include "table1.template"
例如:
bool is_vacant(size_t index) const;
};
#include "table1.template"
#endif
此外,还必须从模板文件中删除#include "table1.h"
行:
#include "stdafx.h"
#include <cassert>
#include <cstdlib>
#include "table1.h" //<-- Remove this line.
或者,您可以在使用它们的任何地方#include
这两个文件:
#include "table1.h"
#include "table1.template"
#include "SomeType.h"
// Other #include statements here.
int main()
{
table<SomeType> myTable;
// ...stuff...
return 0
}
每当使用模板时,模板类都必须具有可用的所有定义,因为.template
文件的内容不是以标准 .h/.cpp 方式编译的。这是因为为您使用的每个不同<class RecordType>
都定义了单独的类实例,因此编译器在使用 table<MyType>
之前需要同时#included
.h 文件和 .template 文件。
相关文章:
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 未解析的外部符号_MsiLocateComponentW@12.
- 在C++中使用 gRPC 时未解析的外部符号
- LibPrivoxy: 未解析的外部符号 __declspec(dllimport) int __stdcall Sta
- 尝试从 XamlApplication 编译 C++/WinRT 空白应用程序时,我收到未解析的外部符号 winrt_make_*
- 我收到错误LNK2001:未解析的外部符号(C++代码)
- 可视抽象类 c++(错误 LNK 2001:未解析的外部符号)
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 仅在少数方法(静态或共享库)中解析的外部符号
- 未解析的外部符号"__declspec(dllimport)与 Spdlog c++ 库
- 函数 _main (OPENGL C++) 中引用的未解析的外部符号 ________
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 使用静态 constexpr 成员的未解析外部符号
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- Dlib LNK2001未解析的外部符号USER_ERROR__consistent_build_configurati
- 矛盾的未解决的外部符号+未使用的库与VS2017和FFMPEG 4
- 链接器错误:切换到unicode生成给出:未解析的外部符号WinMain
- 类模板上一个特定函数的未解析外部符号
- LNK2001:未解析的外部符号public:static类std::vector