返回与该类同名的对象会生成链接器错误

时间:2014-11-09 16:37:53

标签: c++ visual-studio winapi linker-errors

我正在使用C ++和Visual Studio Professional 2013为Windows API编写包装器(以便于阅读)。当我尝试编译解决方案时,我得到两个与wRectangle类中的Intersect函数相关的链接器错误。我评论了函数,看看问题的原因是什么,并且解决方案编译并运行正常。由此,我确定这是因为返回类型是类名的对象(即wRectangle类中的Intersect函数返回一个wRectangle对象)。将Intersect函数的实现移动到Rectangle.h中的原型修复了这个问题,但我更愿意将实现保留在Rectangle.cpp中。有办法解决这个问题吗?

相关文件,大多数评论已被删除,因为它们没有相关性:

Rectangle.h:

#include <Windows.h>
#include "Typedefs.h"
#include "Window.h" 

namespace windows_API_Wrapper
{
    #ifndef RECTANGLE_H
    #define RECTANGLE_H

    class wRectangle
    {
    public:

        wRectangle();

        wRectangle(uInt32 xMin, uInt32 xMax, uInt32 yMin, uInt32 yMax);

        void operator=(wRectangle &assignment);

        void operator=(Window assignment);

        boolean operator!=(wRectangle nonComparable);

        wRectangle Intersect(wRectangle source1, wRectangle source2);

        void Normalize(); 

    private:
        rectangle rect; //A wRectangle structure (typedef of a RECT strucure), for internal use only 
    };

    #endif //RECTANGLE_H
}

Rectangle.cpp:

#include "Rectangle.h" 

using namespace windows_API_Wrapper;

wRectangle::wRectangle()
{
    this->rect.left = 0;
    this->rect.top = 0;
    this->rect.right = 0;
    this->rect.bottom = 0;
}

void wRectangle::operator=(wRectangle &assignment)
{
    this->rect.left = assignment.rect.left;
    this->rect.top = assignment.rect.top;
    this->rect.right = assignment.rect.right;
    this->rect.bottom = assignment.rect.bottom;
}

void wRectangle::operator=(Window assignment)
{
    if (GetClientRect(assignment.Get(), &this->rect) == 0)
        MessageBox(null, "Failed to assign window dimensions to wRectangle!", "ERROR", MB_ICONEXCLAMATION);
}

boolean wRectangle::operator==(wRectangle comparable)
{
    return EqualRect(&this->rect, &comparable.rect);
}

boolean wRectangle::operator!=(wRectangle nonComparable)
{
    return !EqualRect(&this->rect, &nonComparable.rect);
}

wRectangle wRectangle::Intersect(wRectangle source1, wRectangle source2)
{
    wRectangle destination;

    IntersectRect(&destination.rect, &source1.rect, &source2.rect);

    return destination;
}

void wRectangle::Normalize()
{
    if (this->rect.top > this->rect.bottom)
    {
        uInt32 temp = this->rect.top;
        this->rect.top = this->rect.bottom;
        this->rect.bottom = temp;
    }

    if (this->rect.left > this->rect.right)
    {
        uInt32 temp = this->rect.left;
        this->rect.left = this->rect.right;
        this->rect.right = temp;
    }
}

错误1:

error LNK2019: unresolved external symbol "public: __thiscall 
windows_API_Wrapper::wRectangle::wRectangle(class windows_API_Wrapper::wRectangle const &)" (??
0wRectangle@windows_API_Wrapper@@QAE@ABV01@@Z) referenced in function "public: class 
windows_API_Wrapper::wRectangle __thiscall windows_API_Wrapper::wRectangle::Intersect(class 
windows_API_Wrapper::wRectangle,class windows_API_Wrapper::wRectangle)" (?
Intersect@wRectangle@windows_API_Wrapper@@QAE?AV12@V12@0@Z) 

File: G:\Visual Studio Projects\Windows API Wrapper\Windows API Wrapper\Rectangle.obj

Project: Windows API Wrapper

错误2:

error LNK1120: 1 unresolved externals   

File: G:\Visual Studio Projects\Windows API Wrapper\Debug\Windows API Wrapper.exe   

Project: Windows API Wrapper 

1 个答案:

答案 0 :(得分:1)

这是因为你还没有定义

wRectangle(uInt32 xMin, uInt32 xMax, uInt32 yMin, uInt32 yMax);