The standard approach is to separate the implementation from the declarations. This ensures that the class declaration is available to all implementation modules. In a larger program, each logical segment (potentially each class) would be in its one compilation unit. The following is a multi-unit implementation with some comments explaining the finer details.
// -- this be in "client.h"
#include <string>
#include "portfolio.h"
class Client {
public:
void buy(std::string const& name);
bool shouldIBuy();
private:
// since Portfolio is include by value (not a pointer), the
// compiler absolutely requires that it is a "complete type"
// so that it can calculate the correct byte size of a Client
// object
Portfolio port;
};
// -- the following would be in "portfolio.h"
// Client is only referenced in a parameter list so a complete
// type is unnecessary. A forward declaration is the "least
// coupled" solution.
class Client;
class Portfolio {
public:
bool check(Client *client);
};
// -- the following would be in "client.cpp"
#include <iostream>
#include "client.h"
#include "portfolio.h"
void Client::buy(std::string const& name) {
std::cout << "Sending order for " << name << "!\n";
}
bool Client::shouldIBuy() {
return port.check(this);
}
// -- the following would be in "portfolio.cpp"
#include "portfolio.h"
#include "client.h"
bool Portfolio::check(Client *client) {
// we need the complete type of Client at this point
client->buy("AAPL");
return true;
}
// -- the following would be in "main.cpp"
#include "client.h"
int main() {
Client client;
client.shouldIBuy();
return 0;
}