You'll have to split up declarations and definitions. That or use inline functions. Variables generally go in a private or protected section, and getter/setter functions are used to modify them.
Declarations of functions for the person class go in a header person.h:
#include "person.h"
person::person()
{
text = "This is set in person's constructor.";
}
void person::print()
{
std::cout << text << std::endl;
}
void person::setText(std::string newText)
{
text = newText;
}
And finally it comes together in main.cpp;
1 2 3 4 5 6 7 8 9
#include "person.h"
int main()
{
person bob;
bob.print();
bob.setText("Hello world!");
bob.print();
}
So you have a header and a source file for each class. If either class is part of the other you include the header of that class in the source file for the other class.
Or have a single header and source file for both classes.
Can you provide an example? I want to separate the class Node & class Stack by creating 2 header, is it possible? I tried to do it but it gives me an error.
The errors has been reduced, but I still have errors it says that class Stack has no member name push/pop/display. And 'top' was not declared in this scope.
In your stack.cpp file, you need to tell the compiler that the functions are owned by the stack class.
A function definition for the stack class should look like this: