为什么在模板头的末尾添加.cpp不是循环依赖
Why it is not circular dependency to add .cpp at end of template header?
我不敢问为什么我需要在模板声明时在.h
的末尾添加.cpp
。因为它已经在StackOverflow中被回答过很多次了。
但我的问题是,为什么它不是循环依赖,或者编译器如何不继续添加.cpp
到.h
和.h
到.cpp
,当我添加。cpp在头结束?
c++ 11试图解决这个奇怪的模板需求吗?
@Edit:只包括头文件
#ifndef MYMAP
#define MYMAP
#include <iostream>
#include <string>
using namespace std;
template<typename ValType>
class MyMap
{
public:
MyMap();
~MyMap();
void add(string key, ValType val);
ValType getValue(string key);
private:
static const int NumBuckets = 99;
struct cellT
{
string key;
ValType val;
cellT* next;
};
cellT *buckets[NumBuckets];
int hash(string key, int numBuckets);
cellT* findCellForKey(string key, cellT *head);
MyMap(MyMap&);
MyMap operator = (MyMap&);
};
#include "MyMap.cpp" //included .cpp
#endif
@Edit 2: MyMap.cpp
#include "MyMap.h"
//rest of the code
谢谢。
撇开这种技术的优点不谈,您的问题中的代码之所以有效,是因为包含保护:
#ifndef MYMAP
#define MYMAP
...
#endif
第二次包含.h
时,它实际上是一个no-op,因为MYMAP
在第一次循环中已经定义了。
注:不要在头文件中执行using namespace std
。任何包含此标题的代码都将把整个std
名称空间带入当前作用域,无论他们是否想要它
相关文章:
- 我应该将除 .cpp 以外的其他文件添加到 git 中吗?
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 如何将默认代码添加到我在 vim 中打开的每个 cpp 文件?
- 将 CPP 类添加到 Android 项目中
- 如何使用 PHP-CPP 在 PHP 类中添加静态数组
- 如何在库项目中添加头文件和 cpp 文件
- C++/CPP 读取添加的十六进制地址
- 如何将几个.cpp文件添加到一个cmakelists.txt中
- 链接列表添加CPP
- 如何使用 DevC++ 将 lodepng.cpp 和 lodepng.h 添加到我的项目中
- 如何在CPP中添加XMLXPathRegistern(Libxml2)中的多个名称空间
- 找不到构造函数 - 如何将 CPP 文件添加到 Qt 构建过程
- Python 子进程.Popen 在 g++ 中编译,自动添加.cpp文件
- CMAKE 将 java 库添加到 cpp 项目中
- 为什么我不能通过CPP返回将char*添加到char*中
- 产品列表中未添加哪些产品?(Supermart.cpp)
- C ++为编译添加了一个非CPP(例如TXT文件)依赖项
- 如何将新的.cpp文件添加到Android的C 库中
- 将 class.h 和 class.cpp 添加到 makefile
- 如何将两个CPP添加到一个CPP程序文件中?