I have just started to work on a game of rock paper, scissors. I have decided to make a class for a welcome message and i come with the following errors:
Error 6 error C2628: 'Welcome' followed by 'int' is illegal (did you forget a ';'?)
Error 7 error C3874: return type of 'main' should be 'int' instead of 'Welcome'
Error 8 error C2664: 'Welcome::Welcome(const Welcome &)' : cannot convert parameter 1 from 'int' to 'const Welcome &'
#include <iostream>
#include <string>
#include "110ct.h"
usingnamespace std;
class Welcome
{
//Declaration of function
public:
void setWelcomeMessages(string);
void set_Position(int,int);
void display();
}
int main ()
{
string PlayerName;
Welcome A;
//You called those function here
A.setWelcomeMessages("Welcome To Rock, Paper, Scissors");
A.set_Position(0,0);
A.display();
//The rest of your code
}
Those bold lines showed that you declared functions and called them in main()
However, I dont see that you have definition for them.
something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//----------------------------------------------
void Welcome::display()
{
//Code to display
}
//----------------------------------------------
void Welcome::setWelcomeMessages(string)
{
//code to set welcome messages
}
//----------------------------------------------
void Welcome::void set_Position(int,int)
{
//code to set position
}
You need to tell the application how to operate those functions, dont you?
without definition, there is no instruction to the system what to do
int main ()
{
string PlayerName;
Welcome A;
A.setWelcomeMessages ("Welcome To Rock, Paper, Scissors");
A.setPosition (10,0);
A.display ();
cout << "Please Enter Your name" << endl;
cin >> PlayerName;
return 0;
}
Thats my code now, but the damn thing doesnt execute the classl; it executes straight to the cout section :/ as you can see im a complete novice at this, once again i ask of your help
Also when I try adding the colour controller to the void section, the game doesnt execute at all.
Here is an explanation of a class with its code. This Class just builds a ball that you can morph, bounce and roll. I noted it well I hope so you can fallow along and compare it how your class is structured and see how things might work within the class. The reason yours is not working is because you are not passing anything to its members. Just redeclairing the type that should be passed to it.
class Ball
{
/* Things I would use to describe the orange bouncy ball */
string size; // I would tell the balls size. Large, Medium, Small
string shape; // It would be usefull to tell what shape the ball is for idiots. But for fun we can change it to Square if we wanted to.
string color; // I would tell the balls color. Blue, Red, Orange, Black.
//Prototype the members just to tell the compiler that they exist. You define them later.
public:
void Roll_Ball(int); // Passing it an int of how high
void Bounce_Ball(int);
void Change_Size(string);
void Change_Shape(string);
void Change_Color(string);
void Tell_Size();
void Tell_Shape();
void Tell_Color();
Ball() // Here you can set any defaults. This only runs once. Called the Constructor. Notice it has the same name as the Object and returns nothing.
{
size = "Small"; // Our ball is Small
shape = "Round"; // Our ball is Round.
color = "Orange"; // Our ball is Orange.
}
~Ball() // Deconstructor. It destroys the Object and the things that define it.
{
}
};
// Define what we can do with the ball.
/*
So now we are to the Members or Methods in other languages.
Members are used to change values that are protected by there class or used to make the object do somthing.
*/
//What does the ball do? Bounce, Roll?
void Ball::Roll_Ball(int howFar) // How far do i want to roll the ball?
{
cout << "You roll the ball "<< howFar <<" Feet!" << endl;
}
void Ball::Bounce_Ball(int howHigh) // How high do i want to bounce the ball?
{
cout << "You bounce the ball "<< howHigh <<" Feet!" << endl;
}
//Can I get a different ball?
void Ball::Change_Size( string newSize ) // What if I wanted a Large Ball?
{
size = newSize;
}
void Ball::Change_Shape( string newShape ) // What if I wanted a Square Ball?
{
shape = newShape;
}
void Ball::Change_Color( string newColor ) // What if I wanted a Blue Ball?
{
color = newColor;
}
// Maybe I want to know the color, shape , size of the ball
void Ball::Tell_Color() //this is just outputing values the class already knows so i don't need to pass anything in.
{
cout << "The ball is the color: " << color << endl; // Just tells the color of the ball.
}
void Ball::Tell_Size() //this is just outputing values the class already knows so i don't need to pass anything in.
{
cout << "The ball is this size: " << size << endl; // Just tells the size of the ball.
}
void Ball::Tell_Shape() //this is just outputing values the class already knows so i don't need to pass anything in.
{
cout << "The ball is this shape: " << shape << endl; // Just tells the shape of the ball.
}
int main()
{
Ball MyBall; // MyBall by default is orange,small & Round
MyBall.Tell_Color(); // It will tell me my ball is Orange.
MyBall.Tell_Shape(); // It will tell me my ball is Round.
MyBall.Tell_Size(); // It will tell me my ball is Small.
// Im bored so I wanna bounce my ball.
MyBall.Bounce_Ball(12); // It is a super ball bounces 12 feet high.
//I want a blue,square,big ball now.
MyBall.Change_Color("Blue");
MyBall.Change_Shape("Square");
MyBall.Change_Size("Large");
MyBall.Tell_Color(); // It will tell me my ball is Blue.
MyBall.Tell_Shape(); // It will tell me my ball is Square.
MyBall.Tell_Size(); // It will tell me my ball is Large.
// lets Roll my Square Ball.
MyBall.Roll_Ball(1); // Its square, so it don't roll very far. Just one foot.
//I want another ball. But don't want to get rid of my Blue,Big,Square one.
Ball MyOtherBall; // Now I have another ball that is now round,orange, and small again.
MyBall.Tell_Color(); // It will tell me my ball is Blue.
MyOtherBall.Tell_Color(); // It will tell me my other ball is orange.
MyBall.Tell_Shape(); // It will tell me my ball is Square.
MyOtherBall.Tell_Shape(); // It will tell me my other ball is round.
MyBall.Tell_Size(); // It will tell me my ball is Large.
MyOtherBall.Tell_Size(); // It will tell me my other ball is Small.
}
/*********************************************
* Use the [Code] tag please !!
* It is the <> tag on the right hand
* You can see it when you are about to post a reply
* or even a new thread
*********************************************/
#include <iostream>
#include <string>
#include "110ct.h"
usingnamespace std;
class Welcome
{
CursorController crs;
string s;
int xpos, ypos;
public:
//I strongly recommend you to give parameters names at decleration
//Something like this
void setWelcomeMessages(string s);
void setPosition(int a, int b);
void display();
};
void Welcome::setWelcomeMessages(string s)
{
cout << s;
}; //You dont need these after definition of function
void Welcome::setPosition(int,int)
{
//without those parameters' names,
//what is xpos? what is ypos?
//Moreover, what is s?
crs.setPosition(xpos,ypos);
cout << s;
}; //You dont need these after definition of function
void Welcome::display ()
{
//same here
//xpos and ypos dont have any value
//the application has no idea what s is in order to print it.
crs.setPosition(xpos,ypos);
cout << s;
}; //You dont need these after definition of function
int main ()
{
string PlayerName;
Welcome A;
A.setWelcomeMessages ("Welcome To Rock, Paper, Scissors");
A.setPosition (10,0);
A.display ();
cout << "Please Enter Your name" << endl;
cin >> PlayerName;
return 0;
}
Whats heppening is you are not passing any paramaters to your functions. Your just expecting them to know what they are. All your dioing is declairing a type.
Your confusing the prototypes with defining the function.
Example this is your Code:
Line 5 you declare s to hold a string. But where do you ever tell what s is?
Line 12 you are trying to pass a string in but attach it to no variable.
The code below will display s as the one that you pass into the function
Ok so back to my ball class up there ^. You have to tell it what it is before you use it. If you want your function to use your classes variable s you don't need to pass anything into your function. so it would look like this.
But once again it has no idea what s is. You said s is going to hold a string. Nothing else. Look at my ball class carefully and look at the prototypes and the class defenitions. Look where things are set and how they can be set.