0

Apologies if you have seen this question before however it has yet to be answered, essentially in my code I have two structs, defined in separate headers and used globally throughout the project. I simply wish to use both structs (which again, are defined in two separate headers) in other cpp files than just the ones that the header file belongs to. Here is some sample code which I have tested:

class1.h

    #include "class2.h"
    #include <vector>
    #include <string>

    struct trans1{
        string name;
    };
    class class1 {

    private:
        vector <trans2> t2;

    public:
        class1();
    };

class2.h

    #include "class1.h"
    #include <vector>
    #include <string>        

    struct trans2{
        string type;
    };

    class class2{

    private:
        vector <trans1> t1;

    public:
        class2();
    };

errorlog:

    In file included from class1.h:3:0,
                     from class1.cpp:1:
    class2.h:21:13: error: 'trans1' was not declared in this scope
         vector <trans1> t1;
                 ^
    class2.h:21:19: error: template argument 1 is invalid
         vector <trans1> t1;
                       ^
    class2.h:21:19: error: template argument 2 is invalid

I understand that this is ridiculous code in a real world application however this is the simplest way I could demonstrate.

It is worth noting that if I simply comment out the declaration of vector t1 or t2 under 'private:' the code compiles without fail. It is just the fact I am using a second struct.

Any help anyone? Thanks.

2
  • 1
    Can you put the "trans" structs in their own header file? Commented Mar 22, 2015 at 19:28
  • possible duplicate of Using a struct across classes in c++ Commented Mar 22, 2015 at 19:48

4 Answers 4

1

Simply forward-declare the classes that will be used. Put all implementation code into a cpp file, not inline in the header.

Make the vector private. This way no file that includes the header can force code generation against an incomplete class.

Sign up to request clarification or add additional context in comments.

4 Comments

The vectors require a full declaration of the structs. This won't work unless the vectors are changed to store pointers.
I can confirm that this technique does actually work even though the class is not fully defined at the point of the declaration of the vector. tutorialspoint.com/…
As I understand it there is (or was) some disagreement in the standards committee about whether it should be allowed. The arguing for allowing it, surprisingly, makes vector easier to implement efficiently. I think as things stand, what I have done should not work according to a strict standards interpretation, but in fact always does. The things you learn eh? :)
0

you can try to forward declare trans1 in class2.h and trans2 in class1.h like this:

class2.h :

// includes
struct trans1;
// rest of your code

the same thing (but with trans2) in class1.h

Don't forget to add Include guards in your code!

  • edit: and yes, you need to change your vectors to store pointers, otherwise it won't link

1 Comment

I think this is worse because now you have to worry about how to allocate and delete the vector objects.
0

You need to put the "trans" structs in their own header file(s) and include them in your class header files.

You could forward declare them, but this would require changing your vector to use pointers. (In that case I would recommend std::vector<std::unique_ptr<trans>>). This could be appropriate if your structs are big and complex.

The main advantage of the forward-declaration approach is to reduce compile times. However if the structs are really so simple as in your example, I wouldn't bother with the extra overhead of using pointers here.

Comments

0

If You were to do this in single .cpp file, the solution would be trivial:

   struct trans1 { ... };
   struct trans2 { ... };
   class class1 { ... };
   class class2 { .... };

Now you just need to rearrange the code to get this result in every translation unit. (the order of classes/structs in the file is important)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.