尝试引用静态字段的“未定义引用”

时间:2012-02-25 17:44:37

标签: c++ undefined-reference

我的Test类有这个定义:

#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[];
};   

当我尝试使用Test :: outStream或Test :: inStream时,例如:

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

我得到这个消息:“未定义的引用`test :: inStream'”

我已经阅读了一些关于.cpp文件中的静态成员的内容,但我不知道如何使用fstream的

1 个答案:

答案 0 :(得分:3)

您需要定义用于定义其他Test方法的流:

std::ofstream Test::outStream;
std::ifstream Test::inStream;
相关问题