使用 unordered_map 而不是映射时出现链接器错误?

Linker error when using unordered_map but not map?

这是一个很奇怪的问题。我有两个 classes:一个自定义控制台 class (CConsole) 和一个测试 class (CHashTableTest),我用它来玩地图和 unordered_maps 来学习他们是如何工作的。

在我的控制台中 class 我有一个 CConsole 的 public 静态成员变量,它向项目的其余部分公开了一个静态控制台对象,这样我就可以随时写入这个控制台.这适用于我所有的 classes,包括测试 class 但 只有 当测试 classes 使用 map 而不是 unordered_map!

我收到的错误是:

错误 LNK2001:无法解析的外部符号 "public static class CConsole CConsole:output" (?output@CConsole@@2V1@A)

它来自调用测试方法 class 的 class 而不是测试 class 本身,但在调用 class 时没有发生任何奇怪的事情,它只是实例化 CHashTableTest 对象(传入 CConsole 对象)并对其调用 Add 和 Get。它被放置在一个单独的项目中,但是当我使用 map 时这不是问题,因为使用 _declspec(ddlexport).

将静态成员变量设置为外部变量

该解决方案是在 Visual Studio 2012 年设置的,CConsole 和 CHashTableTest classes 在一个 DLL 项目中,该项目是存在调用代码的单元测试项目的外部引用。

CConsole 和 CHashTableTest 文件:

Console.h

#ifndef _CONSOLE_H_
#define _CONSOLE_H_

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <time.h>

// Defines a set of output modes for the CConsole class. This will affect what output the console will target.
// The default is COUT.
enum _declspec(dllexport) EConsoleMode 
{ 
   // Output sent to a CConsole object will be printed using cout.
   COUT,
   // Output sent to a CConsole object will be printed to a file.
   OUTPUT_FILE,
   // Output sent to a CConsole object will be printed using OutputDebugString.
   OUTPUT_DEBUG_STRING, 
   // Output sent to a CConsole object will be printed using Debug::WriteLine.
   DEBUG_WRITE_LINE, 
   // Output sent to a CConsole object will be printed using Console::WriteLine.
   CONSOLE_WRITE_LINE,
   // Output sent to a CConsole object will not be printed.
   NO_OUTPUT,
};

// An output wrapper class that allows logging and redirecting of log and debugging messages to different
// output targets. 
class _declspec(dllexport) CConsole
{
public:
   static CConsole output;

   // Constructs a CConsole object with a specific console mode, default is COUT.
   CConsole(EConsoleMode mode = COUT, const char* filePath = "C:/output.txt");
   ~CConsole(void);

   // Gets the mode of this CConsole object.
   EConsoleMode GetMode();

   const char* GetFilePath();

   // Sets the output file path of this CConsole object.
   void SetFilePath(const char* filePath);

   void TimeStamp();

   // Logs a message with this specific CConsole object. An indirect call to an operator overload of
   // CConsole << Type
   template <typename T> void Log(const T &message);

protected:
   // The mode of this CConsole object.
   EConsoleMode m_mode;

   // The file path of the output file for this CConsole object.
   const char* m_filePath;
};


// Operator overload of CConsole << Type, queries the mode of the given CConsole object and 
// selects the appropriate output method associated with the mode.
template <typename T>
CConsole operator<< (CConsole console, const T &input) 
{
   switch(console.GetMode())
   {
   case COUT:
      {
         std::cout << input << "\n";
         break;
      }
   case OUTPUT_FILE:
      {
         ofstream fout;
         fout.open (console.GetFilePath(), ios::app);
         fout << input;
         fout.close();
         break;
      }
#if ON_WINDOWS
   case OUTPUT_DEBUG_STRING:
      {
         OutputDebugString(input);
         break;
      }
   case DEBUG_WRITE_LINE:
      {
         Debug::WriteLine(input);
         break;
      }
   case CONSOLE_WRITE_LINE:
      {
         Console::WriteLine(input);
         break;
      }
#endif
   case NO_OUTPUT:
      {
         break;
      }
   default:
      {
         std::cout << input;
         break;
      }
   }
   return console;
}


// Logs a message by calling the operator overload of << on this CConsole object with the message
// parameter.
template <typename T> 
void CConsole::Log(const T &message)
{
   this << message;
}

