Can extern and static be used together in a variable declaration.

Hi guys,

I am trying to write a singleton class with a static pointer as a member variable and also a map variable.
But if I use extern, in both the cases I get the error.

Here is the error.



In file included from animal_factory.cpp:1:0:
animal_factory.h:18:27: error: storage class specified for ‘factoryMap’
         extern FactoryMap factoryMap;
                           ^~~~~~~~~~
animal_factory.h:19:38: error: conflicting specifiers in declaration of ‘animalFactory’
         extern static AnimalFactory *animalFactory;
                                      ^~~~~~~~~~~~~
animal_factory.cpp:3:31: error: ‘AnimalFactory* AnimalFactory::animalFactory’ is not a static data member of ‘class AnimalFactory’
 AnimalFactory* AnimalFactory::animalFactory = nullptr;
                               ^~~~~~~~~~~~~
animal_factory.cpp: In static member function ‘static AnimalFactory* AnimalFactory::getAnimalFactory()’:
animal_factory.cpp:17:8: error: ‘animalFactory’ was not declared in this scope
     if(animalFactory == nullptr)
        ^~~~~~~~~~~~~
animal_factory.cpp:19:12: error: ‘animalFactory’ was not declared in this scope
     return animalFactory;
            ^~~~~~~~~~~~~



Here is the code :

animal_factory.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef ANIMAL_FACTORY_H_
#define ANIMAL_FACTORY_H_

#include "animal.h"
#include "cat.h"
#include "dog.h"
#include <map>
#include <cstring>

class AnimalFactory
{
    private:
        AnimalFactory();
        AnimalFactory(const AnimalFactory &);
        AnimalFactory & operator=(const AnimalFactory &);

        typedef std::map<std::string, CreateAnimalFn> FactoryMap;
        extern FactoryMap factoryMap;    
        extern static AnimalFactory *animalFactory;  

    public:
        static AnimalFactory* getAnimalFactory();
        void Register(const std::string &, CreateAnimalFn);
        Animal* createAnimal(const std::string &, unsigned int);
};

#endif 


animal_factory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "animal_factory.h"

AnimalFactory* AnimalFactory::animalFactory;  //If this is not correct way of defining, kindly let me know what's the correct way
AnimalFactory* AnimalFactory::animalFactory = nullptr;
FactoryMap AnimalFactory::factoryMap;//If this is not correct way of defining, kindly let me know what's the correct way
AnimalFactory::AnimalFactory()
{
    Register("cat", &Cat::create);
    Register("dog", &Dog::create);
}

AnimalFactory::AnimalFactory(const AnimalFactory &) {}

AnimalFactory & AnimalFactory::operator=(const AnimalFactory &) { return *this;}

AnimalFactory* AnimalFactory::getAnimalFactory()
{
    if(animalFactory == nullptr)
        return new AnimalFactory();
    return animalFactory;
}

void AnimalFactory::Register(const std::string &s, CreateAnimalFn fPtr)
{
    factoryMap[s] = fPtr;
}

Animal* AnimalFactory::createAnimal(const std::string &s, unsigned int p_nLegs)
{
    auto it = factoryMap.find(s);

    if(it != factoryMap.end())
        return it->second(p_nLegs);

    return nullptr;
}


Last edited on
Topic archived. No new replies allowed.