Jump to content

Noob seeking a troubleshoot?

Rossyboy

hi all, 

 

Im sorry to have to make this post and bother everyone but I've really exhausted myself trying to find the solution here with no avail.

 

Basically, I'm new to C++ and I'm trying to wrap my head around this "classes" idea. I made a silly little console window program just to try and teach myself it but every time I compile I get this error

 

error C2228: left of '.ratedance' must have class/struct/union

 

heres my program

#include "stdafx.h"#include <iostream>#include <string>using namespace std;class Josh{	Josh (int assiage, int assistyle):	age(assiage),	style(assistyle)	{	}public:		void ratedance();private:	int age;	int style;};int main(){	int ch1, ch2;	cout << "what is his age?" << endl;	cin >> ch1;	cout << "what is his style rating?" << endl;	cin >> ch2;	Josh dude(int ch1, int ch2);	dude.ratedance();	char f;	cin >> f;	return 0;}void Josh::ratedance(){	if (age < 18 && style > 5)	{		cout << "this josh has excellent dance moves" << endl;	}	if (age > 18 && style < 5)	{		cout << "meh" << endl;	}	else 	{	}}

 

I know there's just some silly little detail I'm missing but I can't seem to see it. 

 

Anyway I hope one of you wonderful people can help!

 

Thanks

Rossyboy

Link to comment
Share on other sites

Link to post
Share on other sites

Your link isn't working, but it looks like you've just not properly instantiated a variable...

Link to comment
Share on other sites

Link to post
Share on other sites

You already declared ch1 and ch2 as int. You don't put a type when calling a method or constructor in this case at least.

Josh dude(int ch1, int ch2);
Should be:

Josh dude(ch1, ch2);

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

Your link isn't working, but it looks like you've just not properly instantiated a variable...

Thanks for the reply. Sorted the link.

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure if you've thought of it or not, but do you need something to happen if age == 18 and style == 5? (or age > 18 && style < 5 and age < 18 && style > 5)

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

Rossyboy I think by correcting the int problem that squirrl mentioned should fix your problem.

 

The reason being that the issue Squirrl brings up makes dude an incorrect class, and then calling a method from Josh using dude means it can't properly detect the correct class.

 

With that said if correcting that issue doesn't work, try implementing the ratedance method prior to implementation of the main class (It shouldn't matter, but who knows...you usually don't define classes in cpp files, usually it is in header files and implementation goes in cpp files)

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for the help guys! I've made some simplifications to the code. I know that if I was doing this all properly the definitions ect would be in different files but this is only for experimental purposes.

 

Heres what Ive got now.

#include "stdafx.h"#include <iostream>#include <string>using namespace std;class Josh{	Josh (int assiage,  int assistyle):	age(assiage),	style(assistyle)	{		}public:		void ratedance()	{		if (age < 18 && style > 5)		{		cout << "this josh has excellent dance moves" << endl;		}		if (age > 18 && style < 5)		{		cout << "meh" << endl;		}	}	private:	int age;	int style;};int main(){	int ch1, ch2;	cout << "what is his age?" << endl;	cin >> ch1;	cout << "what is his style rating?" << endl;	cin >> ch2;	Josh dude(ch1,ch2);	dude.ratedance();	char f;	cin >> f;	return 0;} 

Unfortunately I'm getting a new error (error C2248: 'Josh::Josh' : cannot access private member declared in class 'Josh') Any ideas?

 

 

THANK YOU EVERYONE

Link to comment
Share on other sites

Link to post
Share on other sites

That is because you have made your constructor private. The default access level is private when undefined. Move your Josh constructor to the public block.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Also considering your architecture, a couple of things came to mind; Isn't Josh a name for a Dude? What I'm trying to say is, wouldn't it make more sense if your class was called Dude and you instantiated against a variable named Josh. Or better yet you gave the Dude class another field called name which would be of the type string into which you passed "Josh" and instantiated against a person/dancer/contestant variable instead. The other thing is that you should really be declaring a class separately, granted you're learning but it's good to start with best practices; i.e. a header (.h) containing the declarations and an implementation (.cpp) containing just that, the implementation.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Also considering your architecture, a couple of things came to mind; Isn't Josh a name for a Dude? What I'm trying to say is, wouldn't it make more sense if your class was called Dude and you instantiated against a variable named Josh. Or better yet you gave the Dude class another field called name which would be of the type string into which you passed "Josh" and instantiated against a person/dancer/contestant variable instead. The other thing is that you should really be declaring a class separately, granted you're learning but it's good to start with best practices; i.e. a header (.h) containing the declarations and an implementation (.cpp) containing just that, the implementation.

I know all these things. It was only a quick program, but thanks for your help :)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×