1

I have been studying C++ for some time (not so long) and now I'm faced with a problem:

#ifndef _FILE_A_H
#define _FILE_A_H

template <typename T>
class A {
    void func();
    /// ... some code here
};

#include "a.cpp"

#endif

I want to place implementation of A-class in file 'a.cpp'. But to do that I need to include 'a.h'. Is it normal to cross-including files in that situation?

I have something like this in 'a.cpp' (it's compiling but looks embarrasing):

#ifndef _FILE_A_CPP
#define _FILE_A_CPP

#include "a.h"

template <typename T>
void A<T>::func() {
    /// ... some code here
}

/// ... and some code here

#endif
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
RoshT54
  • 19
  • 3
  • 4
    You don't need to include `a.cpp` in the header file. It should compile well without it, and should cause double-definition errors when you do ! – Magix May 07 '18 at 10:27
  • 2
    Unrelated to your question, but all symbols starting with an underscore followed by an upper-case letter (like `_FILE_A_H`) are *reserved*. See e.g. [this question and answer](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for details. – Some programmer dude May 07 '18 at 10:28
  • 2
    @Magix note this is a template class.. https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file#495056 – stijn May 07 '18 at 10:30
  • `#include <*.cpp>` - always an error. – SergeyA May 07 '18 at 10:31
  • @stijn All right, then why should you need to include `a.h` in the cpp file ? compiling any other file which includes `a.h` should work, but you shouldn't be compiling `a.cpp` on its own (that's why it is supposed to be a `.tpp` file ! ) – Magix May 07 '18 at 10:33
  • I'm not saying you should need to include a.h, I was adressing the comment that including a.cpp should lead to double definition errors, by showing the typcial way of doing that – stijn May 07 '18 at 10:34
  • @Magix if file `a.cpp` is not included i get a link error (but no need to use `#ifndef` in cpp-file) – RoshT54 May 07 '18 at 10:37
  • How and what do you compile ? – Magix May 07 '18 at 10:38
  • @Magix just for example i have 3 files: `main.cpp`, `a.cpp` and `a.h`; compile it with command `g++ *.cpp -o main`.and that works. File `main.cpp` just include `a.h` and call function `func` from A-class (i make it public). – RoshT54 May 07 '18 at 10:45
  • 5
    It's far more normal to keep the template implementation entirely in the .h file so that you don't have to struggle with this conundrum. – acraig5075 May 07 '18 at 10:45
  • `a.cpp` is supposed to be a `.tpp` file. You should not compile it. Try `g++ main.cpp -o main` – Magix May 07 '18 at 10:47
  • @Magix thanks! Now it looks better (i really don't know about that solution) – RoshT54 May 07 '18 at 10:52
  • @Magix - The exact extension is not that important, it is just a convention. See [.c vs .cc vs .cpp vs.hpp vs .h vs .cxx](https://stackoverflow.com/questions/5171502/c-vs-cc-vs-cpp-vs-hpp-vs-h-vs-cxx) – Bo Persson May 07 '18 at 10:54
  • 2
    @BoPersson I know, but the extension should be changed in order to be excluded in the `*.cpp` glob when compiling – Magix May 07 '18 at 10:57

1 Answers1

1

Thanks @Magix for answer. Now my a.cpp changed to a.tpp and looks something like this:

#include <iostream>

template <typename T>
void A<T>::func() {
    /// ... some code here
}

/// ... and some code here
RoshT54
  • 19
  • 3