2012-02-25 61 views
2

我有這個定義我的測試類:「未定義的引用」嘗試引用靜態字段

#ifndef TEST_H 
#define TEST_H 

#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <fstream> 

class Test { 
     public: 
      static bool testAll(bool debug = false);   
     private: 
      static bool testVector2D(bool debug = false); 
      static bool testPolygon(bool debug = false); 
      static bool testRectangle(bool debug = false); 
      static bool testMap(bool debug = false); 

      static std::ofstream outStream; 
      static std::ifstream inStream; 

      static void prepareWriting(); 
      static void prepareReading(); 

      const char tempFileName[]; 
}; 

當我嘗試使用測試:: outStream或Test :: inStream中,例如這裏:

void Test::prepareWriting() { 
    if (Test::inStream.is_open()) { 
     Test::inStream.close(); 
    } 
    Test::outStream.open(testFileName,ios::out); 
} 

我得到這個消息:「未定義參考`測試::插播「」

我讀過一些關於在.cpp文件inicializating靜態成員,但我不知道該怎麼做與fstream的

+0

不是一個確切的重複數據刪除,但這包含相關信息:http://stackoverflow.com/questions/185844/initializing-private -static-members – 2012-02-25 17:49:14

+1

在C++中,你不必製作一個類的靜態成員函數。您可以改爲使用名稱空間。 – 2012-02-25 17:49:46

+2

此問題由[C++ SO FAQ條目](http://loungecpp.wikidot.com/faq#toc4)回答 – 2012-02-25 17:49:49

回答

3

您需要定義您定義的其他Test方法流:

std::ofstream Test::outStream; 
std::ifstream Test::inStream;