Reputation: 306
Heyo, I have a question about this issue I am having in C++. I am new to C++ and learning. I have experience in object oriented programming so I am looking for the correct way and solve this issue and why it should be done that way.
I have separated each class into an interface header file and implementation cpp file.
The first class "Alpha" is my base class:
// a.h
#pragma once
class Alpha
{
public:
virtual void myFunction();
};
Alpha Implementation:
// a.cpp
#include <iostream>
#include "a.h"
void Alpha::myFunction()
{
std::cout << "Class Alpha myFunction" << std::endl;
}
I then have a sub class "Beta":
// b.h
#pragma once
#include "a.h"
#include "a.cpp"
class Beta : public Alpha
{
public:
virtual void myFunction();
};
Beta Implementation:
// b.cpp
#include <iostream>
#include "b.h"
void Beta::myFunction()
{
std::cout << "Class Beta myFunction" << std::endl;
}
I then have what will be like a manager class called "Gamma".
// g.h
#pragma once
#include <vector>
#include "a.h"
#include "a.cpp"
class Gamma
{
public:
void run();
std::vector<std::shared_ptr<Alpha>> alphaCollection;
};
Gamma implementation:
// g.cpp
#include "g.h"
#include "b.h"
#include "b.cpp"
void Gamma::run()
{
Beta localBeta;
alphaCollection.push_back(std::make_shared<Beta>(localBeta));
// example usage
alphaCollection.at(0)->myFunction();
}
Then finally my main:
#include "g.h";
#include "g.cpp";
int main()
{
Gamma objectG;
objectG.run();
}
The purpose for all of this being that I want a vector of the base class Alpha in my manager where I can then insert elements of a varying number of base class objects.
I used Beta as an example of a derived class but in the real implementation would have more derived classes such as Charlie, Delta etc.
The goal being that my Manager class Gamma will be able to operate on the vector elements as Alpha objects and each of the sub classes perform their own behavior.
I cannot compile the above example because of how Gamma's implementation file includes "g.h" and "b.h" each of which include "a.h" and "a.cpp" thus double including "a.cpp" since it has no header guard.
I have read varying opinions on how to use includes and overall I just feel like a noob in understanding the proper way to organize code to prevent his.
Seriously any guidance is appreciated!
Upvotes: 0
Views: 532
Reputation: 1691
Stop #including .cpp files. The .h is the most you'll ever need, and sometimes just a forward declaration is enough. Here, here, and here are decent descriptions of what purposes they serve.
Upvotes: 0
Reputation: 14039
Do not #include
the cpp files. It's not required and that's what is causing the issue.
#pragma once
is the same as an include guard - but remember it's not portable - it's specific to your current compiler.
You should not have include guards in your cpp files because you shouldn't #include
them.
Upvotes: 1
Reputation: 5128
You only need to include header files, not .cpp files.
You do not want to use header guards in .cpp files.
You don't need forward declaration here (although you could use it instead)
Your implementations are not including "sub class includes", they are including the header files that define their interface. Your include of a.h is correct in g.h.
Upvotes: 0