Question

I am currently working on a c++ project after an absence and it seems that I have forgotten some syntax or method that would allow my code to compile correctly. After attempting to setup my environment, I am met with redefinition errors which I, of course, did not mean to through.

The error is as follows on compile

src/foo.cpp:4:7: error: redefinition of ‘class foo’
In file included from src/foo.cpp:2:0:
./headers/foo.h:7:7: error: previous definition of ‘class foo’

Where I don't believe I am redefining foo in my space; I know that this is a result of using the foo.h header file I have defined.

Here is the code I am compiling

// foo.h
#ifndef FOO_H
#define FOO_H

#include <iostream>

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

#endif /* FOO_H */

// foo.cpp
#include <foo.h>

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

foo::foo(){}

int foo::test(){
    std::cout << "Derp!";
    return 0;
}

foo::~foo(){}

How should I set my header or cpp file up to fix this issue?

Was it helpful?

Solution

You've correctly prototyped your methods within the class, so redefining your class to implement the methods are wrong. Remove the duplicate class, all you need to do is define them as such in your foo.cpp file:

foo::foo()       { .. }
foo::~foo()      { .. }
void foo::test() { .. }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top