I've been attempting a lot of template meta programming lately, particularly using CRTP, and have come across the titular error. Specifically error C2352 'MeshComponent::InternalSetEntity': illegal call of non-static member function.
A Minimal, Complete, and Verifiable snippit of my code is as such:
Component.h
class Entity //Forward declaration
template<class T, EventType... events>
class Component {
private:
short entityID;
public:
void SetEntity(Entity& _entity) { entityID = _entity.GetId(); T::InternalSetEntity(_entity); }
protected:
void InternalSetEntity(Entity& _entity) {}
};
MeshComponent.h
#include "Component.h"
class MeshComponent : public Component<MeshComponent> {
friend class Component<MeshComponent>;
protected:
void InternalSetEntity(Entity& _entity);
};
MeshComponent.cpp
#include "MeshComponent.h"
void MeshComponent::InternalSetEntity(Entity& _entity) {
//Nothing yet
}
Entity.h
class Entity {
private:
short id;
public:
short GetId() {return id;}
};
I am not declaring any static functions, nor do I want to. In fact I require these to be member functions as they will operate on instance specific data.
If someone knows why this error is occurring and a possible solution to the problem I would greatly appreciate it. Thanks in advance.
You know and ensure that MeshComponent inherits from Component<MeshComponent>, but the compiler doesn't know that: as far as it knows in the definition of Component, Component<T> and T are unrelated. You need to perform the downcast explicitly:
static_cast<T*>(this)->InternalSetEntity(_entity);
Related
This is the first time I am using class templates so please don't be to harsh if I made a simply mistake.
I have a class template class A<class T>. It has a method init() that is pure virtual and therefore will be implemented separately in every derived class. What all these possible derived classes will have in common is an init(T* i_x) which basically does some general stuff and then calls the init(). Because this will be the same for every derived class I want to define it in the base class template already. But somehow my compiler doesn't find the right function.
If I try to use the init(T* i_x) on an object of a derived class A_der I get the error:
no matching function for call to 'A_der::init(B_der*)
The classes used for the template parameter T will all be derived from another class B. Therefore the error message involves the class B_der which is derived from class B.
I boiled the problem down to a small example, which should involve everything that is important for the problem. If I try to compile this example in Visual Studio (normally I work in STM32CubeIDE) I get the following error
Severity Code Description Project File Line Suppression State
Error C2660 'A_der::init': function does not take 1
arguments template_class-overload_inherited_method [...]\main.cpp 8
So somehow the only function the compiler finds at this point is init() but not the base class template method init(T* ).
Can somebody please tell me why it is like that and what can I do to get the behaviour I want (without implementing a similar init(T* ) in every derived class of A?
Here is my example code:
base class template A - declaration - A.hpp
template<class T>
class A
{
protected:
T* m_x;
public:
virtual void connect(T* i_x) final;
virtual void init() = 0;
virtual void init(T* i_x) final;
};
base class template A - implementation - A.cpp
#include "A.hpp"
template<class T>
void A<T>::connect(T* i_x)
{
//some checks
m_x = i_x; //connects object of B to A
}
template<class T>
void A<T>::init(T* i_x)
{
connect(i_x);
init();
}
derived class A_der
#include "A.hpp"
#include "B_der.hpp"
#pragma once
class A_der : public A<B_der>
{
void init() override;
};
void A_der::init()
{
//Initialization which needs a B_der connected already
}
main.cpp
#include "B_der.hpp"
#include "A_der.hpp"
int main(void)
{
B_der testB;
A_der testA;
testA.init(&testB);
return 0;
}
For the sake of completeness:
class B
{
};
class B_der : public B
{
};
EDIT - Solved
Thanks a lot for the fast replies.
The combination of the comments from #BoP and #Jarod42 solved the problem.
I had to unhide the method with using A<B_der>::init (actually renaming might be the more elegant way) and move the implementation of A into A.hpp.
I will offer the updated example which builds successfully with Visual Studio 2019 for me here:
base class A
template<class T>
class A
{
protected:
T* m_x;
public:
virtual void connect(T* i_x) final;
virtual void init() = 0;
virtual void init(T* i_x) final;
};
template<class T>
void A<T>::connect(T* i_x)
{
//some checks
m_x = i_x; //connects object of B to A
}
template<class T>
void A<T>::init(T* i_x)
{
connect(i_x);
init();
}
derivad class A_der
A_der.hpp
#include "A.hpp"
#include "B_der.hpp"
class A_der : public A<B_der>
{
public:
void init() override;
using A<B_der>::init;
};
A_der.cpp
#include "A_der.hpp"
void A_der::init()
{
//Initialization which needs a B_der connected already
}
main.cpp
#include "B_der.hpp"
#include "A_der.hpp"
int main(void)
{
B_der testB;
A_der testA;
testA.init(&testB);
return 0;
}
for completeness
B.hpp
class B
{
};
B_der.hpp
#include "B.hpp"
class B_der : public B
{
};
I also forgot to make the methods of A_der public in the earlier example, this is corrected here. And I removed the #pragma onces in this example.
class A_der : public A<B_der>
{
void init() override;
};
When you declare a function init in the derived class, it hides all things named init from the base class. This is just like when declaring something in an inner scope - it hides things with the same name from outer scopes.
There are ways to import the hidden names, but an easy solution would be to just chose a different name, like init_base. Or, probably better, pass a parameter to the class constructor.
im learing c++.I was goofing around trying new stuff and wanted to use an object class sub in class object.But i was getting error saying that the object of class sub not in not defined.I know how to solve this issue i just have to move class sub above class object so that the compiler knows that there is a class called sub.
But I feel like this will get annoying as my code grows bigger and bigger so i tried forward declaring class like we do for function prototyping.But this doesn't work as it gives me this error -
'object::thing' uses undefined class 'sub'
Here is the code -
#include <iostream>
#include <vector>
class sub;
class object;
class object
{
private:
sub thing;
int ray;
public:
void set(int n);
void get() const;
};
class sub
{
public:
int num;
public:
void set_num(int n);
void get_num() const;
};
int main()
{
object ray;
ray.set(4);
ray.get();
}
Can you guys help me out??
thanks
When a type is used in c++, at a minimum that type needs to have been declared previously.
However, in some cases, the type needs to be defined (complete) as well.
class A;
class B {
A a; // error, because A needs to be defined
};
In other cases, the type only needs to be declared (i.e. it can be incomplete):
class A;
class B {
A *a; // fine, because A only needs to be declared
};
If a type needs to be defined when it's used (as in your case), then you have no choice but to define it before hand. You could put the definition in a header file, but that file still needs to be included before the type is used.
I'm trying to create an abstract class, which is used as a "template" for deriving classes.
I created a vector (stack) in which I store pointers to said abstract class, since you can't call member functions from an abstract class itself (thankfully ;))
But when I compile my classes, I get the following error:
error: invalid use of incomplete type 'class GameState'
this->_states.top()->Cleanup();
^~
I already tried many things and I think the problem is, that I have a forward declaration of the GameState class inside the Game class, because they both have to know of each other. Could that be the problem?
For viewing purposes, here are the snippets of my code:
GameState.hpp:
#include "Game.hpp"
class GameState {
public:
Game *game;
virtual void Init()=0;
virtual void Cleanup()=0;
... (etc.)
protected:
GameState(){}
};
Game.hpp:
#include <stack>
#include <SFML/Graphics.hpp>
#include "TextureManager.hpp"
// I GUESS THAT THIS DECLARATION IS THE PROBLEM!
class GameState;
class Game {
public:
TextureManager *TextureMgr;
sf::RenderWindow Win;
Game();
~Game();
... (etc.)
bool IsRunning() { return this->running; }
bool Quit() { this->running = false; }
private:
std::stack<GameState*> _states;
bool running;
};
Game.cpp:(calling example of member function)
void Game::Cleanup() {
while (!this->_states.empty()) {
this->_states.top()->Cleanup();
this->_states.pop();
}
}
Thank you very much in advance. And if the forward declaration is the problem; do you probably know any article or book, where such problems, where two classes have to know of each other, are described with different approaches for a solution?
I've already searched in a bunch of books, but I can't really find one, which got my eye.
You need to include GameState.hpp into Game.cpp so GameState will be complete at that point. Forward declaration allows to declare std::stack<GameState*> however a definition should be available to call class methods.
I have two classes, Friend2 is a friend of Friend1. After Friend2 accesses the Friend1's private member variable, I want Friend1 to be able to access the Friend2's public member functions. So I decided to use composition inside Friend1. However, the compiler shows me the error:
use of undefined type 'friend2'
Then, I tried another way, making Friend1 a friend of Friend2 too. But I still got the same error. Would anyone teach me the way to solve? Thx a lot!
#ifndef FRIEND1_H
#define FRIEND1_H
class friend2;
class friend1 {
private:
int x;
public:
friend1();
int comp(friend2* f2o);
friend class friend2;
};
friend1::friend1() {
x = 1;
}
int friend1::comp(friend2* f2o) {
return f2o->getxx(); //the error : use of undefined type
}
#endif
#ifndef FRIEND2_H
#define FRIEND2_H
#include "friend1.h"
#include <iostream>
class friend2 {
private:
int xx;
public:
friend2();
void p(friend1* f1o);
int getxx() const;
friend class friend1;
};
friend2::friend2() {}
void friend2::p(friend1* f1o) {
xx = f1o->x;
}
int friend2::getxx() const {
return xx;
}
#endif
Also, is composition or friend class the better way to do this? Why?
You get //the error : use of undefined type because class Friend2 is only declared, not defined at that point. To solve this move int friend1::comp(friend2* f2o) implementation to friend1.cpp and include friend2.h from there.
UPDATE In general, if two classes are mutual friends (and even if only one of them is a friend to another), it's a good reason to think about the design.
I'm trying to build a solution which has three files. With main.cpp it is four files.
Entity.h
#pragma once
#include "SystemBase.h"
namespace Engine {
class Entity {
public:
Entity() { }
void s(SystemBase* sb) { }
};
}
SubscribersList.h
#pragma once
#include "SystemBase.h"
#include "Entity.h"
namespace Engine {
class SubscribersList {
friend SystemBase;
public:
SubscribersList() { }
void f(Entity* e) { }
};
}
SystemBase.h
#pragma once
#include "SubscribersList.h"
#include "Entity.h"
namespace Engine {
class SystemBase {
public:
SystemBase() { }
void g(Entity* e) { }
private:
SubscribersList m;
};
}
Don't focus on the body's of methods in the headers. It is just to keep things simple. I found two ways to build the solution.
1. Write the word class before all class names. But it crashes when I try to separate the realization from prototypes.
2. Write all code in one file.
I don't/won't write the keyword class before all class names to build the solution, and certainly I don't/won't write a big project in one file. So why I can't build it? What is the magic?!
To understand the problem of cyclic header dependency we first need understand the difference between a class declaration and definition and the concept of incomplete types.
A prototype or forward declaration of a type Type is written as:
class Type;
Such a forward declaration allows you to create pointers and reference to that type.
You cannot however instantiate, dereference pointers to or use a reference to Type until its full type is declared.
A declaration for Type could be written as:
class AnotherType;
class Type {
public:
void aMemberFunc();
private:
AnotherType *m_theOtherThing;
};
Now we have the declaration instances can be created and pointers to Type can be dereferenced.
However before m_theOtherThing is dereferenced or instanciated AnotherType must be fully declared.
class AnotherType {
Type m_aType;
}
Should do, which gives us both the full declaration and definition of AnotherType.
That allows to continue on to write the definition of Type::aMemberFunc:
void Type::aMemberFunc() {
m_theOtherThing = new AnotherType();
}
If instead of presenting this code to the compiler in this order we instead presented the full declarations of Type and AnotherType up front:
class Type {
public:
void aMemberFunc();
private:
AnotherType *m_theOtherThing;
};
class AnotherType {
Type m_aType;
}
Then AnotherType *m_theOtherThing; will fail to compile as AnotherType has not been declared or forward declared by that point.
Switching the order gives:
class AnotherType {
Type m_aType;
}
class Type {
public:
void aMemberFunc();
private:
AnotherType *m_theOtherThing;
};
Now Type m_aType; will not compile as Type has not been declared. A forward declaration would not do in this case.
Using #pragma once instead of header guards does not in anyway change the problem. #pragma once only ensures the header is include just once it does not effect the order the compiler processes the code otherwise. It certainly does not allow the compiler to ignore undefined types when it reaches them.
For this kind of class structure there is no way for the compiler to be able to process it without the use for forward declarations.