簡體   English   中英

為什么庫中的此模板類在使用時會生成鏈接器錯誤?

[英]Why does this template class in a library generate linker errors when used?

我有以下設置。 在庫的頭文件中定義的RectangleT類。 嘗試在主應用程序中使用該類。 鏈接時,我嘗試調用的每個函數都會出錯-構造函數和GetLeft / GetTop / GetRight / GetBottom除外-但是-調用GetWidth / GetHeight時確實會出錯。

這是我為一個簡單的模板類獲得的代碼。

namespace My2D
{
    template <typename T>
    class MY2D_API RectangleT
    {
    public:     // Construction
        RectangleT(const T left = 0, const T top = 0, const T right = 0, const T bottom = 0)
            : m_left(left)
            , m_top(top)
            , m_right(right)
            , m_bottom(bottom)
        {
        }

        RectangleT(const RectangleT<T> &source)
            : m_left(source.m_left)
            , m_top(source.m_top)
            , m_right(source.m_right)
            , m_bottom(source.m_bottom)
        {
        }

        virtual ~RectangleT(void)
        {
        }

    public:     // Getters / setters
        T GetLeft() const { return m_left; }
        T GetTop() const { return m_top; }
        T GetRight() const { return m_right; }
        T GetBottom() const { return m_bottom; }
        T GetWidth() const { return m_right - m_left; }
        T GetHeight() const { return m_bottom - m_top; }

        void SetLeft(const T value) { m_left = value; }
        void SetTop(const T value) { m_top = value; }
        void SetRight(const T value) { m_right = value; }
        void SetBottom(const T value) { m_bottom = value; }

    protected:  // Members
        T m_left;
        T m_top;
        T m_right;
        T m_bottom;
    };
}

任何人有任何想法嗎?

我刪除了編譯器指令MY2D_API並嘗試了您的代碼,它工作正常,請參見下文。

Windows 7,MS VS 2010

int main ()
{
  My2D::RectangleT < int > rect;

  rect.SetBottom(3);
  rect.SetLeft(3);
  rect.SetRight(8);
  rect.SetTop(8);
  return rect.GetHeight();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM