2

I'm trying to implement a class in C++ which contains most of the functions which I might need to use across different class hierarchies (the project has multiple different inheritance trees).

After reading through and taking advise from multiple answers for this kind of implementation on Stack overflow, Why can templates only be implemented in the header file? I decided to implement this using a .h file and 2 different .cpp files. I tried to implement a small test case using this FAQ as a guideline. The code is as below:

test.h

#ifndef TEST_H
#define TEST_H

#include <iostream>
#include <cmath>
#include <complex>

template<typename T> 
class test{
public:
static bool IsClose(const T &a, const T &b);
};
#endif

testImpl.h

#include "test.h"

template <typename T> 
bool test<T>::IsClose(const T &a, const T &b){
    return (std::abs(a-b) <= (1e-8 + 1e-5 * std::abs(b)));
}

testImpl.cpp

#include "testImpl.h"

template class test<int>;
template class test<double>;

main.cpp

#include "test.h"
#include <iomanip>
int main(){
    std::cout << std::boolalpha << test<double>::IsClose(1e-7,1.1e-7) << std::endl;
    return 0;
}

On compiling using g++ -o test main.cpp testImpl.cpp I'm getting the following error:

main.cpp: In function ‘int main()’:
main.cpp:4:36: error: ‘test’ is not a template
     std::cout << std::boolalpha << test<double>::IsClose(1e-7,1.1e-7) << std::endl;

If anyone can advise me on where I'm going wrong your help will be much appreciated. Thanks in advance!! Also if there is a better method to achieve what I'm trying to do, your thoughts are welcome on that matter as well.

  • put all of your template code in headers. Just the explicit template specialization implementations should be in cpp – bolov Dec 23 '20 at 02:39
  • @bolov @Elliot I changed the name of ```test.cpp``` to ```testImpl.h``` but still have the same error. – Dewan Arun Singh Dec 23 '20 at 02:49
  • Does it compile if you place all the source in a single file? Which g++ version are you using? Also, like @Elliott suggested, try renaming __TEST_H__ not to use leading or trailing underscores. – Eugene Dec 23 '20 at 02:56
  • @Eugene it compiles when I put everything in a single source file. I'm using g++ version 7.5.0 and I tried removing the leading and trailing underscores but to no effect. Also I tried this code as template functions instead of template class and it compiled fine. – Dewan Arun Singh Dec 23 '20 at 03:02
  • @Elliot I changed that too in the code. Removed the leading and trailing underscores.... But sadly I still have the same error. – Dewan Arun Singh Dec 23 '20 at 03:30
  • I couldn't replicate your problem. What's your cpu and OS? – Elliott Dec 23 '20 at 03:30
  • 1
    @Elliot Ubuntu 18.04 and CPU is a i7 8th generation. – Dewan Arun Singh Dec 23 '20 at 03:36
  • @DewanArunSingh, weird. I'm using ubuntu 20.04 and and i7 8th gen also (i7-8550U). – Elliott Dec 23 '20 at 03:39
  • What does `gcc -v` give you? – Elliott Dec 23 '20 at 03:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226338/discussion-between-dewan-arun-singh-and-elliott). – Dewan Arun Singh Dec 23 '20 at 03:49

1 Answers1

0

I figured it out finally. I needed to let the compiler know that it needs to use a more recent version of C++. So, g++ -std=c++17 -o test main.cpp testImpl.cpp compiled the code for me perfectly fine.

The need for using -std flag arises only when you're using the gcc version 7.5.0. All versions of gcc after it, work with the g++ -o test main.cpp testImpl.cpp.

  • This helps to solve the problem, but doesn't explain why it wasn't compiling in the first place. That's still a mystery (possibly a bug in GCC 7.5.0, known or unknown). Also, you're welcome, but there's no need to thank anyone in your answer. Stackoverflow is supposed to be very blunt. =P – Elliott Dec 23 '20 at 04:04
  • 1
    @Elliot it seems you were right, THIS is a bug in gcc 7.5.0 I tried the same code with gcc 10.1.0 and it worked without the -std flag. – Dewan Arun Singh Dec 23 '20 at 04:45