C++ Connecting two .cpp files [duplicate] - c++

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 9 years ago.
I have a problem connecting two .cpp files in C++. Here are my files
Header.h
//Header.h
template <class T> class asd{
asd();
check();
print();
}
file1.cpp
//file1.cpp
//defines all methods in class asd
#include "Header.h"
template<class T> asd<T>:: asd(T a, T b){//codes}
template<class T> T asd<T>:: check(T a){//codes}
template<class T> void asd<T>::print(){//codes}
file2.cpp
//file2.cpp
//main method
#include "Header.h"
int main(){//codes}
The thing I do not understand is that the code runs fine when I put main() inside file1.cpp, but it wont compile when I separate them into two files. Can someone please give pointers?
Edit:
For those with the same problem, solution can be found here:
http://www.cplusplus.com/forum/articles/14272/

Member functions of a class template should appear in the header file. Just move the function definitions from file1.cpp to Header.h.
Imagine you are the compiler. When compiling main, if you attempt to instantiate asd in any way, the compiler needs to be able to see the function definitions to generate the appropriate code. For example, if in main you do asd<int> my_asd;, the compiler needs to instantiate asd with T replaced with int. It can't do that for the functions if it can't see the function definitions.

Related

Why multiple definitions of a template does not generate a link error? [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 1 year ago.
Assume these two cc files:
0.cc
#include <iostream>
template <class T>
T test(){
return 5;
}
int main(){
return test<int>();
}
1.cc
template <class T>
T test(){
return 5;
}
We compile them with g++ 0.cc 1.cc.
This compiles and runs successfully but if above functions were not templates then we would get a duplicate symbol error during compilation and linking phase.
Why using templates does not generate a link error?
Template is not instantiated until it is called in the code. Think of it as a local function to the compiled object.
Therefore, template as such is not exported and is not subject to linking of any kind, as it can't be referenced from another object. Should you be calling template defined in another header, it would still be instantiated in the local object.

Static variable in h-file C++ template library. Is it OK? [duplicate]

This question already has answers here:
How to have static data members in a header-only library?
(3 answers)
Closed 6 years ago.
I am developing lightweight parser as C++ h-file template library.
Gramma is described in specific BNF-like notation using overloaded operators on some classes which should be enumerated somehow. I need just one global variable as some counter performing it.
I do not want to use extern int var; in h-file and int var; in cpp-file because all my stuff in single header file and now the user just needs to include it.
I can declare static int var; in header file but copy of this variable appears in all object files where my header file is included.
Is it OK for template library?
Any suggestions?
As already mentioned you can use singleton pattern.
This version doesn't require definition of static member in template cpp file.
template <typename T> class Tmpl
{
public:
static Tmpl<T>& GlobalInstance()
{
static Tmpl<T> m_Singleton;
return m_Singleton;
};
};

C++: static variables in the class to be included in multiple cpp files [duplicate]

This question already has answers here:
Declaring static data members of normal class and class template
(2 answers)
Closed 9 years ago.
// A.h
class A {
public:
static int a;
};
int A::a = 0;
If I try to include A.h in multiple .cpp files, link would fail with multiple definition of A::a. I think this makes sense because each .obj file contains A::a
However, I could use a template,
// A.h
template<class T>
class A {
public:
static T a;
};
template<class T>
T A<T>::a = 0;
I can now include A.h in multiple .cpp files and also I can assign the value A<int>::a = 100; in one .cpp file and get the same value in another one with A<int>::a.
When does template make such difference?
Is there only 1 copy of this static variable? Which .obj will keep this variable?
Is the constructor called only once? If the initial value is different, which one wins?
When does template make such difference?
Always. I suppose I don't understand the question.
Is there only 1 copy of this static variable?
Only one copy in the final program for each distinct type T the template was instantiated with.
Which .obj will keep this variable?
All of them that were generated from translation units where the template was instantiated. The linker then chooses one and discards all others.
Is the constructor called only once?
Once for each specialization.
If the initial value is different, which one wins?
That would be a violation of the One Definition Rule. Such a program would be ill-formed, no diagnostic required.
Why not define static member in the souce file that implements the class A? Then you should be able to include A.h in multiple source files w/o problem.
// A.h
class A {
public:
static int a;
};
// A.cpp
int A::a = 0;

instantiating a class template and call its methods [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can templates only be implemented in the header file?
When I include MyClass.h and do:
MyClass<int, int> ccc = MyClass<int, int>();
ccc.myMethod1(3, 4);
I get a lot of errors telling undefined reference to constructor and methods ... However when I include MyClass.cpp (which is not a proper why to code) there is no error ! How to fix that ?
I'm compiling under Code::Blocks using g++
The reason is that template classes are not compiled, only instantiated templates will be compiled.
Rule of thumb: don't place template implementations in a cpp file but either directly in the header or another file included by the header (if you want to part implementation from the interface).
E.g:
myclass.h
template<typename A>
class MyClass
{
...
};
#include "myclass.inc"
myclass.inc
//implementation goes here:
....

separating constructor implementation with template from header file [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why should the implementation and the declaration of a template class be in the same header file?
My header file has
template <typename T>
class AA : public BB<T>
{
public:
AA()
{ ... }
this is working fine. But I need to separate the constructor implementation from header file.
So in cpp, I have
template <typename T>
AA<T>::AA()
{ ... }
When I compile this, it compiled but I get unresolved external symbol error. What am I missing here?
You can explicitly instantiate templates in your implementation file using :
template class AA<int>;
this will generate a definition from a template, but it is of use only if you know what types your class clients will use
If you put a template implementation into a .cpp file you need to make sure that it gets instantiated: the compiler won't do it automatically for you. Roughly the same question was answered about a day ago: do template always have to be in the header?