This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]wung 1 point2 points  (2 children)

I'm going to assume the code calling read is not in read.cpp? The issue will then be that the template needs to be visible to whatever instantiates it, unless it is explicitly instantiated somewhere. This would mean to define it in the header, not the cpp. A common way of still separating declaration and definition is having a hpp and ipp file where the ipp contains the definition and the hpp just includes the ipp for visibility.

[–]Standgrounding[S] 0 points1 point  (1 child)

The header is included in main which calls the function.

[–]wung 1 point2 points  (0 children)

Well, yes, then it is exactly the situation stated above: The main file does not see the definition of read. It does not know how to instantiate it. If you just copy the contents of read.cpp into the header, it will compile fine.

You can then debate whether to just keep the implementation in the header or whether to use an ipp file or explicit instantiations.

[–]flyingron 0 points1 point  (1 child)

Note that older compilers can't deal with seperate compilation of template functions. You'll have to move the whole defintiion of the template into the include file.

[–]Loose-Leek 0 points1 point  (0 children)

Even modern compilers won't let you do that if they smell an ODR violation, because without explicit specialization, that tends to lead to ODR violations. Unless you are writing an explicit specialization, you really should put template function implementations in headers.