为什么模板只能在头文件中实现?

Quote from The C++ standard library: a tutorial and handbook:

The only portable way of using templates at the moment is to implement them in header files by using inline functions.

Why is this?

(Clarification: header files are not the only portable solution. But they are the most convenient portable solution.)

转载于:https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file

csdnceshi67
bug^君 Book is out of date.
5 年多之前 回复
csdnceshi64
游.程 While it is true that placing all template function definitions into the header file is probably the most convenient way to use them, it is still not clear what's "inline" doing in that quote. There's no need to use inline functions for that. "Inline" has absolutely nothing to do with this.
5 年多之前 回复
csdnceshi74
7*4 The question is incorrect. There is another portable way. The template class can be explicitly instantiated - as has been pointed out by other answers.
7 年多之前 回复

14个回答

It is not necessary to put the implementation in the header file, see the alternative solution at the end of this answer.

Anyway, the reason your code is failing is that, when instantiating a template, the compiler creates a new class with the given template argument. For example:

template<typename T>
struct Foo
{
    T bar;
    void doSomething(T param) {/* do stuff using T */}
};

// somewhere in a .cpp
Foo<int> f; 

When reading this line, the compiler will create a new class (let's call it FooInt), which is equivalent to the following:

struct FooInt
{
    int bar;
    void doSomething(int param) {/* do stuff using int */}
}

Consequently, the compiler needs to have access to the implementation of the methods, to instantiate them with the template argument (in this case int). If these implementations were not in the header, they wouldn't be accessible, and therefore the compiler wouldn't be able to instantiate the template.

A common solution to this is to write the template declaration in a header file, then implement the class in an implementation file (for example .tpp), and include this implementation file at the end of the header.

// Foo.h
template <typename T>
struct Foo
{
    void doSomething(T param);
};

#include "Foo.tpp"

// Foo.tpp
template <typename T>
void Foo<T>::doSomething(T param)
{
    //implementation
}

This way, implementation is still separated from declaration, but is accessible to the compiler.

Another solution is to keep the implementation separated, and explicitly instantiate all the template instances you'll need:

// Foo.h

// no implementation
template <typename T> struct Foo { ... };

//----------------------------------------    
// Foo.cpp

// implementation of Foo's methods

// explicit instantiations
template class Foo<int>;
template class Foo<float>;
// You will only be able to use Foo with int or float

If my explanation isn't clear enough, you can have a look at the C++ Super-FAQ on this subject.

csdnceshi51
旧行李 I guess you can do. I don't believe it's necessary.
一年多之前 回复
weixin_41568196
撒拉嘿哟木头 Hi, Big Boss. I would like to ask: since the answer indicates a way which is to do some explicit instantiations in .cpp file. So I think in all the other files that use any instantiations of that template, I should/must write extern template *delaration* for saving costs, am I right? Otherwise there's no meaning of using explicit instantiations.
一年多之前 回复
csdnceshi52
妄徒之命 Emphasis on using a file extensions like .tpp in the second case (it must not be .cpp, otherwise there will be problems, or the use of macros is required to prevent cyclical inclusions).
一年多之前 回复
csdnceshi75
衫裤跑路 I think, answer needs an update.it's no longer "only in header", it's implementation-dependent, according to ISO standard's description of translation process. Compilers allowed to store data about template instantiation and use it on link stage. But that means that templates might be defined in .cpp files but it's up to compiler to support that.
一年多之前 回复
csdnceshi60
℡Wang Yan Is this why builds slow down dramatically after adding header libraries?
接近 2 年之前 回复
csdnceshi51
旧行李 I agree - that wording in this answer is not quite right. But I think Luc was trying to simplify (and used "compiler" in place of "your compiler and linker").
接近 2 年之前 回复
csdnceshi73
喵-见缝插针 Aha, it's not what linkers do, generating code, right? Then I think the thought process above should be in the answer. Because normally (when not using templates), compiler does not need to have access to the implementation. And that's maybe the why that people want to know.
大约 2 年之前 回复
csdnceshi73
喵-见缝插针 "Consequently, the compiler needs to have access to the implementation of the methods". Why would the compiler need to instantiate the methods? It does not when I'm declaring a non-template function, which is implemented in another cpp. Then, I think, it's the linker's job to find the implementation according to the function signature or how it does it. I don't see why the linker wouldn't just grab the instantiated signature and in addition to finding a normal function implementation, in case of a template signature it would find a template definition to generate the code.
大约 2 年之前 回复
csdnceshi70
笑故挽风 Do you mean the example with explicit instantiations? I don't really see what additional information I could give, you just need to write in a .cpp the lines that instantiate your template with the arguments you want.
2 年多之前 回复
csdnceshi58
Didn"t forge How use 3-rd example? Full code please.
2 年多之前 回复
csdnceshi71
Memor.の I created a template library .cpp and .h , and only included the header . Was trying to know why I am getting linker error for hours and finally with this answer I understand why
2 年多之前 回复
csdnceshi80
胖鸭 Explicit instantiation still needs the definition of the template. In this sense, it's not the second way, there is only one way .
2 年多之前 回复
csdnceshi70
笑故挽风 To instantiate a template, you need its definition. If the definition is not in the header file, the only place you can do instantiations is in the implementation file that contains the definition. If the definition is in the header file, anyone with access to the header file can instantiate the template, either by using it directly (by defining a variable of the type template, or calling the function template for instance) or by making an explicit instantiation (instantiating the template without using it).
接近 3 年之前 回复
weixin_41568183
零零乙 I'd like to know, is it possible to do the explicit instantiations from somewhere other than the class' header or source file? For instance, do them in main.cpp?
接近 3 年之前 回复
weixin_41568134
MAO-EYE Would like to add that Visual Studio (2015 Community Up3 in my case) might misbehave wildly when trying to implement the first solution (i.e. #including at the bottom) - see this for example. Had to exclude and re-include the implementation file from/to project to make it compile - rather ridiculous..
3 年多之前 回复
csdnceshi80
胖鸭 One can never understand this without knowing "separate compilation model", which is described in the link..
接近 4 年之前 回复
weixin_41568131
10.24 Using the template instant generates error C2512 "no appropriate default constructor available", even though i defined the default constructor as foo() { }
接近 4 年之前 回复
csdnceshi70
笑故挽风 Yes, a function template can be explicitely instantiated juste like a class template (see How do I explicitely instantiate a template function).
大约 4 年之前 回复
csdnceshi74
7*4 Can we use explicit instantiations with template function without a class?
大约 4 年之前 回复
csdnceshi64
游.程 I've added a sentence at the very start of this answer to clarify that the question is based on a false premise. If somebody asks "Why is X true?" when in fact X is not true, we should quickly reject that assumption.
4 年多之前 回复
csdnceshi78
程序go Use explicit instantiation otherwise you will get very slow builds soon..
5 年多之前 回复
csdnceshi70
笑故挽风 No, the members of a class are private by default, not protected.
接近 6 年之前 回复
csdnceshi59
ℙℕℤℝ when compiling some other .cpp file, the .cpp with the implementation is unknown (and might even not be available, think library).
接近 6 年之前 回复
csdnceshi70
笑故挽风 structs and classes are equivalent with the exception that the default access modifier for classes is "private", while it is public for structs. There are some other tiny differences that you can learn by looking at this question.
接近 6 年之前 回复
csdnceshi66
必承其重 | 欲带皇冠 is 'struct' means the same than 'class' here ?
接近 6 年之前 回复
csdnceshi65
larry*wei I don't think this explains the question that clearly, the key thing is obviously related with the compilation UNIT which is not mentioned in this post
7 年多之前 回复
csdnceshi56
lrony* "the compiler needs to have access to the implementation of the methods, to instantiate them with the template argument (in this case int). If these implementations were not in the header, they wouldn't be accessible" But why is an implementation in the .cpp file not accessible to the compiler? A compiler can also access .cpp information, how else would it turn them into .obj files? EDIT: answer to this question is in the link provided in this answer...
大约 8 年之前 回复
csdnceshi68
local-host Actually the explicit instantiation needs to be in a .cpp file which has access to the definitions for all of Foo's member functions, rather than in the header.
8 年多之前 回复

Plenty correct answers here, but I wanted to add this (for completeness):

If you, at the bottom of the implementation cpp file, do explicit instantiation of all the types the template will be used with, the linker will be able to find them as usual.

Edit: Adding example of explicit template instantiation. Used after the template has been defined, and all member functions has been defined.

template class vector<int>;

This will instantiate (and thus make available to the linker) the class and all its member functions (only). Similar syntax works for template functions, so if you have non-member operator overloads you may need to do the same for those.

The above example is fairly useless since vector is fully defined in headers, except when a common include file (precompiled header?) uses extern template class vector<int> so as to keep it from instantiating it in all the other (1000?) files that use vector.

csdnceshi62
csdnceshi62 Explicit instantiations must be in a place where the implementation of the template methods is visible. If your implementations are in a cpp file, then (without silliness) you can only explicitly instantiate in that same cpp file, not in any other cpp file.
接近 3 年之前 回复
csdnceshi70
笑故挽风 I'd like to know, is it possible to do the explicit instantiations from somewhere other than the class' header or source file? For instance, do them in main.cpp?
接近 3 年之前 回复
csdnceshi78
程序go Used after the template has been defined, "and all member functions has been defined". Thanks !
大约 3 年之前 回复
csdnceshi72
谁还没个明天 vector is not a good example because a container is inherently targeting "all" types. But it does happen very frequently that you create templates that are only meant for a specific set of types, for instance numeric types: int8_t, int16_t, int32_t, uint8_t, uint16_t, etc. In this case, it still makes sense to use a template, but explicitly instantiating them for the whole set of types is also possible and, in my opinion, recommended.
4 年多之前 回复
csdnceshi54
hurriedly% This can be good in many cases but generally breaks the purpose of template which is meant to allow you to use the class with any type without manually listing them.
大约 5 年之前 回复
csdnceshi76
斗士狗 Ugh. Good answer, but no real clean solution. Listing out all possible types for a template does not seem to go with what a template is supposed to be.
5 年多之前 回复

It's because of the requirement for separate compilation and because templates are instantiation-style polymorphism.

Lets get a little closer to concrete for an explanation. Say I've got the following files:

  • foo.h
    • declares the interface of class MyClass<T>
  • foo.cpp
    • defines the implementation of class MyClass<T>
  • bar.cpp
    • uses MyClass<int>

Separate compilation means I should be able to compile foo.cpp independently from bar.cpp. The compiler does all the hard work of analysis, optimization, and code generation on each compilation unit completely independently; we don't need to do whole-program analysis. It's only the linker that needs to handle the entire program at once, and the linker's job is substantially easier.

bar.cpp doesn't even need to exist when I compile foo.cpp, but I should still be able to link the foo.o I already had together with the bar.o I've only just produced, without needing to recompile foo.cpp. foo.cpp could even be compiled into a dynamic library, distributed somewhere else without foo.cpp, and linked with code they write years after I wrote foo.cpp.

"Instantiation-style polymorphism" means that the template MyClass<T> isn't really a generic class that can be compiled to code that can work for any value of T. That would add overhead such as boxing, needing to pass function pointers to allocators and constructors, etc. The intention of C++ templates is to avoid having to write nearly identical class MyClass_int, class MyClass_float, etc, but to still be able to end up with compiled code that is mostly as if we had written each version separately. So a template is literally a template; a class template is not a class, it's a recipe for creating a new class for each T we encounter. A template cannot be compiled into code, only the result of instantiating the template can be compiled.

So when foo.cpp is compiled, the compiler can't see bar.cpp to know that MyClass<int> is needed. It can see the template MyClass<T>, but it can't emit code for that (it's a template, not a class). And when bar.cpp is compiled, the compiler can see that it needs to create a MyClass<int>, but it can't see the template MyClass<T> (only its interface in foo.h) so it can't create it.

If foo.cpp itself uses MyClass<int>, then code for that will be generated while compiling foo.cpp, so when bar.o is linked to foo.o they can be hooked up and will work. We can use that fact to allow a finite set of template instantiations to be implemented in a .cpp file by writing a single template. But there's no way for bar.cpp to use the template as a template and instantiate it on whatever types it likes; it can only use pre-existing versions of the templated class that the author of foo.cpp thought to provide.

You might think that when compiling a template the compiler should "generate all versions", with the ones that are never used being filtered out during linking. Aside from the huge overhead and the extreme difficulties such an approach would face because "type modifier" features like pointers and arrays allow even just the built-in types to give rise to an infinite number of types, what happens when I now extend my program by adding:

  • baz.cpp
    • declares and implements class BazPrivate, and uses MyClass<BazPrivate>

There is no possible way that this could work unless we either

  1. Have to recompile foo.cpp every time we change any other file in the program, in case it added a new novel instantiation of MyClass<T>
  2. Require that baz.cpp contains (possibly via header includes) the full template of MyClass<T>, so that the compiler can generate MyClass<BazPrivate> during compilation of baz.cpp.

Nobody likes (1), because whole-program-analysis compilation systems take forever to compile , and because it makes it impossible to distribute compiled libraries without the source code. So we have (2) instead.

csdnceshi77
狐狸.fox It's not rhetoric. The question is "why do you have to implement templates in a header?", so I explained the technical choices the C++ language makes that lead to this requirement. Before I wrote my answer others already provided workarounds that are not full solutions, because there can't be a full solution. I felt those answers would be complemented by a fuller discussion of the "why" angle of the question.
接近 2 年之前 回复
csdnceshi63
elliott.david How's this an answer? It provides no solution but rhetoric only.
接近 2 年之前 回复
csdnceshi77
狐狸.fox You should be able to do it from any file that has access to the full template implementation (either because it's in the same file or via header includes).
接近 3 年之前 回复
csdnceshi64
游.程 I'd like to know, is it possible to do the explicit instantiations from somewhere other than the class' header or source file? For instance, do them in main.cpp?
接近 3 年之前 回复
weixin_41568110
七度&光 emphasized quote a template is literally a template; a class template is not a class, it's a recipe for creating a new class for each T we encounter
接近 4 年之前 回复

Although standard C++ has no such requirement, some compilers require that all function and class templates need to be made available in every translation unit they are used. In effect, for those compilers, the bodies of template functions must be made available in a header file. To repeat: that means those compilers won't allow them to be defined in non-header files such as .cpp files

There is an export keyword which is supposed to mitigate this problem, but it's nowhere close to being portable.

csdnceshi67
bug^君 This is almost the most accurate answer, except "that means those compilers won't allow them to be defined in non-header files such as .cpp files" is patently false.
8 年多之前 回复
weixin_41568208
北城已荒凉 You can, and you don't have to put "inline" even. But you'd be able to use them just in that cpp file and nowhere else.
大约 11 年之前 回复
weixin_41568126
乱世@小熊 Why can't I implement them in .cpp file with the keyword "inline"?
大约 11 年之前 回复

That is exactly correct because the compiler has to know what type it is for allocation. So template classes, functions, enums,etc.. must be implemented as well in the header file if it is to be made public or part of a library (static or dynamic) because header files are NOT compiled unlike the c/cpp files which are. If the compiler doesn't know the type is can't compile it. In .Net it can because all objects derive from the Object class. This is not .Net.

csdnceshi69
YaoRaoLov In fact, it's almost the opposite of the truth, which is that header files are very frequently compiled many times, whereas a source file is usually compiled once.
大约 4 年之前 回复
csdnceshi76
斗士狗 "header files are NOT compiled" - that's a really odd way of describing it. Header files can be part of a translation unit, just like a "c/cpp" file.
8 年多之前 回复

The compiler will generate code for each template instantiation when you use a template during the compilation step. In the compilation and linking process .cpp files are converted to pure object or machine code which in them contains references or undefined symbols because the .h files that are included in your main.cpp have no implementation YET. These are ready to be linked with another object file that defines an implementation for your template and thus you have a full a.out executable. However since templates need to be processed in the compilation step in order to generate code for each template instantiation that you do in your main program, linking won't help because compiling the main.cpp into main.o and then compiling your template .cpp into template.o and then linking won't achieve the templates purpose because I'm linking different template instantiation to the same template implementation! And templates are supposed to do the opposite i.e to have ONE implementation but allow many available instantiations via the use of one class.

Meaning typename T get's replaced during the compilation step not the linking step so if I try to compile a template without T being replaced as a concrete value type so it won't work because that's the definition of templates it's a compile time process, and btw meta-programming is all about using this definition.

A way to have separate implementation is as follows.

//inner_foo.h

template <typename T>
struct Foo
{
    void doSomething(T param);
};


//foo.tpp
#include "inner_foo.h"
template <typename T>
void Foo<T>::doSomething(T param)
{
    //implementation
}


//foo.h
#include <foo.tpp>

//main.cpp
#include <foo.h>

inner_foo has the forward declarations. foo.tpp has the implementation and include inner_foo.h; and foo.h will have just one line, to include foo.tpp.

On compile time, contents of foo.h are copied to foo.tpp and then the whole file is copied to foo.h after which it compiles. This way, there is no limitations, and the naming is consistent, in exchange for one extra file.

I do this because static analyzers for the code break when it does not see the forward declarations of class in *.tpp. This is annoying when writing code in any IDE or using YouCompleteMe or others.

Templates need to be instantiated by the compiler before actually compiling them into object code. This instantiation can only be achieved if the template arguments are known. Now imagine a scenario where a template function is declared in a.h, defined in a.cpp and used in b.cpp. When a.cpp is compiled, it is not necessarily known that the upcoming compilation b.cpp will require an instance of the template, let alone which specific instance would that be. For more header and source files, the situation can quickly get more complicated.

One can argue that compilers can be made smarter to "look ahead" for all uses of the template, but I'm sure that it wouldn't be difficult to create recursive or otherwise complicated scenarios. AFAIK, compilers don't do such look aheads. As Anton pointed out, some compilers support explicit export declarations of template instantiations, but not all compilers support it (yet?).

csdnceshi68
local-host "When a.cpp is compiled, it is not necessarily known that the upcoming compilation b.cpp will require an instance of the template, let alone which specific instance would that be." This cluster F is by design, in other words.
接近 2 年之前 回复
csdnceshi75
衫裤跑路 open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1426.pdf
5 年多之前 回复
csdnceshi67
bug^君 Ok, thanks for good example and explanation. Here is my question though: why compiler cannot figure out where template is called, and compile those files first before compiling definition file? I can imagine it can be done in a simple case... Is the answer that interdependencies will mess up the order pretty fast?
6 年多之前 回复
weixin_41568127
?yb? If that interests you, the paper is called "Why we can't afford export", it's listed on his blog (gotw.ca/publications) but no pdf there (a quick google should turn it up though)
大约 11 年之前 回复
weixin_41568127
?yb? So I don't think export isn't implemented 'yet'. It'll probably never get done by anyone else than EDG after the others saw how long it took, and how little was gained
大约 11 年之前 回复
weixin_41568127
?yb? export doesn't eliminate the need for source disclosure, nor does it reduce compile dependencies, while it requires a massive effort from compiler builders. So Herb Sutter himself asked compiler builders to 'forget about' export. As the time investment needed would be better spend elsewhere...
大约 11 年之前 回复
csdnceshi57
perhaps? "export" is standard, but it's just hard to implement so most of the compiler teams just haven't done yet.
大约 11 年之前 回复

Templates must be used in headers because the compiler needs to instantiate different versions of the code, depending on the parameters given/deduced for template parameters. Remember that a template doesn't represent code directly, but a template for several versions of that code. When you compile a non-template function in a .cpp file, you are compiling a concrete function/class. This is not the case for templates, which can be instantiated with different types, namely, concrete code must be emitted when replacing template parameters with concrete types.

There was a feature with the export keyword that was meant to be used for separate compilation. The export feature is deprecated in C++11 and, AFAIK, only one compiler implemented it. You shouldn't make use of export. Separate compilation is not possible in C++ or C++11 but maybe in C++17, if concepts make it in, we could have some way of separate compilation.

For separate compilation to be achieved, separate template body checking must be possible. It seems that a solution is possible with concepts. Take a look at this paper recently presented at the standards commitee meeting. I think this is not the only requirement, since you still need to instantiate code for the template code in user code.

The separate compilation problem for templates I guess it's also a problem that is arising with the migration to modules, which is currently being worked.

It means that the most portable way to define method implementations of template classes is to define them inside the template class definition.

template < typename ... >
class MyClass
{

    int myMethod()
    {
       // Not just declaration. Add method implementation here
    }
};
共14条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
一个正常能用的头文件,在MFC新项目中引用报错?
做一个简单的小程序,引用了一个matrix.h的头文件,里面都是模板定义; 在vs2010下控制台程序测试可以正常运行。 后来在mfc下做了一个dlg在添加头文件的时候出现了错误,有大佬说是重复包含头文件,或者相互引用头文件的问题,也有说#include "stdafx.h"这个文件的问题,实在不知道怎么办,恳请各位大佬指点。 ![图片说明](https://img-ask.csdn.net/upload/202002/15/1581700264_422638.png) 我在另一个程序中直接引用的,没报错。直接复制过来的内容就有了问题。 包含头文件情况:![图片说明](https://img-ask.csdn.net/upload/202002/15/1581700479_742251.png)
关于在VS code中添加头文件相对路径报错的问题
我在VScode中编写C++程序, 在c_cpp_properties.json的"includePath": 中添加了自己编写的头文件的相对路径,然而显示报错: tempCodeRunnerFile.cpp:1:10: 致命错误:S.h:No such file or directory #include "S.h" 在#include中直接写头文件的绝对路径能成功,并且在VS2019中添加同样的相对路径的操作却成功了,有大佬知道是什么原因吗?
为什么模板类的声明和实现必须定义在一个.h文件中?
”模板类的实现,脱离具体的使用,是无法单独的编译的;把声明和实现分开的做法也是不可取的,必须把实现全部写在头文件里面。“这个的原理是啥啊,越详细越好。
.h头文件中为什么不能正确引用iostream.h头文件?
.h头文件中为什么不能正确引用iostream.h头文件? ``` #include<iostream> using namespace std; ``` 为什么在.cpp文件中都可以正确引用iostream头文件,而我在.h文件中用同样的代码却并不能正确引用?总是说: ``` fatal error: iostream: No such file or directory #include<iostream> ``` 这是为什么??我是新手,求各位大神解答!感谢!
C语言头文件里放声明,其实现放哪?
我写了一个头文件a.h,实现在a.c中,主文件中#include "a.h",报错。。。我想知道头文件的声明和定义的规范使用方法。。谢谢。。
DEV C++ 头文件与头文件源码
请问我自定义一个头文件和头文件的源码,都放在同一文件夹中,该文件夹也设置为头文件路径中了。 之后在代码中调用头文件时,能找到头文件但头文件的源码无法找到,也就是找不到声明函数的定义。如何关联头文件和图文件源码?
函数模板在头文件和main函数中应该怎么写
第一次写,提示找不到标示符max,求指正 ![图片说明](https://img-ask.csdn.net/upload/201604/12/1460458025_550032.png) ![图片说明](https://img-ask.csdn.net/upload/201604/12/1460457818_773123.png) ![图片说明](https://img-ask.csdn.net/upload/201604/12/1460457848_436496.png)
已经安装了mysql,也有mysql.h头文件,但是g++编译的时候却报错找不到头文件
已经安装了mysql,也有mysql.h头文件(不在/usr/include目录下 ),但是编译的时候却报错找不到头文件,这种情况怎么办啊 ![图片说明](https://img-ask.csdn.net/upload/202001/14/1578996461_834665.png) 菜鸟求指点
请问getegid这个函数在vc中的哪个头文件中
请问vc中有这个函数吗,有的话是在哪个头文件中呢,没有的话是用什么代替呢?我在msdn上搜过,那上面显示头文件是<sys/syscall.h>,但是我试了一下显示没有这个头文件的错误信息。
static 函数定义在头文件中有什么作用
static 函数定义在头文件中有什么作用?学渣求教,static 不就是要限制作用域吗?
VC中头文件没有怎么办
某些头文件在tc中有 在VC中没有 那该怎么办呢
Java语言两个Java文件中怎么共享一个变量,两个文件都要调用它,怎么实现啊
Java语言两个Java文件中怎么共享一个变量,两个文件都要调用它,怎么实现啊
linux下头文件和实现怎么封装进动态库
看了半天,发现函数声明和实现都封装进了动态库中,但是就想问了,怎么把 头文件和实现文件一起封装进动态库里面去的!
Java怎么实现多个文件根据文件名生成同名文件夹并把文件分别添加到同名文件夹中 。
想问一下Java怎么实现多个文件根据文件名生成同名文件夹并把文件分别添加到同名文件夹中  就比如说有 1.txt,2.txt,3.txt 三个文件 然后生成1,2,3 三个文件夹 并把1.txt放入1文件夹 2.txt放入2文件夹 3.txt 放入3文件夹 想看看各位大佬有没有什么思路
c++引用c# dll,在C++头文件中引用,c# dll和c# namespace,总是提示找不到c# namespace
1.c++是clr工程 2.自定义一个c++ class AAA,在头文件中#using引用c# dll,然后using namespace c#命名空间 3.在c++工程中定义了一个class AAA的全局变量 4.编译的时候提示找不到c#的命名空间 求大神解释
求mysock头文件求mysock头文件
谁有mysock库 我需要那些头文件,自己不会写!谁有mysock库 我需要那些头文件,自己不会写!谁有mysock库 我需要那些头文件,自己不会写!谁有mysock库 我需要那些头文件,自己不会写!
我想实现在头文件中该变量怎么办
头文件中一个函数的变量想通过外界输入获取 该怎么办?![图片](https://img-ask.csdn.net/upload/201512/22/1450787704_442907.jpg)
linux下pandoc使用自定义的template模板将md文件转换为中文pdf文件-报错问题
## linux下pandoc使用自定义的template模板m将md文件转换为中文pdf文件报错问题 --- 在linux环境下使用pandoc将md文件转换为pdf文件(中文的),使用了自定义的模板,该模板来自于[tzengyuxio](https://github.com/tzengyuxio/pages/tree/gh-pages/pandoc)的pm-template.latex文件,我将该模板文件放到pandoc的模板文件夹下(linux默认的路径是:/usr/share/pandoc/data/templates) --- **linux下一安装pandoc(1.16.0.2),texlive2017并且已经配置好中文环境** *转换指令* ``` pandoc demo.md -o new.pdf --latex-engine=xelatex --template=pm-template.latex ``` - 报错问题 - - 1.第一次报错问题(已解决) ![图片说明](https://img-ask.csdn.net/upload/201912/19/1576724614_579189.png) 该问题已经通过[killercup](https://github.com/killercup/trpl-ebook/issues/18)得到了解决,在pm-templatex.latex文件导言区加入如下内容 ``` \newcommand{\tightlist}{% \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} ``` - - 2.**第二次报错问题(就是我想请教各位前辈的)** ![图片说明](https://img-ask.csdn.net/upload/201912/19/1576724974_966113.png) 如果哪位前辈知道这个报错问题的话,qing指点我一下,万分感谢
c++类实现中如何定义cmath头文件
c++类实现中如何定义cmath头文件 不知怎么的总是出错,即使和说上写的一样也通不过
新建STM32工程模板时,运行时头文件出现错误,如图
![新建stm32工程模板编译时头文件出错](https://img-ask.csdn.net/upload/201804/13/1523586087_854695.png)
终于明白阿里百度这样的大公司,为什么面试经常拿ThreadLocal考验求职者了
点击上面↑「爱开发」关注我们每晚10点,捕获技术思考和创业资源洞察什么是ThreadLocalThreadLocal是一个本地线程副本变量工具类,各个线程都拥有一份线程私有的数
程序员必须掌握的核心算法有哪些?
由于我之前一直强调数据结构以及算法学习的重要性,所以就有一些读者经常问我,数据结构与算法应该要学习到哪个程度呢?,说实话,这个问题我不知道要怎么回答你,主要取决于你想学习到哪些程度,不过针对这个问题,我稍微总结一下我学过的算法知识点,以及我觉得值得学习的算法。这些算法与数据结构的学习大多数是零散的,并没有一本把他们全部覆盖的书籍。下面是我觉得值得学习的一些算法以及数据结构,当然,我也会整理一些看过...
《奇巧淫技》系列-python!!每天早上八点自动发送天气预报邮件到QQ邮箱
此博客仅为我业余记录文章所用,发布到此,仅供网友阅读参考,如有侵权,请通知我,我会删掉。 补充 有不少读者留言说本文章没有用,因为天气预报直接打开手机就可以收到了,为何要多此一举发送到邮箱呢!!!那我在这里只能说:因为你没用,所以你没用!!! 这里主要介绍的是思路,不是天气预报!不是天气预报!!不是天气预报!!!天气预报只是用于举例。请各位不要再刚了!!! 下面是我会用到的两个场景: 每日下
死磕YOLO系列,YOLOv1 的大脑、躯干和手脚
YOLO 是我非常喜欢的目标检测算法,堪称工业级的目标检测,能够达到实时的要求,它帮我解决了许多实际问题。 这就是 YOLO 的目标检测效果。它定位了图像中物体的位置,当然,也能预测物体的类别。 之前我有写博文介绍过它,但是每次重新读它的论文,我都有新的收获,为此我准备写一个系列的文章来详尽分析它。这是第一篇,从它的起始 YOLOv1 讲起。 YOLOv1 的论文地址:https://www.c...
知乎高赞:中国有什么拿得出手的开源软件产品?(整理自本人原创回答)
知乎高赞:中国有什么拿得出手的开源软件产品? 在知乎上,有个问题问“中国有什么拿得出手的开源软件产品(在 GitHub 等社区受欢迎度较好的)?” 事实上,还不少呢~ 本人于2019.7.6进行了较为全面的回答,对这些受欢迎的 Github 开源项目分类整理如下: 分布式计算、云平台相关工具类 1.SkyWalking,作者吴晟、刘浩杨 等等 仓库地址: apache/skywalking 更...
20行Python代码爬取王者荣耀全英雄皮肤
引言 王者荣耀大家都玩过吧,没玩过的也应该听说过,作为时下最火的手机MOBA游戏,咳咳,好像跑题了。我们今天的重点是爬取王者荣耀所有英雄的所有皮肤,而且仅仅使用20行Python代码即可完成。 准备工作 爬取皮肤本身并不难,难点在于分析,我们首先得得到皮肤图片的url地址,话不多说,我们马上来到王者荣耀的官网: 我们点击英雄资料,然后随意地选择一位英雄,接着F12打开调试台,找到英雄原皮肤的图片...
简明易理解的@SpringBootApplication注解源码解析(包含面试提问)
欢迎关注文章系列 ,关注我 《提升能力,涨薪可待》 《面试知识,工作可待》 《实战演练,拒绝996》 欢迎关注我博客,原创技术文章第一时间推出 也欢迎关注公 众 号【Ccww笔记】,同时推出 如果此文对你有帮助、喜欢的话,那就点个赞呗,点个关注呗! 《提升能力,涨薪可待篇》- @SpringBootApplication注解源码解析 一、@SpringBootApplication 的作用是什...
西游记团队中如果需要裁掉一个人,会先裁掉谁?
2019年互联网寒冬,大批企业开始裁员,下图是网上流传的一张截图: 裁员不可避免,那如何才能做到不管大环境如何变化,自身不受影响呢? 我们先来看一个有意思的故事,如果西游记取经团队需要裁员一名,会裁掉谁呢,为什么? 西游记团队组成: 1.唐僧 作为团队teamleader,有很坚韧的品性和极高的原则性,不达目的不罢休,遇到任何问题,都没有退缩过,又很得上司支持和赏识(直接得到唐太宗的任命,既给袈...
Python语言高频重点汇总
Python语言高频重点汇总 GitHub面试宝典仓库 回到首页 目录: Python语言高频重点汇总 目录: 1. 函数-传参 2. 元类 3. @staticmethod和@classmethod两个装饰器 4. 类属性和实例属性 5. Python的自省 6. 列表、集合、字典推导式 7. Python中单下划线和双下划线 8. 格式化字符串中的%和format 9. 迭代器和生成器 10...
究竟你适不适合买Mac?
我清晰的记得,刚买的macbook pro回到家,开机后第一件事情,就是上了淘宝网,花了500元钱,找了一个上门维修电脑的师傅,上门给我装了一个windows系统。。。。。。 表砍我。。。 当时买mac的初衷,只是想要个固态硬盘的笔记本,用来运行一些复杂的扑克软件。而看了当时所有的SSD笔记本后,最终决定,还是买个好(xiong)看(da)的。 已经有好几个朋友问我mba怎么样了,所以今天尽量客观
程序员一般通过什么途径接私活?
二哥,你好,我想知道一般程序猿都如何接私活,我也想接,能告诉我一些方法吗? 上面是一个读者“烦不烦”问我的一个问题。其实不止是“烦不烦”,还有很多读者问过我类似这样的问题。 我接的私活不算多,挣到的钱也没有多少,加起来不到 20W。说实话,这个数目说出来我是有点心虚的,毕竟太少了,大家轻喷。但我想,恰好配得上“一般程序员”这个称号啊。毕竟苍蝇再小也是肉,我也算是有经验的人了。 唾弃接私活、做外
ES6基础-ES6的扩展
进行对字符串扩展,正则扩展,数值扩展,函数扩展,对象扩展,数组扩展。 开发环境准备: 编辑器(VS Code, Atom,Sublime)或者IDE(Webstorm) 浏览器最新的Chrome 字符串的扩展: 模板字符串,部分新的方法,新的unicode表示和遍历方法: 部分新的字符串方法 padStart,padEnd,repeat,startsWith,endsWith,includes 字...
Python爬虫爬取淘宝,京东商品信息
小编是一个理科生,不善长说一些废话。简单介绍下原理然后直接上代码。 使用的工具(Python+pycharm2019.3+selenium+xpath+chromedriver)其中要使用pycharm也可以私聊我selenium是一个框架可以通过pip下载 pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple/ 
阿里程序员写了一个新手都写不出的低级bug,被骂惨了。
你知道的越多,你不知道的越多 点赞再看,养成习惯 本文 GitHub https://github.com/JavaFamily 已收录,有一线大厂面试点思维导图,也整理了很多我的文档,欢迎Star和完善,大家面试可以参照考点复习,希望我们一起有点东西。 前前言 为啥今天有个前前言呢? 因为你们的丙丙啊,昨天有牌面了哟,直接被微信官方推荐,知乎推荐,也就仅仅是还行吧(心里乐开花)
Java工作4年来应聘要16K最后没要,细节如下。。。
前奏: 今天2B哥和大家分享一位前几天面试的一位应聘者,工作4年26岁,统招本科。 以下就是他的简历和面试情况。 基本情况: 专业技能: 1、&nbsp;熟悉Sping了解SpringMVC、SpringBoot、Mybatis等框架、了解SpringCloud微服务 2、&nbsp;熟悉常用项目管理工具:SVN、GIT、MAVEN、Jenkins 3、&nbsp;熟悉Nginx、tomca
Python爬虫精简步骤1 获取数据
爬虫的工作分为四步: 1.获取数据。爬虫程序会根据我们提供的网址,向服务器发起请求,然后返回数据。 2.解析数据。爬虫程序会把服务器返回的数据解析成我们能读懂的格式。 3.提取数据。爬虫程序再从中提取出我们需要的数据。 4.储存数据。爬虫程序把这些有用的数据保存起来,便于你日后的使用和分析。 这一篇的内容就是:获取数据。 首先,我们将会利用一个强大的库——requests来获取数据。 在电脑上安装
作为一个程序员,CPU的这些硬核知识你必须会!
CPU对每个程序员来说,是个既熟悉又陌生的东西? 如果你只知道CPU是中央处理器的话,那可能对你并没有什么用,那么作为程序员的我们,必须要搞懂的就是CPU这家伙是如何运行的,尤其要搞懂它里面的寄存器是怎么一回事,因为这将让你从底层明白程序的运行机制。 随我一起,来好好认识下CPU这货吧 把CPU掰开来看 对于CPU来说,我们首先就要搞明白它是怎么回事,也就是它的内部构造,当然,CPU那么牛的一个东
破14亿,Python分析我国存在哪些人口危机!
2020年1月17日,国家统计局发布了2019年国民经济报告,报告中指出我国人口突破14亿。 猪哥的朋友圈被14亿人口刷屏,但是很多人并没有看到我国复杂的人口问题:老龄化、男女比例失衡、生育率下降、人口红利下降等。 今天我们就来分析一下我们国家的人口数据吧! 更多有趣分析教程,扫描下方二维码关注vx公号「裸睡的猪」 即可查看! 一、背景 1.人口突破14亿 2020年1月17日,国家统计局发布
web前端javascript+jquery知识点总结
Javascript javascript 在前端网页中占有非常重要的地位,可以用于验证表单,制作特效等功能,它是一种描述语言,也是一种基于对象(Object)和事件驱动并具有安全性的脚本语言 ,语法同java类似,是一种解释性语言,边执行边解释。 JavaScript的组成: ECMAScipt 用于描述: 语法,变量和数据类型,运算符,逻辑控制语句,关键字保留字,对象。 浏览器对象模型(Br
Qt实践录:开篇
本系列文章介绍笔者的Qt实践之路。
在家远程办公效率低?那你一定要收好这个「在家办公」神器!
相信大家都已经收到国务院延长春节假期的消息,接下来,在家远程办公可能将会持续一段时间。 但是问题来了。远程办公不是人在电脑前就当坐班了,相反,对于沟通效率,文件协作,以及信息安全都有着极高的要求。有着非常多的挑战,比如: 1在异地互相不见面的会议上,如何提高沟通效率? 2文件之间的来往反馈如何做到及时性?如何保证信息安全? 3如何规划安排每天工作,以及如何进行成果验收? ......
作为一个程序员,内存和磁盘的这些事情,你不得不知道啊!!!
截止目前,我已经分享了如下几篇文章: 一个程序在计算机中是如何运行的?超级干货!!! 作为一个程序员,CPU的这些硬核知识你必须会! 作为一个程序员,内存的这些硬核知识你必须懂! 这些知识可以说是我们之前都不太重视的基础知识,可能大家在上大学的时候都学习过了,但是嘞,当时由于老师讲解的没那么有趣,又加上这些知识本身就比较枯燥,所以嘞,大家当初几乎等于没学。 再说啦,学习这些,也看不出来有什么用啊!
这个世界上人真的分三六九等,你信吗?
偶然间,在知乎上看到一个问题 一时间,勾起了我深深的回忆。 以前在厂里打过两次工,做过家教,干过辅导班,做过中介。零下几度的晚上,贴过广告,满脸、满手地长冻疮。   再回首那段岁月,虽然苦,但让我学会了坚持和忍耐。让我明白了,在这个世界上,无论环境多么的恶劣,只要心存希望,星星之火,亦可燎原。   下文是原回答,希望能对你能有所启发。   如果我说,这个世界上人真的分三六九等,
为什么听过很多道理,依然过不好这一生?
记录学习笔记是一个重要的习惯,不希望学习过的东西成为过眼云烟。做总结的同时也是一次复盘思考的过程。 本文是根据阅读得到 App上《万维钢·精英日课》部分文章后所做的一点笔记和思考。学习是一个系统的过程,思维模型的建立需要相对完整的学习和思考过程。以下观点是在碎片化阅读后总结的一点心得总结。
B 站上有哪些很好的学习资源?
哇说起B站,在小九眼里就是宝藏般的存在,放年假宅在家时一天刷6、7个小时不在话下,更别提今年的跨年晚会,我简直是跪着看完的!! 最早大家聚在在B站是为了追番,再后来我在上面刷欧美新歌和漂亮小姐姐的舞蹈视频,最近两年我和周围的朋友们已经把B站当作学习教室了,而且学习成本还免费,真是个励志的好平台ヽ(.◕ฺˇд ˇ◕ฺ;)ノ 下面我们就来盘点一下B站上优质的学习资源: 综合类 Oeasy: 综合
雷火神山直播超两亿,Web播放器事件监听是怎么实现的?
Web播放器解决了在手机浏览器和PC浏览器上播放音视频数据的问题,让视音频内容可以不依赖用户安装App,就能进行播放以及在社交平台进行传播。在视频业务大数据平台中,播放数据的统计分析非常重要,所以Web播放器在使用过程中,需要对其内部的数据进行收集并上报至服务端,此时,就需要对发生在其内部的一些播放行为进行事件监听。 那么Web播放器事件监听是怎么实现的呢? 01 监听事件明细表 名
3万字总结,Mysql优化之精髓
本文知识点较多,篇幅较长,请耐心学习 MySQL已经成为时下关系型数据库产品的中坚力量,备受互联网大厂的青睐,出门面试想进BAT,想拿高工资,不会点MySQL优化知识,拿offer的成功率会大大下降。 为什么要优化 系统的吞吐量瓶颈往往出现在数据库的访问速度上 随着应用程序的运行,数据库的中的数据会越来越多,处理时间会相应变慢 数据是存放在磁盘上的,读写速度无法和内存相比 如何优化 设计
一条链接即可让黑客跟踪你的位置! | Seeker工具使用
搬运自:冰崖的部落阁(icecliffsnet) 严正声明:本文仅限于技术讨论,严禁用于其他用途。 请遵守相对应法律规则,禁止用作违法途径,出事后果自负! 上次写的防社工文章里边提到的gps定位信息(如何防止自己被社工或人肉) 除了主动收集他人位置信息以外,我们还可以进行被动收集 (没有技术含量) Seeker作为一款高精度地理位置跟踪工具,同时也是社交工程学(社会工程学)爱好者...
作为程序员的我,大学四年一直自学,全靠这些实用工具和学习网站!
我本人因为高中沉迷于爱情,导致学业荒废,后来高考,毫无疑问进入了一所普普通通的大学,实在惭愧...... 我又是那么好强,现在学历不行,没办法改变的事情了,所以,进入大学开始,我就下定决心,一定要让自己掌握更多的技能,尤其选择了计算机这个行业,一定要多学习技术。 在进入大学学习不久后,我就认清了一个现实:我这个大学的整体教学质量和学习风气,真的一言难尽,懂的人自然知道怎么回事? 怎么办?我该如何更好的提升
前端JS初级面试题二 (。•ˇ‸ˇ•。)老铁们!快来瞧瞧自己都会了么
1. 传统事件绑定和符合W3C标准的事件绑定有什么区别? 传统事件绑定 &lt;div onclick=""&gt;123&lt;/div&gt; div1.onclick = function(){}; &lt;button onmouseover=""&gt;&lt;/button&gt; 注意: 如果给同一个元素绑定了两次或多次相同类型的事件,那么后面的绑定会覆盖前面的绑定 (不支持DOM事...
相关热词 c#导入fbx c#中屏蔽键盘某个键 c#正态概率密度 c#和数据库登陆界面设计 c# 高斯消去法 c# codedom c#读取cad文件文本 c# 控制全局鼠标移动 c# temp 目录 bytes初始化 c#
立即提问