1

I have two classes in two different header files. I, as advised in another topic with a similar question, declared class A before class B and declared class B before class A. But it did not help. Seller still can't see the Organization

Seller.h

#ifndef OOP_3_SELLER_H
#define OOP_3_SELLER_H
#include "Organization.h"
class Organization;
class Seller{
protected:
Organization*owner;
...
};

Organiztion.h

#ifndef ORGANIZATION_OOP3_H
#define ORGANIZATION_OOP3_H
#include "Seller.h"
class Seller;
class Organization{
 std::vector<Seller*> own;
...
};

The compiler tells me the following error: error C2027: use of undefined type 'Organization'. That is, as I understand it, the Organization sees the Seller, but the Seller does not see the Srganization

MRV
  • 23
  • 3

1 Answers1

1

Since you've forward declared the class Organization in Seller.h there is no need to write #include "Organisation.h". Similarly, in Organization.h" since you've forward declared class Seller there is no need to write #include "Seller.h". Also, always take into account cyclic dependency like in your program Organization.h has a #include "Seller.h" and then Seller.h has a #include "Organization.h"

The running(successfully compiled) program can be seen here.

Organization.h

#ifndef ORGANIZATION_OOP3_H
#define ORGANIZATION_OOP3_H
//#include "Seller.h"
#include <vector>
class Seller;
class Organization{
 std::vector<Seller*> own;

};
#endif

Seller.h

#ifndef OOP_3_SELLER_H
#define OOP_3_SELLER_H
//#include "Organization.h"
class Organization;
class Seller{
protected:
Organization*owner;

};
#endif

In the .cpp files(also called source files) you should include the headers using #include.

Your program has other problems too like many(and i mean many many)of the methods in Seller.h have no return value for methods that have non-void return type.

I tried solving some of the problems in you github code but there are just too many problems(errors and warnings) to solve. Also i would suggest you to use cpp files as well.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • I don’t quite understand what you meant about cpp files, but I don’t use them. Removed the includes but it didn't help. Also you can see all code in github.com/sabudilovskiy/OOP3 – MRV Nov 18 '21 at 09:24
  • @MRV If you're going to be serious about C++, then you better start using cpp files corresponding to the header files. Although in simple/small projects you can skip their use but when the project gets large/bigger it is better to use them. Now you said that even after removing the `#include`s you still get the error. So this mean you must have some other things/code in your header files that you didn't give/wrote in your original code snippets. Can you paste the exact code of the header files that you have right now so that i/we can narrow down the problem? Unless you're developing headeronly – Jason Nov 18 '21 at 09:28
  • The problem is that I work in СLion and they use СMake there. For some reason I do not understand, it works very badly when used with cpp along with class templates. If you add them to the executable files (as Clion does by default) and, as usual, everyone does just include headers, then it will not compile. I solved the problem by including the cpp files, but this did not help for long, and the whole meaning of the separate files is generally lost. Now I will try to split the code. – MRV Nov 18 '21 at 09:36