2012-02-26 198 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*。修復它應該清除這兩個錯誤。