Dwie klasy, które zależą od siebie - jedna z funkcji powoduje błąd

0

Cześć,
mam prośbę o pomoc.
Mam dwie klasy, które zależą od siebie (poniżej kod). Wszystko jest ok, poza tym, że przy wywołaniu jeden z funkcji wywala błąd kompilacji "error C2039: „fun2”: Nie jest składową „PosManager<Pos>”
Czy mogę mieć prośbę o informację jak powinienem zadeklarować tą funkcję aby było ok?

//----------------------------
//main.cpp
//----------------------------
#include <iostream>
#include <string>
#include "PosManager.h"
#include "Pos.h"

int main()
{
    PosManager<Pos> pos_m;
    Pos pos;
    pos_m.pos = &pos;

    pos_m.fun1();
    pos_m.fun2(); //<-- Tu jest błąd kompilacji
    pos.fun1();
    pos.fun2();

    std::string x;
    std::cin >> x;

}

//----------------------------
PosManager.h
//----------------------------
#pragma once
#include <iostream>
#include "Pos.h"

class Pos;

template<class T>
class PosManager
	{
	public:
		T* pos;
		void fun1();
		void fun22();
	};

template<class T>
inline void PosManager<T>::fun1()
	{
	std::cout << "PosManager => fun1()\n";
	pos->fun1();
	}

//----------------------------
PosManager.cpp
//----------------------------
#include "PosManager.h"
#include "Pos.h"

template<class T>
void PosManager<T>::fun22()
	{
	std::cout << "PosManager => fun2()\n";
	pos->fun2();
	}

//----------------------------
Pos.h
//----------------------------
#pragma once
#include <iostream>
#include "PosManager.h"

template<class T>
class PosManager;


class Pos
	{
	public:
		void fun1();
		void fun2();
	};

inline void Pos::fun1()
	{
	std::cout << "Pos => fun1()\n";
	}

//----------------------------
Pos.cpp
//----------------------------
#include "Pos.h"
void Pos::fun2()
	{
	std::cout << "Pos => fun2()\n";
	}

Z góry dziękuję za pomoc!

2

literówka w deklaracji funkcji, w pliku PosManager.h masz void fun22();

0

Dziękuję za czujność - faktycznie była literówka, ale to nie to. Nazwę funkcji zmieniłem po tym jak już nic mi nie przychodziło do głowy.
Obecnie, gdy poprawiłem nazwę funkcji w main, błąd kompilacji to:
"1>Testopt.obj : error LNK2019: nierozpoznany zewnŕtrzny symbol "public: void __thiscall PosManager<class Pos>::fun22(void)" (?fun22@?$PosManager@VPos@@@@QAEXXZ) przywo│any w funkcji _main"

1

to wrzuć aktualny kod, bo nie wiem co poprawiłeś i ciężko coś poradzić.

1

definicja metody szablonowej musi być być w pliku nagłówkowym :) A u Ciebie definicja metody fun22() jest w cpp

1

@Kofcio CRTP robi się trochę inaczej. Niżej przykład wzięty z mojego śmietnika https://github.com/pdy/scrapyard/blob/master/TemplatesFun/src/TemplatesFun.cpp#L58

template<class Derived>
class Interface
{
    const Derived& derived() const { return *static_cast<const Derived*>(this); }

public:
    std::string name() const { return derived().name(); }

    void run() const
    {
        derived().handleColor();
        derived().handleShape();
    }

    void handleColor() const { std::cout << "Interface handleColor\n"; }

    void handleShape() const { std::cout << "Interface handleShape\n"; }

private:
    Interface() = default;
    friend Derived;
};

class Derived : public Interface<Derived>
{
public:
    std::string name() const { return "Derived"; }

    void handleShape() const { std::cout << "Derived handleColor\n"; }
};

class Derived2 : public Interface<Derived2>
{
public:
    std::string name() const { return "Derived2"; }
};

template<class T>
void logName(const Interface<T> &obj)
{
    std::cout << obj.name() << "\n";
}

int main()
{
   Derived d1;
   Derived2 d2;
   logName(d1);
   logName(d2);
   return 0;
}

0
_dominik napisał(a):

definicja metody szablonowej musi być być w pliku nagłówkowym :) A u Ciebie definicja metody fun22() jest w cpp

Ohh, chyba masz rację... :) Dziękuję za pomoc!

@several z uwagi, że ja nie chcę dziedziczyć po żadnej klasie to chyba nie jest to do końca problem CRTP(?), ale chyba problem jest dość mocno powiązany. Tobie również bardzo dziękuję za pomoc! :) Jeszcze trochę o tym poczytam i zaktualizuję swoją wiedzę.

1 użytkowników online, w tym zalogowanych: 0, gości: 1