-2

I have some confusion with how I can use definition from one header with other. Let's say I want to use Test type in classA.cpp. Should I import test.h in the classA.cpp file or the header? Seems like forward declaration doesn't work as implemented bellow (I get incomplete type is not allowed). This leaves me concluding I have to import test.h in classA.h. Then in my main.cpp only import classA.h as otherwise I get error that _Test Test is already defined.

test.h

#ifndef test_h
#define test_h

typedef struct _Test
{
    double somevalue;
} Test;

#endif

classA.h

#ifndef classA_h
#define classA_h

typedef struct _Test Test;

class classA{
    public:
        Test qtest;
        void somefct();
};

#endif

classA.cpp

#include "test.h"    
#include "classA.h"

void classA::somefct(){
    qtest.somevalue;
    return;
}
Mooder
  • 27
  • 3
  • 2
    In the header file. Because when you include `test.h` in `classA.h`, and then you include `classA.h` into `classA.cpp`, classA.cpp will have access to all the stuff in `test.h`. – The Coding Fox Feb 28 '22 at 15:12
  • 2
    this looks like you were trying to learn C++ from C material. This is not recommended, because C and C++ are two different languages. Assuming the two are the same or one would be a subset of the other leads to confusion and pain – 463035818_is_not_an_ai Feb 28 '22 at 15:13
  • 2
    what i mean is `typedef struct _Test Test;`. In C++ you would write a forward declaration as `struct _Test;` no need to `typedef` a type name. Though identifiers with leading `_` followed by a capital letter are reserved. Technically speaking your code has undefined behavior. – 463035818_is_not_an_ai Feb 28 '22 at 15:14
  • Maybe also look at [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – Wyck Feb 28 '22 at 15:15
  • Just replace `typedef struct _Test Test;` in classA.h with `#include "test.h"` . There is no need for forward decls in this code. Ideally you also jettison the typedef alias in `test.h` and just use `class Test { ... etc. };` – WhozCraig Feb 28 '22 at 15:20
  • *as otherwise I get error that `_Test Test` is already defined* – this happens only if you don't include the headers. If you do so it is prevented by the `#ifdef #define #endif``, which is called an include guard – even if you include the header in main.cpp again... – Aconcagua Feb 28 '22 at 15:26
  • Side note: You should *always* include all the headers you use in a source directly, assume you need ``, and you get it included from ``, thus you don't include it on your own – however, a later version of the latter drops ``, so now your own code breaks as soon as you update the demo library – it won't, if you include `` on your own instead – right from the start. In your case: If you use `Test` within main.cpp, include the header there even if it is included already indirectly via `classA.h`. – Aconcagua Feb 28 '22 at 15:26

1 Answers1

0

Should I import test.h in the classA.cpp file or the header?

You should include the definition of _Test in the file classA.h, because classA.h relies on _Test being defined.

This leaves me concluding I have to import test.h in classA.h.

Your conclusion is correct.

Then in my main.cpp only import classA.h as otherwise I get error that _Test Test is already defined.

You're attempting to treat the symptom rather than its cause. The cause of the issue is that you have two definitions for Test in separate headers. The proper solution is to not have two definitions. Simply remove the definition of Test in classA.h and instead include its definition.


_Test name is reserved to the language definition. By definiint it, the behaviour of your program will be undefined. Solution: Don't use _Test as name of a struct.

Also, the typedef is redundant in C++. You could just name the struct as Test.

eerorika
  • 232,697
  • 12
  • 197
  • 326