C++中的重新定义,头文件中的先前定义?

Max*_*ard 0 c++ header include guard redefinition

所以我很困惑.尝试实现先前在头文件中声明的方法时,我收到重新定义错误.我在标题中添加了包含防护,但仍然遇到了同样的错误.如果有人能向我解释我没有看到的东西,那就太棒了.

在main.cpp包含的文件中:2:./ something.cpp:7:12:错误:重新定义'method'int Thing :: method(void)^ ./ thing.hpp:12:6:注意:之前的定义在这里int方法(void){}; ^

- 编辑 -

我现在得到以下内容:

重复符号__ZN5Thing6methodEv in:main.o thing.o ld:1个用于体系结构x86_64的重复符号

thing.hpp:

#ifndef THING_H
#define THING_H

class Thing {

public:
        int a;
        int b;
        char c;

        int method(void) {};
        Thing(int anA, int aB, char aC): a(anA), b(aB), c(aC) {};


};

#endif
Run Code Online (Sandbox Code Playgroud)

thing.cpp

#include <iostream>
#include <stdio.h>
#include "thing.hpp"

using namespace std;

int Thing::method(void)
{
        return 5;

}
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include <iostream>
#include "thing.cpp"

using namespace std;

Thing* thing = new Thing(5,5,'c');

int main(int argc, char ** argv)
{
        cout << thing->method() <<endl;

}
Run Code Online (Sandbox Code Playgroud)

Jon*_*ter 7

在您的头文件中,您有:

int method(void) {};
Run Code Online (Sandbox Code Playgroud)

这是一个内联定义,而不是声明.该{}实际上是提供(尽管空)函数体编译器.{}如果要在另一个文件中定义该函数,请删除它.

此外,您#include "thing.cpp"不是#include "thing.hpp"main.cpp文件的顶部.