gpt4 book ai didi

c++ - 头文件中的单例编译错误

转载 作者:行者123 更新时间:2023-11-30 02:05:39 26 4
gpt4 key购买 nike

这是我的课:

#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 的定义不能在 header 中。

关于c++ - 头文件中的单例编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9449657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com