0

How can I append functions to classes?

I have a code with two classes, but in my functions in the classes, I use parameters of the type of the other class, like this class Firstclass { Firstclass() { } void printOther(Secondclass message) {} }; As you can see, the printOther function takes a parameter of type Secondclass. However, I define the Secondclass class after the Firstclass class, so I get an error on that function because Secondclass is not defined yet. Are there any prototypes in C++ like in JavaScript? Or really any way to have the functions as part of the class while also being defined?

10th Aug 2018, 1:10 AM
jacksonofgames 28
jacksonofgames 28 - avatar
18 Respuestas
+ 1
I'd put classes in different files and then include one class you need. When you write everything in one file the order of classes matters, but when you nicely separate your code and put it in other files you can just use include and order won't matter
10th Aug 2018, 2:39 AM
Jakub Stasiak
Jakub Stasiak - avatar
+ 1
jacksonofgames 28 , whatever Jakub Stasiak has suggested is best general practice.. it's good way to deal with this type of scenario... If you still want to do in same file, you must go with forward declaration of your second class just above definition of first class. class Secondclass;//this is just declared... you can define after your first class definition or here it self also based on your wish
10th Aug 2018, 3:52 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Any class you can declare as forward declaration... forward declaration is used to let compiler know that it's going to be there at later stage, but you will find for sure.. If you want to use any class within another class before it is defined, just do forward declaration of what is going to get defined later on after usage..
10th Aug 2018, 4:29 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Awesome! Thanks so much for your help you guys
10th Aug 2018, 4:34 AM
jacksonofgames 28
jacksonofgames 28 - avatar
10th Aug 2018, 5:15 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Okay I get two errors using this code: #include <iostream> using namespace std; class Two; class One { public: One() { } void checkConstructor() { Two *dos; cout << dos->num; } }; class Two { public: Two() { cout << "It works"; } int num = 44; }; int main() { One test; test.checkConstructor(); return 0; } I get an error saying "invalid use of incomplete type 'class Two' cout << dos->num; " I also get a note saying "forward declaration of 'class Two'"
14th Aug 2018, 7:28 PM
jacksonofgames 28
jacksonofgames 28 - avatar
0
Thank all of you guys for your feedback, I will probably end up putting them in separate files, but just out of curiosity Ketan, what if I wanted to do the same thing in the Secondclass, with a function that takes a parameter of type Firstclass?
10th Aug 2018, 4:17 AM
jacksonofgames 28
jacksonofgames 28 - avatar
0
you can have forward declaration of both class in beginning and later you define both class to avoid any issue when multiple classes are there in same file..
10th Aug 2018, 4:31 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Hmm... Forward declaration of the classes didn't seem to work Ketan. I am still getting the error "Secondclass has not been defined in void Firstclass::printOther(Secondclass message)", even after these two lines before defining the classes: class Firstclass; class Secondclass;
10th Aug 2018, 3:11 PM
jacksonofgames 28
jacksonofgames 28 - avatar
0
jacksonofgames 28 seems it might be some known issue of sololearn.. I have used the same in past.. please try once on other compiler once
10th Aug 2018, 3:23 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Of course, do you know of any other free c++ compilers?
10th Aug 2018, 4:31 PM
jacksonofgames 28
jacksonofgames 28 - avatar
0
Thanks, been trying to configure Visual Studio Code to use c++, but its proving to be very difficult
11th Aug 2018, 3:56 PM
jacksonofgames 28
jacksonofgames 28 - avatar
0
Well I have configured C++ to run on Visual Studio Code and tried all of the suggestions here. Forward declaration strangely didn't seem to work here either, and putting the classes in different files and including them in the main file resulted in the same error that Secondclass is not a type. I'm out of ideas.
14th Aug 2018, 4:07 AM
jacksonofgames 28
jacksonofgames 28 - avatar
0
whatever compiler I told in previous comment works for me on system (never tried it on app)... I have used forward declaration with that option... and that concept is already used for friend class.. check with other possibilities of to check on compiler if time permits you or go ahead with different classes in different files..
14th Aug 2018, 5:06 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Actually it seems like the easiest way is forward declaration, but it doesn't work because the system needs to know how much space the class will take up, and I don't know how to tell it that ahead of time. Also it does matter what order you include the classes in
14th Aug 2018, 5:07 PM
jacksonofgames 28
jacksonofgames 28 - avatar
0
If you want to pass forwarded class as an argument to function you have to use pointer. So you'd have like: class SecondClass; class FirstClass { FirstClass(); void someMethod(SecondClass *sc); };
14th Aug 2018, 5:24 PM
Jakub Stasiak
Jakub Stasiak - avatar
0
Ah, so then I would access its properties like this, correct? *sc->propertyone *sc->propertytwo
14th Aug 2018, 6:55 PM
jacksonofgames 28
jacksonofgames 28 - avatar
0
Without * just: sc->propOne;
14th Aug 2018, 7:09 PM
Jakub Stasiak
Jakub Stasiak - avatar