I'm try to use inheritance here where the "started" class is the parent and as I made the "BeginJourney" the child things haven't been working. It says that the "started" base class is undefined.
#pragma once
class BeginJourney :public started
{
public:
//BeginJourney();
void Cell(int BreakOut);
private:
};
BeginJourney.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<iostream>
#include <iomanip>
#include "BeginJourney.h"
usingnamespace std;
void BeginJourney::Cell(int BreakOut)
{
cout << "You wake up in a cell with a dilapitated wall behind you, a window one may sqeeze threw above" << endl;
cout<< "and a sleeping guard outside the cell." << endl;
cout << endl;
cout << setfill('-') <<setw(65) << "What will you do?"<<setw(55)<<"-"<<setfill('-') << endl;
cout << "1. Crush the ruined wall behind you / 2.Jump and climb to reach the window / 3.Hypnotise the guard"<< endl;
cin >> BreakOut;
}
Okay so I added a header file for started (called Start now) and a separate cpp for it. I also put #include "Start.h" into the BeginJourney header file.
Start.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#pragma once
#include <vector>
class Start
{
public:
void startingclass();
void getclass();
protected:
int starting_class, intelligence, strength, agility;
string attributes[3] = { "intelligence","strength","agility" };
vector<int> attribute_points;
private:
char redo;
};
Please post the error messages exactly as they appear in your development environment. These messages have important information embedded within them to aid in locating and fixing the issues.
By the way in your Start.h header file you will need to properly scope the classes from the std namespace. You also need to specify all the proper #includes for the standard classes you are trying to use.
You might consider a re-think of this project. Keep what you have but re-shuffle/re-organize it before you go too far descending into a very complicated structure.
1. Your classes are simpler if they are separate from the interface. Menus within classes are not a good idea - detract from what the class is really about, suit only 1 platform, add complications and make editing difficult.
2. It appears you have a Game class and 3 Players - Maje, Ninja and Barbarian - inheriting their unique properties from the Player class.
Okay I decided to start from scratch because going through all the code was maddening. I''ll try my best with the factory patterns.
That being said while slowly remaking this I think I found one of the problems from my previous attempt. It all comes from the "Start" header file line 10.
here's what the errors say:
'stats':unknown override specifier
syntax error: missing ',' before '['
unexpected token(s) preceding '{'; skipping apparent body function
syntax error: missing ')' before ';'
syntax error:')'
unexpected token(s) preceding ';'
Here's the code:
source.cpp
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include "Start.h"
usingnamespace std;
int main()
{
int choice = NULL;
Start start;
start.starting_class(choice);
}
First it is a very bad practice to use the "using" statements in the global scope of a header. You should properly scope the standard classes using the scope resolution operator:: (std::string, std::vector).
Second you need to include all the required header files. You need to #include the headers in all files that try to use the items.
Third your compiler error messages should be telling you exactly where it detects the problems, you left out that important information from your snippet error reports.
I've refactored the geeks factory and this might give you a better picture of what I'm driving at.
1. Your naming of classes could be more self-explanatory.
2. A Game class comes later where the setup occurs.
3. Note that the Players only return strings etc - ie you can use a Player class on any platform, not just the console
4. Obviously each of the Player classes need to be update/modified to incorporate their properties and methods as required - including inheritance where appropriate.
Not sure if this thread is dead or not, but for the factory pattern I was able to create it and learn it. However, for the pattern is there a way it can print out the starting class you chose instead of being always one class from line 68? I tried a switch function, but didn't work.
Client()
{
display();
switch (choice)
{
case 1:player_type type = mage;
break;
case 2:player_type type = ninja;
break;
case 3:player_type type = ninja;
break;
Pplayer = player::create(type);
}
}
I know why now why it doesn't work, but just asking if there's another method. If statements didn't work either if you were wondering.
You're mixing interface issues with the object classes which is not good practice. (for interface stuff use a Menu class perhaps)
display() doesn't return anything so you need to look carefully at the scope of 'choice' within Client() - tricky to resolve
Client doesn't create Player objects, Player::Create(type) does. You won't get arrested by going outside the Factory pattern but there is a deliberate discipline to it.
Use a switch instead of if/else cascade but use it properly - you need a default case for starters.
I'd put line 12 outside the switch block.
case numbers are OK, the enum is better for self-documentation purposes :)
Once I go and make a separate class dedicated to interface, should I use friend function to access the choice variable from the Client class like this?
No need to have complicated friend stuff. All you're doing is creating a menu to do exactly the same as main() and that can be done with a function/method in the Menu class to process the chosen option along with the obvious Menu::display() method ...
Ok, so I changed somethings a little, but can't figure out how to change character class based on what number you enter. The switch function I have doesn't work because of the redefinition of 'type'. Also it says that 'type' is skipped by by 'case' label and 'default' label as well.
Okay do you realize that you're trying to create a new instance of player_type in every case statement?
If you really must create an instance of some type then you will need braces in that case statement to denote a scope. But realize that that scope is only valid within the braces.
You would be better off creating the variable prior to the switch() and just assign a value it that variable in each case.
By the way, do you realize that C++ doesn't require a class for every little thing? For example what is the purpose of placing that cout statement within it's own dedicated class? I haven't looked very closely at the rest of the code but Client code also looks like it may be better off outside of any class.
Forgive me if I 'm coming off stupid here (teaching myself myself how to code sometimes is confusing) but is it something like this your talking about?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Client()
{
std::cin >> choice;
switch (choice)
{
case 1: {player_type type = mage;}
break;
case 2:{player_type type = ninja;}
break;
case 3:{player_type type = barbarian; }
break;
default:exit(1);
Pplayer = player::create(type);
}
}
It currently says 'type' is undefined since putting up the brackets.
This runs with a few small changes however the Client is not the place to have console-oriented cout's and cins. They should be in the relevant (create_a_player) menu. (There could be 50 menu's in the whole game - that's the relevance of a possible Menu class).
Now thing is, is can only happen when the class member is public, which is bad form, and you said not to stress with friend functions earlier correct? Is there any particular method I should use here to separate the input from the client?