我总是使用一个*.h文件作为我的类定义,但在阅读了一些boost库代码后,我意识到它们都在使用*.hpp.我一直厌恶那个文件扩展名,我想主要是因为我不习惯它.
使用*.hpp过的优点和缺点是*.h什么?
.cc和.cpp文件扩展名有什么区别?
从Google,我了解到它们都来自C++语言,但我不确定它们之间的差异.
所以,我听说C++模板不应该分为标题(.h)和源(.cpp)文件.
例如,像这样的模板:
template <class T>
class J
{
T something;
};
Run Code Online (Sandbox Code Playgroud)
这是真的?为什么会这样?
如果因为这个我必须将声明和实现放在同一个文件中,我应该把它放在.h文件或.cpp文件中吗?
假设我有2个头文件,1个.ipp扩展文件和一个main.cpp文件:
第一个头文件(如Java中的接口):
template<class T>
class myClass1{
public:
virtual int size() = 0;
};
Run Code Online (Sandbox Code Playgroud)
第二个头文件:
#include "myClass1.h"
template<class T>
class myClass2 : public myClass1<T>
public:
{
virtual int size();
private:
int numItems;
};
#include "myClass2.ipp"
Run Code Online (Sandbox Code Playgroud)
然后是我的myClass2.ipp文件:
template <class T>
int myClass2<T>::size()
{
return numItems;
}
Run Code Online (Sandbox Code Playgroud)
最后一个是我的主要:
#include "myclass2.h"
void tester()
{
myClass2<int> ForTesting;
if(ForTesting.size() == 0)
{
//......
}
else
{
//.....
}
}
int main(){
tester();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
myClass1,myClass2和myClass2.ipp属于头文件.源文件中的main.cpp.使用这种方式实现程序而不是仅使用.h和.cpp文件有什么好处?什么是.ipp扩展名的文件?.ipp和.cpp之间的区别?
可能重复:
正确的C++代码文件扩展名?.cc vs .cpp
.c vs .cc vs. .cpp vs .hpp vs .h vs .cxx
我在带扩展名的文件中编写了C++代码.cpp,.cc或者.cxx
任何人都可以解释这三者之间有什么区别,哪一种最好(是平台依赖的),为什么?
我目前正在研究cygwin.
我正在尝试将一个 cpp 文件添加到具有以下设置的 arduino 项目中...
project
--folder
--foo.h
--foo.cpp
--project.ino
Run Code Online (Sandbox Code Playgroud)
我#include "folder/foo.h在project.ino. 然而,虽然头文件提供了函数的原型,但函数定义在 cpp 文件中。当我尝试使用 Arduino IDE 编译代码时,它失败并显示错误
Undefined reference to 'bar()'
并bar()位于foo.cpp
我看了这个,但我没有设置sketch/import library(但是我有sketch/include library,但是我没有看到任何接近使用自定义文件夹位置的东西)
我也看了这个。但与上述相同,该设置在我的 ide 中不存在。(我最近下载的)
代码
//project.ino
#include "folder/foo.h"
void setup() {
Serial.begin(9600);
}
void loop() {
bar();
}
Run Code Online (Sandbox Code Playgroud)
//folder/foo.h
#include "Arduino.h"
void bar();
Run Code Online (Sandbox Code Playgroud)
//folder/foo.cpp
#include "foo.h"
void bar() {
Serial.println("bar");
}
Run Code Online (Sandbox Code Playgroud)
错误
/tmp/ccNTRFfU.ltrans0.ltrans.o: In function `loop':
/home/temporary/project/project.ino:9: undefined reference to …Run Code Online (Sandbox Code Playgroud)