#endif

Console.cpp

#include "Console.h"

CConsole CConsole::output = *new CConsole(OUTPUT_FILE, "C:/LocalProjects/---/output.txt"); // Known memory leak here, discussed in comments

// Constructs a CConsole object by assigning the mode parameter to the 
// m_mode member variable.
CConsole::CConsole(EConsoleMode mode, const char* filePath)
{
   m_mode = mode;
   m_filePath = filePath;
   TimeStamp();
}


CConsole::~CConsole(void)
{
   //Log("\n\n");
}


// Returns the current mode of this CConsole object.
EConsoleMode CConsole::GetMode()
{
   return m_mode;
}


const char* CConsole::GetFilePath()
{
   return m_filePath;
}

void CConsole::TimeStamp()
{
   if(m_mode == OUTPUT_FILE)
   {
      std::ofstream file;
      file.open (m_filePath, std::ios::app); // 
      std::time_t currentTime = time(nullptr);
      file << "Console started: " << std::asctime(std::localtime(&currentTime));
      file.close();
   }
}

void CConsole::SetFilePath(const char* filePath)
{
   m_filePath = filePath;
}

HashTableTest.h

#ifndef _HASH_TABLE_TEST_H_
#define _HASH_TABLE_TEST_H_

#include <unordered_map>
#include <map>
#include <vector>
#include "Debuggable.h"
#include "Console.h"

using namespace std;

//template class __declspec(dllexport) unordered_map<int, double>;

struct Hasher
{
public:
  size_t operator() (vector<int> const& key) const
  {
      return key[0];
  }
};
struct EqualFn
{
public:
  bool operator() (vector<int> const& t1, vector<int> const& t2) const
  {
     CConsole::output << "\t\tAdding, t1: " << t1[0] << ", t2: " << t2[0] << "\n"; // If I delete this line then no error!
     return (t1[0] == t2[0]);
  }
};

class __declspec(dllexport) CHashTableTest : public CDebuggable 
{
public:
   CHashTableTest(CConsole console = *new CConsole());
   ~CHashTableTest(void);
   void Add(vector<int> key, bool value);
   bool Get(vector<int> key);

private:
   CConsole m_console;
   unordered_map<vector<int>, bool, Hasher, EqualFn> m_table; // If I change this to a map then no error!
};

#endif

明确一点,如果我将上面的 'unordered_map' 更改为 'map' 并从模板参数列表中删除 Hasher 函数对象,这将编译。如果我不这样做,我会收到链接器错误。我没有包括 HashTableTest.cpp 因为它几乎什么都没有。

编辑

我不确定我是否正确使用 unordered_map 这里是 CHashTableTest class:

的实现

HashTableTest.cpp

#include "HashTableTest.h"


CHashTableTest::CHashTableTest(CConsole console) : CDebuggable(console)
{
}


CHashTableTest::~CHashTableTest(void)
{
}

void CHashTableTest::Add(vector<int> key, bool value)
{
   m_table[key] = value;
}

bool CHashTableTest::Get(vector<int> key)
{
   return m_table[key];
}

当 DLL 生成的导入库与使用的 EXE(或另一个 DLL)链接时,被引入的项目的 declspecs 应该是 receiver-side 上的 declspec(dllimport)。当您 构建 实际 DLL 时,这些相同的项目可通过使用 declspec(dllexport) 获得。您的代码有后者,但没有前者。

这通常是通过在编译器配置的预处理器部分为您的 DLL 项目定义的单个预处理器宏来实现的。例如,在你的 header(s) 中声明从你的 DLL 导出的东西:

#ifndef MYDLL_HEADER_H 

#ifdef MYDLL_EXPORTS 
#define EXPORT __declspec(dllexport) 
#else 
#define EXPORT __declspec(dllimport) 
#endif 

class EXPORT CConsole 
{ 
// stuff here. 
}; 

#endif 

这样做会告诉编译器在构建 DLL 时导出 class 成员,包括 class 静态,因为 MYDLL_EXPORTS 是在 DLL 项目的预处理器配置标志中定义的.

构建消费项目时,请勿在编译器设置的预处理器配置中定义MYDLL_EXPORTS。因此它将使用 dllimport,而不是 dllexport。这应该让链接器知道去哪里寻找它需要的东西。

祝你好运。