Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 14:39

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Template Class Vererben

Discussion on Template Class Vererben within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: May 2015
Posts: 27
Received Thanks: 0
Template Class Vererben

Guten Tag liebe Epvper,
und wie ihr im Titel seht hab ich ein Problem:

Ich habe eine Klasse:
Code:
#pragma once

template <typename T>
class PublicClass
{
public:
	static T *instance();
	static void deleteInstance();

private:
	static T *m_instance;
};
Code:
#include "PublicClass.h"

template <typename T>
T *PublicClass<T>::m_instance = nullptr;

template <typename T>
T *PublicClass<T>::instance()
{
	if (m_instance == nullptr)
		m_instance = new T();

	return m_instance;
}

template <typename T>
void PublicClass<T>::deleteInstance()
{
	if (m_instance != nullptr)
		delete m_instance;
}
Die ich an eine andere Klasse vererben möchte:

Code:
#ifndef CONFIGURATION_H
#define CONFIGURATION_H
#include "PublicClass.h"

#include <iostream>

class Configuration : public PublicClass<Configuration>
{
public:
	Configuration(){}
	~Configuration(){}

        void test() {std::cout << "test";}

}
private:

};

#endif // CONFIGURATION_H
In der Main ruf ich das ganze so Auf:

Code:
Configuration::instance()->test();

Doch dies Funktioniert nicht so ganz wie ich will:



Bitte um Hilfe!

Danke im schonmal!
Krijo' is offline  
Old 10/12/2015, 11:26   #2
Moderator


 
elite*gold: 558
Join Date: Feb 2010
Posts: 6,546
Received Thanks: 1,418
Mein C++ ist eigentlich noch ziemlich dürftig, aber ich glaube, ich konnte die zwei Fehler entdecken, die Problem darstellen. Ich verstehe zwar nicht wieso der zweite bei dir nicht aufgefallen ist, aber gut dann ist das halt so.

Der erste Fehler ist, dass Template-Klassen komplett in Header-Datei definiert sein müssen.

Der zweite Fehler, und das verstehe ich bei dir nicht ganz, sind die nicht vorhandenen Zeiger. Wenn du "Configuration::instance()->test()" gehst du davon aus, dass "instance()" ein "Configuration*" zurückgibt, bei dir war es aber nur ein "Configuration". In dieser Zeile "m_instance = new T()" erstellst du ein "Configuration*" und willst es in "Configuration" speichern, diese Zeile wird vermutlich erst nach der Korrektur des ersten Fehlers Probleme machen.

Ich habe noch den Rückgabedatentypen von deleteInstance geändert, weil du dort nichts zurück gibst.
ComputerBaer is offline  
Old 10/12/2015, 13:48   #3
 
elite*gold: 0
Join Date: May 2015
Posts: 27
Received Thanks: 0
Oh das andere war ein Fehler von mir sry, hatte es gestern nacht noch schnell als Beispiel gemacht und paar sachen vergessen Teste eben.

Nachtrag:

Jetzt kommt dieser Fehler:
Krijo' is offline  
Old 10/12/2015, 14:04   #4
Moderator


 
elite*gold: 558
Join Date: Feb 2010
Posts: 6,546
Received Thanks: 1,418
Vielleicht ein fehlendes include? Ich weiß es nicht, bei meinem Testaufbau hat so jedenfalls funktioniert. Die C++ Fehlermeldungen des Visual Studios kenne ich nicht.
ComputerBaer is offline  
Old 10/12/2015, 14:30   #5
 
elite*gold: 0
Join Date: May 2015
Posts: 27
Received Thanks: 0
Also includes fehlen nicht.

Fehler Fixed, kann geschlossen werden.
Krijo' is offline  
Old 10/14/2015, 07:13   #6



 
IceTrailer's Avatar
 
elite*gold: 150
Join Date: Sep 2010
Posts: 2,070
Received Thanks: 821
Quote:
Originally Posted by Krijo' View Post
Also includes fehlen nicht.

Fehler Fixed, kann geschlossen werden.

Vielleicht postest du noch, wie du den Fehler gefixt hast?
IceTrailer is offline  
Reply


Similar Threads Similar Threads
Sdl Gaming Template [Visual Studio Template]
06/28/2013 - Coding Tutorials - 5 Replies
If you have problems with the sdl dll's just download them from their site. C# SDL - Browse Files at SourceForge.net For learning Sdl + Documentation: SourceForge.net: cs-sdl I'll be doing a few tutorials when I got time. Basically it's just a project template which you add to your Visual Studio.
[Website Template] Silkroad free template
01/16/2013 - SRO Guides & Templates - 4 Replies
Silkroad website free template Live demo :Click Here ! Download attachment .. :)
[Template]Homepage Template by CrystalPrime (Gold-Version)
09/21/2011 - Metin2 PServer Guides & Strategies - 46 Replies
#Done



All times are GMT +1. The time now is 14:39.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.