Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对foo::bar流静态的未定义引用_C++ - Fatal编程技术网

C++ 对foo::bar流静态的未定义引用

C++ 对foo::bar流静态的未定义引用,c++,C++,我一直在尝试编译一个我花了三天时间构建的程序,但我不知道如何让它停止抛出错误。我不断得到编译错误“undefined reference to Foo::bar”,其中“bar”是Foo.h文件中声明的静态流 Foo.h Class Foo { public: <insert methods> private: static ofstream& bar; } Class-Foo { 公众: 私人: 流和棒的静态; }

我一直在尝试编译一个我花了三天时间构建的程序,但我不知道如何让它停止抛出错误。我不断得到编译错误“undefined reference to Foo::bar”,其中“bar”是Foo.h文件中声明的静态流

Foo.h

Class Foo
{
    public:
          <insert methods>
    private:
          static ofstream& bar;
 }
Class-Foo
{
公众:
私人:
流和棒的静态;
}
Foo.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include "EventReport.h"
using namespace std;

Foo::Foo()
{
   bar.open("foobar.txt");
}
#包括
#包括
#包括
#包括“EventReport.h”
使用名称空间std;
Foo::Foo()
{
打开(“foobar.txt”);
}
我一直在Foo.cpp中的“条”上看到错误消息(文件中有多条)。你知道为什么吗

undefined reference to Foo::bar
这个错误意味着你告诉编译器这个对象存在

class Foo {
      static ofstream& bar;
};
…编译器决定使用它

但你从未定义过它。没有定义

将此添加到Foo.cpp:

ofstream& Foo::bar = (something);

... 我应该写一个宏。:)“某物”应该是什么类型?我仍然在努力学习C++概念。@ JurWiTB,它应该引用一个现存的<代码>流< /COD>对象。