-3

I'm using C++ and Visual Studio Professional 2013 to write a wrapper for the Windows API (to make it easier to read). When I try to compile the solution, I get two linker errors related to the Intersect function in the wRectangle class. I commented out the function to see what the cause of the problem was, and the solution compiled and ran fine. From this, I determined that it was because the return type was an object of the class name (i.e. The Intersect function in the wRectangle class returns a wRectangle object). Moving the implementation of the Intersect function to its prototype in Rectangle.h fixed the issue, but I would prefer to keep the implementation in Rectangle.cpp. Any way to solve this?

Relevant files, most of the comments have been removed since they serve no relevance:

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;
    }
}

Error 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

Error 2:

error LNK1120: 1 unresolved externals   

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

Project: Windows API Wrapper 
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – AStopher Nov 09 '14 at 16:46
  • Please see: http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix. You have not `#pragma comment`ed a required `.lib`. – AStopher Nov 09 '14 at 16:47

1 Answers1

1

This is because you have not defined

wRectangle(uInt32 xMin, uInt32 xMax, uInt32 yMin, uInt32 yMax);
ravi
  • 10,994
  • 1
  • 18
  • 36
  • Thank you! I just created a definition for that constructor and commented out the copy constructor (which I also forgot to define) and my solution compiles properly now. – anviltilde Nov 09 '14 at 16:46