2012-02-26 201 views
1

这是我的课:编译错误

#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我试图谷歌,但我不认为这个编译器消息错误和我的代码之间的链接..

+0

Singleton ???????为什么??????????????????? – 2012-02-26 01:15:58

回答

2

您需要更改此:

static pointPool* getInstance() 

要这样:

static PointPool* getInstance() 

您的构造和析构后的分号也是不必要的。而且,正如Ed所说的,PointPool :: instance的定义不能在头文件中。

+1

首都总是会让你有时 - 猜测它是死刑! – 2012-02-26 01:27:08

1

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

1

您还有一个错字pointPool*,应该是PointPool*。修复它应该清除这两个错误。