user1930581
user1930581

Reputation: 1565

C++11 multiple definitions of class function

RESOLVED: I'm a complete doofus. After pouring through all the files carefully and tracing back through our proprietary dependency file generator, it turns out that there were indeed rogue .cpp includes that had recently been added into the file system, and that was causing the issue. Sorry, and thanks for all the help everyone! XD

I'm having a frustrating difficulty with an obscure bug I was hoping someone could help with. Basically, I'm declaring a generic class in a header file like so:

//foo.h
#pragma once

class foo
{
  public:
    foo();
    ~foo();
    void random_function();
};

Note that I've declared pragma once to prevent multiple definitions, and all function prototypes are placed in class definition. The corresponding class is defined like so:

// foo.cpp
#include "foo.h"

foo::foo() {
}

foo::~foo(){
}

void foo::random_function(){
//do stuff
}

This class gets used in quite a number of other files. For example:

// bar.h
#include “foo.h”

class bar
{
  bar();
  ~bar();
  std::shared_ptr<foo> get_foo();
  std::shared_ptr<foo> my_foo;
};

But when I compile, I get the following error for each function member of the class definition:

bar.o: In function `foo:foo()':
dir/foo.cpp:80: multiple definition of `foo::foo()'
blah.o:dir/foo.cpp:80: first defined here
bar.o: In function `foo:foo()':
dir/foo.cpp:80: multiple definition of `foo::foo()'
blah.o:dir/foo.cpp:80: first defined here

Note how it appears to repeat the same 3-line error twice in a row. This same error pattern is repeated for every constructor/destructor/function declaration. Also note how it appears to be saying that the function foo() in foo.cpp is first defined in foo.cpp, which does'nt seem to make much sense. But I just noticed, that it starts with bar.o and then says blah.o...?

Really scratching my head on this one, can't seem to decipher where the issue is coming from or where I should look. Any help would be greatly appreciated! :D

Addendums:

Upvotes: 0

Views: 1924

Answers (1)

DGomez
DGomez

Reputation: 1480

Try adding in foo.h

#ifndef FOO_H
#define FOO_H

//foo class and stuff

#endif

Upvotes: 1

Related Questions