Class Problem

Hi,

I've just started learning object programming in c++ and I have a problem with my code.

It gives me these errors:

1.error C2065: 'RegistrationForm' : undeclared identifier
2.error C2146: syntax error : missing ';' before identifier 'regForm'
3.error C2065: 'regForm' : undeclared identifier
4.error C2061: syntax error : identifier 'RegistrationForm'
5.error C2065: 'regForm' : undeclared identifier
6.error C2228: left of '.setValue' must have class/struct/union
7. type is ''unknown-type''


My code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

class sample
{
public:static void main()
	   {
    RegistrationForm regForm = new RegistrationForm();
	regForm.setValue(102,324);
	   }
};

class RegistrationForm
{
    void setValue (int course, int student)
	{
		cout << "Course " << course << " has been dropped from student " << student << endl;
	}
};


Can you help me? I'm standing in front of this situation and for hours I couldn't find an answer.

Thank you,
RobertEagle
Move your definition for the "RegistrationForm" class above your definition for the "sample" class. This should fix the issue.
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>
using namespace std;

class RegistrationForm
{
public:void setValue (int course, int student)
	{
		cout << "Course " << course << " has been dropped from student " << student << endl;
	}
};

class sample
{
public:static void main()
	   {
    RegistrationForm regForm = new RegistrationForm();
	regForm.setValue(102,324);
	   }
};
Looks like you are used to Java.

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:
1
2
RegistrationForm regForm;
regForm.setValue(102,324);



The order in which you declare/define things matter. That's why you should define RegistrationForm before sample.

In C++ the main function should be a free function (not a class member) and have return type int.
Last edited on
public static void 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.

How would it look like in C++?
1
2
3
4
5
int main()
{
RegistrationForm reg;
reg.setValue(0,0);
}


EDIT: Don't stick it inside a class. I suggest creating a main.cpp file for your main function.
Last edited on
The only thing that public:static void 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?
True, but I doubt this is what the OP wanted.
Thank you very much for your help.
Topic archived. No new replies allowed.