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?
18 ответов
+ 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
+ 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
+ 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..
+ 1
Awesome! Thanks so much for your help you guys
+ 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'"
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?
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..
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;
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
0
Of course, do you know of any other free c++ compilers?
0
Thanks, been trying to configure Visual Studio Code to use c++, but its proving to be very difficult
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.
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..
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
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);
};
0
Ah, so then I would access its properties like this, correct?
*sc->propertyone
*sc->propertytwo
0
Without * just:
sc->propOne;