Regarding C++ Include Another Class

Regarding C++ Include another class

What is the basic problem in your code?

Your code needs to be separated out in to interfaces(.h) and Implementations(.cpp).

The compiler needs to see the composition of a type when you write something like

ClassTwo obj;

This is because the compiler needs to reserve enough memory for object of type ClassTwo to do so it needs to see the definition of ClassTwo. The most common way to do this in C++ is to split your code in to header files and source files.

The class definitions go in the header file while the implementation of the class goes in to source files. This way one can easily include header files in to other source files which need to see the definition of class who's object they create.

Why can't I simply put all code in cpp files and include them in other files?

You cannot simple put all the code in source file and then include that source file in other files.C++ standard mandates that you can declare a entity as many times as you need but you can define it only once(One Definition Rule(ODR)). Including the source file would violate the ODR because a copy of the entity is created in every translation unit where the file is included.

How to solve this particular problem?

Your code should be organized as follows:

//File1.h

Define ClassOne 

//File2.h

#include <iostream>
#include <string>

class ClassTwo
{
private:
string myType;
public:
void setType(string);
std::string getType();
};

//File1.cpp

#include"File1.h"

Implementation of ClassOne

//File2.cpp

#include"File2.h"

void ClassTwo::setType(std::string sType)
{
myType = sType;
}

void ClassTwo::getType(float fVal)
{
return myType;
}

//main.cpp

#include <iostream>
#include <string>
#include "file1.h"
#include "file2.h"
using namespace std;

int main()
{

ClassOne cone;
ClassTwo ctwo;

//some codes
}

Is there any alternative means rather than including header files?

If your code only needs to create pointers and not actual objects you might as well use Forward Declarations but note that using forward declarations adds some restrictions on how that type can be used because compiler sees that type as an Incomplete type.

Including a class object inside another class c++

In methods definition like in

Car::Car(int forR, int revR, int 
forL, int revL)
{
rMotor.init(int forR, int revR);
lMotor.init(int forL, int revL);
};

you are calling init, not defining it. So it is wrong specifying the type of the parameters (int) and it results in a set of compilation errors.

Just call the methods in this way, just passing to motor::init the variables coming from Cars constructor:

Car::Car(int forR, int revR, int 
forL, int revL)
{
rMotor.init(forR, revR);
lMotor.init(forL, revL);
};

How can I use a value from another class in c++?

Either x is a static variable (also known as a global variable), and in this case, this should be:

class MainInt
{
public:
MainInt();
static int x;
};

// in cpp:
int MainInt::x = 1;

or it's a traditional variable, as it it feels like from the constructor. In that case, you need to instantiate an object:

MainInt variable;
cout << variable.x << endl;

How do I reference another class into another class in C++?

You're looking for dependency injection:

class foo
{
public:
int variable1 = 012;
};

class bar
{
foo _foo;
public:
bar(foo& fooInjected) :
_foo(fooInjected)
{}
int getFooVariable() { return _foo.variable1; }
};

There is an idea of inversion of control: bar has no control over the creation of foo instance, which is created outside. It's a common way to inject for instance a service. It's also a very important way to loosely couple classes working together and to mock and to test them. But here foo is a concrete class, instead and ideally you're refering only an interface (c#) or an abstract class (c++) in bar. Which concrete class is behind is out of control of bar. I recommend M. Seeman's book Dependency Injection in .NET to understand this completely.

or inheritance:

class foo
{
public:
int variable1 = 012;
};

class bar: public foo
{
public:
int getFooVariable() { return variable1; }
};

Here, bar builds up heavily on foo. bar is a "richer version" of foo if this makes sense. This is what to choose if there is a high cohesion inside bar.

Invoking a variable through another's instance function is a most horrid idea, that goes against class cohesion:

int getFooVariable(foo* foo) { return foo->variable1; }

How to include a class as a member of another class in c++

There is no std:: namespace specifier for string in your Person.h. You should specialize std::string:

class Person
{
public:
Person(std::string n, Birthday b);

Include a class as member variable in another class (C++)

Compiler needs to see the definition of class Player in header file (BST.h) which you have provided in BST.cpp ( by including "Player.h").

So, BST header should be:-

#include "Player.h"         <<<include this file

class Player; <<<remove this forward declaration
class BinarySearchTree
{
private:

struct Node {
Player info;


Related Topics



Leave a reply



Submit