C++ 使用不同的头类型在Ubuntu下编译

C++ 使用不同的头类型在Ubuntu下编译,c++,ubuntu,compilation,g++,C++,Ubuntu,Compilation,G++,我有3个文件,即“main.cpp”、“testclass.cpp”和“testclass.h”。我通过调用以下命令编译文件: g++ testclass.cpp main.cpp main.cpp #include <iostream> #include "testclass.hpp" int main() { testclass foo(56); std::cout << "Object in cpp\t" << numberobje

我有3个文件,即“main.cpp”、“testclass.cpp”和“testclass.h”。我通过调用以下命令编译文件:

g++ testclass.cpp main.cpp
main.cpp

#include <iostream>
#include "testclass.hpp"

int main()
{
    testclass foo(56);

    std::cout << "Object in cpp\t" << numberobject.getNumber() << "\n";

    return 0;
}
testclass.cpp

#include "testclass.hpp"

testclass::testclass()
{
}

testclass::testclass(int number)
{
    this->number = number;
}


int testclass::getNumber()
{
    return number;
}
将出现编译错误

testclass.cpp:7:1: error: prototype for ‘testclass::testclass(int)’ does not match any in class ‘testclass’
 testclass::testclass(int number)
 ^
testclass.h:4:7: error: candidates are: testclass::testclass(const testclass&)
 class testclass
       ^
testclass.cpp:3:1: error:                 testclass::testclass()
 testclass::testclass()
 ^
但是,如果我将“testclass.h”更改为“testclass.hpp”,并将所有
#include
语句从
#include“testclass.h”
更改为
#include“testclass.hpp”
,那么它工作得很好


为什么我不能编译.h文件?还有用.h文件编译的方法吗?

最后,我发现有一个奇怪的“testobject.h.gch”“同一目录下的文件。在我删除它后,它可以正常工作。

您应该发布一个帖子。您确定两个标题的内容相同吗?您是否检查了XXX_H防护装置是否正确且与您的所有产品不同?尝试查看预处理的文件,并查看是否包含正确的文件。
testclass.cpp:7:1: error: prototype for ‘testclass::testclass(int)’ does not match any in class ‘testclass’
 testclass::testclass(int number)
 ^
testclass.h:4:7: error: candidates are: testclass::testclass(const testclass&)
 class testclass
       ^
testclass.cpp:3:1: error:                 testclass::testclass()
 testclass::testclass()
 ^