2010-06-28 4 views
0

다음과 같은 설정이 있습니다. 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; 
    }; 
} 

누구나 아이디어가 있습니까?

+4

도움이 될 링커 오류의 텍스트 ... 모든 – Cogwheel

+0

먼저, 내가 MY2D_API을 가정하고있어 수출을위한 것입니다. 클래스 템플릿을 내보낼 필요가 없으며 내부 링크가 있습니다. 그것을 제거하는 것이 좋습니다 것이 효과가 없더라도 혼란 스럽습니다. 그렇지 않으면 클래스는 나에게 잘 보입니다. 클라이언트 사용에 문제가 있다고 생각됩니다. 오류 및 GetWidth/GetHeight라는 클라이언트 코드를 게시하십시오. 또한 애완 동물처럼, 가상의 dtor가 필요합니까? 이 클래스는 상속을 위해 사용됩니까? SuperRectangles를 만들기 위해 RectangleT를 상속하지 말 것을 권장합니다. – stinky472

+1

MY2D_API 매크로 코드를 제공해야합니다. 귀하의 코드는 (2008 Express에서) 잘 작성되지 않으므로 문제는 아마도 해당 매크로와 관련됩니다. 정확한 링커 오류 메시지를 제공해야합니다. 오류 메시지없이 찻잎을 추측 할 수 있습니다. – SigTerm

답변

2

컴파일러 지시문 MY2D_API을 제거하고 코드를 사용해 보았지만 제대로 작동합니다 (아래 참조). 윈도우 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(); 
} 
관련 문제