代码之家  ›  专栏  ›  技术社区  ›  Mark Ingram

为什么库中的这个模板类在使用时会生成链接器错误?

  •  0
  • Mark Ingram  · 技术社区  · 14 年前

    I've got the following setup. RectangleT class defined in a header file in a library. Attempted to use the class in my main application. When linking I get an error for every function I try to call - except constructor and the GetLeft/GetTop/GetRight/GetBottom - BUT - I do get the error when calling 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;
        };
    }
    

    有人有什么想法吗?!

    1 回复  |  直到 14 年前
        1
  •  2
  •   Ramadheer Singh    14 年前

    我删除了编译器指令 MY2DYAPI 并尝试了你的代码,它运行良好,见下文。

    Windows 7,MS与2010

    int main ()
    {
      My2D::RectangleT < int > rect;
    
      rect.SetBottom(3);
      rect.SetLeft(3);
      rect.SetRight(8);
      rect.SetTop(8);
      return rect.GetHeight();
    }