-3

What is the correct way to use inheritance among multiple files?

I am new to C++, and I am trying to create a class for all my GDI+ related functions which I'm gonna use in my separate cpp files. I have tried several approaches and to be able to find the problem more easily I got to trying with empty constructor.

I get LNK2019 with this code (I took away parts which are unrelated to the issue, only what is related to WndFuncs class was left):

Header of functions file:

#ifndef WNDFUNCS_H
#define WNDFUNCS_H

class WndFuncs
{
private:
public:
    WndFuncs(); //declaration
};

#endif

The file itself:

#include "stdafx.h"

#include <windows.h>
#include <commctrl.h>
#include <winuser.h>
#include <gdiplus.h>
#include "WndFuncs.h"

WndFuncs::WndFuncs() //definition
{

}

The header of class that tries to inherit the class:

#ifndef SEARCHEDITBOX_H
#define SEARCHEDITBOX_H

class SearchEditBox : public WndFuncs
{
private:
    WndFuncs b;
    SearchEditBox();

public:
    ~SearchEditBox();
    static SearchEditBox* CreateEditBox(HINSTANCE hInst, HWND hwnd, int pos_x, int pos_y, int width, int height, WndCols const* p_wndCols);

#endif

The the class file:

#include "stdafx.h"
#include "WndCols.h"

#include <windows.h>
#include "WndFuncs.h"
#include "SearchEditBox.h"

SearchEditBox::SearchEditBox()
    : b()
{

}

SearchEditBox::~SearchEditBox()
{
    if (editBox)
        DestroyWindow(editBox);
}

SearchEditBox* SearchEditBox::CreateEditBox(HINSTANCE hInst, HWND hwnd, int pos_x, int pos_y, int width, int height, WndCols const* p_wndCols)
{
    SearchEditBox *p_SearchEditBox = new SearchEditBox; //allocating dynamic memory for class (which by itself is declared as pointer)
    return p_SearchEditBox;
}

The error is:

LNK2019 unresolved external symbol "public: __thiscall WndFuncs::WndFuncs(void)" (??0WndFuncs@@QAE@XZ) referenced in function "private: __thiscall SearchEditBox::SearchEditBox(void)" (??0SearchEditBox@@AAE@XZ)

I have read the explanation and all the points on the MSDN page (even tried putting "__cdecl" into the function declaration), I am sure the function is declared AND defined (in the class files; I also tried with const int x thinking that the problem may be in empty constructor), so the WndFuncs file should be fine.

I have read this and my assumption is that I declare the class in the wrong way in the inheriting file (and the linker thus can't link to correct functions in WndFuncs class), but even when I am trying to do everything as described in here it does not help either. I am not using any virtual members so the problem should not be related to that (as pointed out on that page).

When I add destructor to the WndFuncs class I get 2 LNK2019 errors, so the problem should not be related to that also. I also have the header files in right order I think (tried both).

I tried also with other function (with or without constructor) with same error.

Community
  • 1
  • 1
Xzsh4s575sf75
  • 83
  • 1
  • 11
  • Is `WndFuncs` name of the class, the member, or the function? Why are you trying to include a member with a type of your parent? Your code doesn't make any sense. I think, you need a good reading on C++ language. – SergeyA Jun 06 '16 at 20:48
  • @SergeyA I got to the point where I tried everything what popped to my mind, I will change the code so that it is more obvious. – Xzsh4s575sf75 Jun 06 '16 at 20:51
  • Daniel, the first thing you should do is to provide us with MCVE. – SergeyA Jun 06 '16 at 20:52
  • @πάνταῥεῖ I am not sure if the duplicate is correct, though. OPs code is so terribly confused, that linking issues might just be a side effect. – SergeyA Jun 06 '16 at 20:53
  • @SergeyA I tried giving you MCVE, when I remove everything related to WndFuncs class from SearchEditBox then it works, and on the other hand, to be able to reproduce the code I would have to give you the whole class with main Window (I am working with win32 app) no? Maybe I just tried to be MCVE too much then... – Xzsh4s575sf75 Jun 06 '16 at 20:58
  • @πάνταῥεῖ You point this (being duplicate) to article I already list in things I tried in OP. I read it and did not find the solution. – Xzsh4s575sf75 Jun 06 '16 at 21:00
  • @SergeyA I tried to edit it to be MCVE example (it works in my files). Should I add main as well? Btw I use warning level 4 and I already had all warnings related to my files resolved. – Xzsh4s575sf75 Jun 06 '16 at 21:18
  • @SergeyA I also edited the title to make it reflect more the real question – Xzsh4s575sf75 Jun 06 '16 at 21:36
  • @DanielMaczak You probably missed to add `WndFuncs.cpp` to your projects source files properly. – πάντα ῥεῖ Jun 07 '16 at 02:36
  • @πάνταῥεῖ It worked! Thank you thousand times! I spent 6 hours on this issue believe it or not (as I said, I am just learning, and I always try to show you that I did do the research); I have also read the post before you linked to when making my post [duplicate]. I believe your solution is not there though, so could you please remove the [duplicate] sign on this post? – Xzsh4s575sf75 Jun 07 '16 at 19:01
  • @DanielMaczak I reopened the question that you can write an answer for the soluiton you found. – πάντα ῥεῖ Jun 08 '16 at 06:01
  • @πάνταῥεῖ Well, you found it so the solution should technically be yours. You don't want it? – Xzsh4s575sf75 Jun 08 '16 at 06:47
  • My comment was just a guess. You know best what was the reason for the problem, and what was necessary to fix it. – πάντα ῥεῖ Jun 08 '16 at 06:49

1 Answers1

0

The problem was solved by adding the class files to the project the correct way: Project > Add class. After that the references were linked correctly.

What I did wrong was adding files to the project separately (through: File > Add > File).

Xzsh4s575sf75
  • 83
  • 1
  • 11