【发布时间】:2015-12-07 10:55:31
【问题描述】:
我有 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;
}
测试类头
#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 文件编译?
【问题讨论】:
-
你应该发一个mcve
-
您确定两个标题的内容相同吗?您是否检查过
#ifndef XXX_H警卫对于您的所有包含是否正确且不同?尝试查看预处理文件并查看是否包含正确的文件。
标签: c++ ubuntu compilation g++