0

I've been trying to compile a program I've spent the past three days building, and I can't figure out how to get it to stop throwing an error. I keep getting the compile error "undefined reference to Foo::bar" where "bar" is a static ofstream declared in the Foo.h file.

Foo.h

Class Foo
{
    public:
          <insert methods>
    private:
          static ofstream& bar;
 }

Foo.cpp

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

Foo::Foo()
{
   bar.open("foobar.txt");
}

I keep getting the error message on the "bar" in Foo.cpp (there are multiple in the file). Any ideas on why?

johwiltb
  • 123
  • 1
  • 2
  • 10

1 Answers1

0
undefined reference to Foo::bar

That error means that you told the compiler this object exists...

class Foo {
      static ofstream& bar;
};

...and the compiler decided to use it.

But you never defined it. It's undefined.

Add this to Foo.cpp:

ofstream& Foo::bar = (something);
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180