Ok, I made this change and it gave me the error that my function from RegistrationForm is private so I made it public.
But then it gave me another error. And this is: 1.error C2440: 'initializing' : cannot convert from 'RegistrationForm *' to 'RegistrationForm'. No constructor could take the source type, or constructor overload resolution was ambiguous
Now my code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
class RegistrationForm
{
public:void setValue (int course, int student)
{
cout << "Course " << course << " has been dropped from student " << student << endl;
}
};
class sample
{
public:staticvoid main()
{
RegistrationForm regForm = new RegistrationForm();
regForm.setValue(102,324);
}
};
Do you want regForm to be an RegistrationForm object or a pointer to a RegistrationForm object? new is used to dynamically allocate an object. It gives you a pointer to the allocated object. You have to use delete on the pointer to free the memory when you no longer need it.
1 2 3
RegistrationForm* regForm = new RegistrationForm();
regForm->setValue(102,324);
delete regForm;
It doesn't look like you actually need dynamic allocations and pointers here so best avoid it. Just create the object and call the function like this:
publicstaticvoid main()?
Are you sure you aren't trying to learn C++ using a Java tutorial? Either that or you have a Java background and are trying to apply the same techniques to C++. That won't work.
Your error comes from this line: RegistrationForm regForm = new RegistrationForm();
In order to use "new" in order to assign memory for the variable, regForm needs to be a pointer, as such: RegistrationForm *regForm = new RegistrationForm();
I think in your case it's much easier to just declare regForm the way you did, but forget about "new" and pointers for now: RegistrationForm regForm;
This way you can use regForm just as you would expect. There's no need to explicitly call new in order to create the object. It already exists.
I also suggest you start working your way through the C++ tutorials on this site.
Well, this is what the book told me, and it's a C++/Java/C# book.
Now, really, I'm not used with Java.
I'm used to C++, and maybe this is why I can't really figure out how it does work.
The only thing that public:staticvoid main() does is declare a member function for the "sample" class called "main" with no return type. Although it may be a little confusing to look at at first, no compiler is going to try to make that the entry point of the application so it's technically not wrong.
Yes, man, another life. I love saying that now it works flawless.
I know quite well C++, I've been using it for a long time making complex mathematics structures, but now I decided to take it to the next level. Through it I won a national contest.
I bought this book called OOP demystified, and I'm at page 20 and it looks "heavy", and sincerely I want to learn c++, because it's simple and straight. Is it a good idea studying this book or should I check the tutorials from this website?