“未定义参考”试图引用静态字段 [英] "Undefined reference" trying to reference an static field
本文介绍了“未定义参考”试图引用静态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的测试类有这个定义:
#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时,例如: p>
void Test :: prepareWriting(){
if(Test :: inStream.is_open()){
Test :: inStream.close();
}
Test :: outStream.open(testFileName,ios :: out);
}
我得到这个msg:未定义的引用`Test :: inStream'
我已经阅读了关于.cpp文件中的静态成员的信息,但是我不知道如何使用fstream的
解决方案您需要定义其中定义测试
方法的流:
std :: ofstream Test :: outStream;
std :: ifstream测试:: inStream;
I have this definition for my Test class:
#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[];
};
When I try to use Test::outStream or Test::inStream, for example here:
void Test::prepareWriting() {
if (Test::inStream.is_open()) {
Test::inStream.close();
}
Test::outStream.open(testFileName,ios::out);
}
I get this msg: "undefined reference to `Test::inStream'"
I have read something about inicializating the static members in the .cpp file, but I don't know how to do that with fstream's
解决方案 You need to define the streams where you define the other Test
methods:
std::ofstream Test::outStream;
std::ifstream Test::inStream;
这篇关于“未定义参考”试图引用静态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文