我有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标题
#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()
^
但是,如果我更改&#34; testclass.h&#34;到&#34; testclass.hpp&#34;并且还将所有#include
语句从#include "testclass.h"
更改为#include "testclass.hpp"
,效果很好。
为什么我无法编译.h文件?无论如何都要用.h文件编译?
答案 0 :(得分:0)
最后,我发现有一个奇怪的&test; testobject.h.gch&#34;同一目录下的文件。删除后它工作正常。