about Separate Compilation, how?

I have a header file, header.h

1
2
3
4
5
6
7
8
9
//header.h
#ifndef HEADER_H
#define HEADER_H

template <class T>
void nice(T a)
{std::cout << a << std::endl;}

#endif 


and a source file B
1
2
3
//fileB.cpp
#include "header.h"
template void nice<int>(int);//and here generate a instantiation. 


and a source file A
1
2
3
4
5
6
7
//fileA.cpp
#include <iostream>
//and here I don't include header.h
int main()
{
nice(5);//error
}


now that i made the compiler to generate a instantiation in fileB.cpp, why fileA.cpp cannot recognize the nice() template function instantiation?
Your A.cpp doesn't know anything about any template function "nice"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//fileA.cpp
#include <iostream>
//and here I don't include header.h <-- why?
// if you just include it here:
#include "header.h" // Without this A.cpp doesn't know that there is a function nice
// and certainly doesn't know anything about whatever is going on in B.cpp

// Or you just include B.cpp
#include "B.cpp"  // I don't know why you would include the cpp instead of the h 
// however it's possible 

int main()
{
nice(5);//error
}


Summary: you have to include either header.h or B.cpp
Last edited on
I guess I know it. thanks
Topic archived. No new replies allowed.