在头文件内的singleton中编译错误

compile errors in singleton inside head file

本文关键字:编译 错误 singleton 文件      更新时间:2023-10-16

这是我的类:

#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中,我试图用谷歌搜索,但我没有找到这个编译器消息错误和我的代码之间的链接。。

您需要更改以下内容:

static pointPool* getInstance()

对此:

static PointPool* getInstance()

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

这一行PointPool* PointPool::instance = 0;应该在.cpp文件中,否则将有多个副本。

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