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

Compile under Ubuntu with different header type

本文关键字:Ubuntu 编译 类型      更新时间:2023-10-16

我有三个文件,即"main.cpp"、"testclass.cpp"answers"testclass.h"。我通过调用编译这些文件

g++ testclass.cpp main.cpp

main.cpp

#include <iostream>
#include "testclass.hpp"
int main()
{
    testclass foo(56);
    std::cout << "Object in cppt" << numberobject.getNumber() << "n";
    return 0;
}

测试类头

#ifndef TESTCLASS_H
#define TESTCLASS_H
class testclass
{
 private:
    int number;
 public:
    testclass();
    testclass(int);
    int getNumber();
};
#endif //TESTCLASS_H

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"文件。删除它后,它运行良好。