Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;模板西格尔顿工厂 这里是C++的一个简单的项目,具有2种设计模式:Sigelon和Primes,Sigelon也是模板类和接口(IHASH)和类(HASH1)。 一个简单的工厂类(HashFactory)创建一个sigleton(Hash1);Hash1继承了接口IHash,理想情况下我有Hash1,Hash2。。哈森_C++_Templates_Design Patterns_Singleton_Factory Pattern - Fatal编程技术网

C++;模板西格尔顿工厂 这里是C++的一个简单的项目,具有2种设计模式:Sigelon和Primes,Sigelon也是模板类和接口(IHASH)和类(HASH1)。 一个简单的工厂类(HashFactory)创建一个sigleton(Hash1);Hash1继承了接口IHash,理想情况下我有Hash1,Hash2。。哈森

C++;模板西格尔顿工厂 这里是C++的一个简单的项目,具有2种设计模式:Sigelon和Primes,Sigelon也是模板类和接口(IHASH)和类(HASH1)。 一个简单的工厂类(HashFactory)创建一个sigleton(Hash1);Hash1继承了接口IHash,理想情况下我有Hash1,Hash2。。哈森,c++,templates,design-patterns,singleton,factory-pattern,C++,Templates,Design Patterns,Singleton,Factory Pattern,在编译时我有一个错误,有什么问题 g++ main.cpp main.cpp: In static member function ‘static IHash* HashFactory::get(int)’: main.cpp:11:15: error: ‘static T& Singleton<T>::getInstance() [with T = Hash1]’ is inaccessible static T &getInstance() {

在编译时我有一个错误,有什么问题

g++  main.cpp 
main.cpp: In static member function ‘static IHash* HashFactory::get(int)’:
main.cpp:11:15: error: ‘static T& Singleton<T>::getInstance() [with T = Hash1]’ is inaccessible
static T &getInstance() {
           ^
main.cpp:76:50: error: within this context
     if (type == 1)return &Hash1::getInstance();
                                              ^
g++main.cpp
main.cpp:在静态成员函数“static IHash*HashFactory::get(int)”中:
main.cpp:11:15:错误:“static T&Singleton::getInstance()[with T=Hash1]”不可访问
静态T&getInstance(){
^
main.cpp:76:50:错误:在此上下文中
if(type==1)返回&Hash1::getInstance();
^
剪切并粘贴此代码以编译它:

#include <iostream>
using namespace std;
///////////////////////////////////////////////
//Class Singleton
 template<class T>
class Singleton {
public:

static T &getInstance() {
    if (!_instanceSingleton) {
        _instanceSingleton = new T();
    }
    return *_instanceSingleton;
}

private:
    static T *_instanceSingleton;
};

template<class T> T *Singleton<T>::_instanceSingleton = 0;

/////////////////////////////////////////////////
//Interface IHash
class IHash {

public:

    void function1() {
        cout << "function1";
    }

    virtual void recordHash(bool b) = 0;

    ~IHash() {
        dispose();
    }


private:

    void dispose() {
        cout << "dispose\n";
    }
};

///////////////////////////////////////////////////
//Class Hash1 is a singleton and inherits IHash

class Hash1 : public IHash, Singleton<Hash1> {
    friend class Singleton<Hash1>;
public:
    void recordHash(bool b);
private:
    //private constructor, is a sigleton
    Hash1();
};

Hash1::Hash1() {
    cout << "create Hash1\n";
}

void Hash1::recordHash(bool b) {
    cout << b << " recordHash\n";
}


////////////////////////////////////////////////////
//Factory for IHash
class HashFactory {
public:
    static IHash *get(int type) {
        if (type == 1)return &Hash1::getInstance();
//        if (type == 2)return &Hash2::getInstance();
//        if (type == 3)return &Hash3::getInstance();
        return 0;
    }
};

//////////////////////////////////////////////////////

int main() {
    int type=1;
    IHash *a = HashFactory::get(type);
    a->recordHash(true);
    a->function1();
    return 0;
}
#包括
使用名称空间std;
///////////////////////////////////////////////
//单件阶级
模板
单件阶级{
公众:
静态T&getInstance(){
如果(!\u instanceSingleton){
_instanceSingleton=new T();
}
返回*_instanceSingleton;
}
私人:
静态T*_instanceSingleton;
};
模板T*单例::_instanceSingleton=0;
/////////////////////////////////////////////////
//界面IHash
第i类{
公众:
无效函数1(){

cout
Hash1
Singleton
的继承是隐式私有的。将其更改为

class Hash1 : public IHash, public Singleton<Hash1> {
class Hash1:public IHash,public Singleton{

Hash1
Singleton
继承的内容是隐式私有的。请将其更改为

class Hash1 : public IHash, public Singleton<Hash1> {
class Hash1:public IHash,public Singleton{

Hash1
Singleton
继承的内容是隐式私有的。请将其更改为

class Hash1 : public IHash, public Singleton<Hash1> {
class Hash1:public IHash,public Singleton{

Hash1
Singleton
继承的内容是隐式私有的。请将其更改为

class Hash1 : public IHash, public Singleton<Hash1> {
class Hash1:public IHash,public Singleton{

你认为不应该使用单例设计模式吗?你也可以在你的类上使用static。你确定需要它吗?@LouisMartin Pierrat:或者只创建一个实例。
Hash1 my_instance;
。你认为不应该使用单例设计模式吗?你也可以在你的类上使用static。你确定需要它吗?@LouisMartin Pierrat:或者只创建一个实例。
Hash1 my_instance;
。你认为不使用单例设计模式吗?你也可以在你的类上使用static。你确定需要它吗?@LouisMartin Pierrat:或者只创建一个实例。
Hash1 my_instance;
。你认为不使用单例设计模式吗?你可以使用你的类也是静态的。你确定需要吗?@LouisMartin Pierrat:或者只创建一个实例。
Hash1 my_instance;