用于在其他类中声明对象时模板化类链接错误

时间:2011-05-29 08:56:19

标签: c++ templates linker

我创建了一个模板化数据类(CAnyData,请参阅其头文件副本供您参考),我在另一个类(CConstantDataBlock中声明了一些变量,请查看其头文件副本供你参考)。正如您所看到的,后者几乎是一个空洞的阶级。但是当我编译我的项目时,VS2008编译器会出现以下链接错误。请帮助我弄清楚我的CConstantDataBlock和/或CAnyData出了什么问题?

1>------ Build started: Project: Tips, Configuration: Debug Win32 ------
1>Compiling...
1>ConstantDataBlock.cpp
1>Linking...
1>   Creating library F:\Tips\Debug\Tips.lib and object F:\Tips\Debug\Tips.exp
1>ConstantDataBlock.obj : error LNK2019: unresolved external symbol "public: __thiscall CAnyData<double>::~CAnyData<double>(void)" (??1?$CAnyData@N@@QAE@XZ) referenced in function __unwindfunclet$??0CConstantDataBlock@@QAE@XZ$0
1>ConstantDataBlock.obj : error LNK2019: unresolved external symbol "public: __thiscall CAnyData<int>::~CAnyData<int>(void)" (??1?$CAnyData@H@@QAE@XZ) referenced in function __unwindfunclet$??0CConstantDataBlock@@QAE@XZ$0
1>ConstantDataBlock.obj : error LNK2019: unresolved external symbol "public: __thiscall CAnyData<double>::CAnyData<double>(void)" (??0?$CAnyData@N@@QAE@XZ) referenced in function "public: __thiscall CConstantDataBlock::CConstantDataBlock(void)" (??0CConstantDataBlock@@QAE@XZ)
1>ConstantDataBlock.obj : error LNK2019: unresolved external symbol "public: __thiscall CAnyData<int>::CAnyData<int>(void)" (??0?$CAnyData@H@@QAE@XZ) referenced in function "public: __thiscall CConstantDataBlock::CConstantDataBlock(void)" (??0CConstantDataBlock@@QAE@XZ)
1>F:\Tips\Debug\Tips.exe : fatal error LNK1120: 4 unresolved externals
1>Build log was saved at "file://f:\Tips\Tips\Debug\BuildLog.htm"
1>Tips - 5 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

#pragma once

#include <string>

using namespace std;

template <class T>
class CAnyData
{
public:
    CAnyData(void);
    CAnyData(int nWordNumber, string sContents, T Type, int nWidth, int nPrecision);
    ~CAnyData(void);

// Operators
    CAnyData( const CAnyData& rhs );
    const CAnyData& operator = (const CAnyData& rhs);

    // Must define less than relative to name objects.
    bool operator<( const CAnyData& AnyData ) const;

    // Compares profile's of two objects which represent CAnyData
    inline bool operator ==(const CAnyData& rhs) const;

// Get properties  
    inline int WordNumber() const { return m_nWordNumber; }
    inline const string& Contents() const { return m_sContents; }
    inline const T& DataType() const { return m_Type; }
    inline int Width() const { return m_nWidth; }
    inline int Precision() const { return m_nPrecision; }

// Set properties
    void WordNumber(int nWordNumber) const { m_nWordNumber = nWordNumber; }
    void Contents(string sContents) const { m_sContents = sContents; }
    void DataType(T Type) const { m_Type = Type; }
    void Width(int nWidth) const { m_nWidth = nWidth; }
    void Precision(int nPrecision) const { m_nPrecision = nPrecision; }

protected:
    void Init(void);

protected:
    int m_nWordNumber;
    string m_sContents;
    T m_Type;
    int m_nWidth;
    int m_nPrecision;
};

#pragma once

#include "AnyData.h"

// Constants block
// This block consists of 64 words to be filled with useful constants.

class CConstantDataBlock
{
public:
    CConstantDataBlock(void);
    ~CConstantDataBlock(void);

protected:
    CAnyData<int>    m_nEarthEquatorialRadius;
    CAnyData<int>    m_nNominalSatelliteHeight;
    CAnyData<double> m_dEarthCircumference;
    CAnyData<double> m_dEarthInverseFlattening;
};

1 个答案:

答案 0 :(得分:1)

似乎您没有CAnyData的几个方法的定义,包括默认构造函数和析构函数。当你在CConstantDataBlock - 类中使用它们时,虽然需要构造函数和析构函数。

由于CAnyData是一个类模板,所有定义都应该是written directly into the header-file(就像你对所有getter和setter一样)。