You need to go one step back.
You need first to analyze WHAT you want to do and then think of HOW you could do. Then you would do it (write the code) and then you would test it. In reality there are even more steps.
So let's start with the WHAT. You have some entities that have a relation. So you have
- an "online store"
- "articles" and
- "comments" for the "articles"
That are your entities or objects. These entities have a relation
- The online store has 0 or 1 or many articles
- An existing article can have 0 or 1 or many comments
So, you want to store 0 or 1 or many "somethings". To store such "somethings", C++ offers containers in the STL. For example std::vector or std::list. You need to read about the properties of those containers and select the most fitting for you. In this case it would be most certainly a std::vector. It can contain 0 or more elements and can grow dynamically.
For the relations in C++ and object oriented languages mostly so called "has a" or "is a" properties are used. "is a" relations are modelled with derivations and "has a" types are models via so called containment.
Example:
#include <string>
#include <vector>
// "Is a"-relation. Done by deriving from base class Shape
struct Shape
{
std::string name;
};
// A line is a shape
struct Line : public Shape
{
int length;
};
// "Has a"-relation. Done by containment. Class online Shop contains articles
struct Article
{
std::string articleName;
};
struct OnlineShop
{
std::string shopName;
std::vector<Article> article;
};
int main()
{
Line line{ "long line",12345 };
std::string myShopName{ "My Shop" };
std::vector<Article> myArticles{ Article{ "Apple"}, Article{"Banana"} };
OnlineShop myOnlineShop{ myShopName, myArticles };
return 0;
}
Now you may understand a little better.
So for your solution, after thinking of the entities and the relations, you may come to a possible solution:
- You will create one class OnlineStore
- You will create a class Article
- And a class Comment
Since there are possibly many Articles and many Comments, you will put those in a std::vector. And the class OnlineShop will conatin the class Article which will contain the class Comment.
As a result there will be no circular dependencies. Circular references are in most cases an indicator for a design flaw.
The below shows an skeleton of such implementation:
#include <string>
#include <vector>
struct Comment
{
std::string commentText{};
std::string commentAuthor{};
};
struct Article
{
std::string articleName{};
std::vector<Comment> comment{};
};
struct OnlineShop
{
std::vector<Article> article{};
};
int main()
{
OnlineShop onlineShop{};
return 0;
}
Please define your classes based on this approach. Then add the needed functionality.
Please also read about the principles of object oriented programming.
Hope this helps a little . . .