在head文件中的singleton中编译错误

时间:2012-02-26 01:12:30

标签: c++ singleton

这是我的班级:

#ifndef POINTPOOL_H_
#define POINTPOOL_H_

#include <list>
#include <iostream>

#include "ofPoint.h"

// adapted from https://gist.github.com/1124832

class PointPool
{
    private:
        std::list<ofPoint*> resources;
        static PointPool* instance;

        PointPool() {};

    public:
        ~PointPool() {};                      // 1
        static pointPool* getInstance()       // 2
        {
            if (instance == 0)
            {
                instance = new PointPool();
            }
            return instance;
        }

        Resource* getPoint()
        {
            if (resources.empty())
            {
                std::cout << "Creating new." << std::endl;
                return new ofPoint();
            }
            else
            {
                std::cout << "Reusing existing." << std::endl;
                ofPoint* resource = resources.front();
                resources.pop_front();
                return resource;
            }
        }

        void disposePoint(ofPoint* object)
        {
            object->x = 0;
            object->y = 0;
            object->z = 0;
            resources.push_back(object);
        }
};

PointPool* PointPool::instance = 0;

#endif /* POINTPOOL_H_ */

我得到了

expected unqualified-id at end of input

在评论1和

expected ‘;’ before ‘*’ token

在评论2我试图谷歌但我不知道这个编译器消息错误和我的代码之间的链接..

3 个答案:

答案 0 :(得分:2)

你需要改变这个:

static pointPool* getInstance()

对此:

static PointPool* getInstance()

构造函数和析构函数之后的分号也是不必要的。而且,正如Ed所提到的,PointPool :: instance的定义不能在标题中。

答案 1 :(得分:1)

此行PointPool* PointPool::instance = 0;应位于.cpp文件中 - 否则您将拥有多个副本。

答案 2 :(得分:1)

pointPool*中也有拼写错误,应该是PointPool*。修复它应该清除这两个错误。