I am making a program (a text game) and I am using four different classes in it. I using a different file for each class. What I want to know is, how do I run/compile the classes together? If it helps, I am running Dev-C++
Thanks for the help.
Printable View
I am making a program (a text game) and I am using four different classes in it. I using a different file for each class. What I want to know is, how do I run/compile the classes together? If it helps, I am running Dev-C++
Thanks for the help.
Assuming that all the files are part of one project, you would just build the project and Dev-C++ will compile each file and link them together for you.
When I want to use the class while in another class, I do: Class classname; then I do classname.method();
When I do the Class classname; I get an error. What do I need to do?
Okay, when you have classes A, B, and C for example. Each class needs two files a header file for each, and an implementation file for each, so it will be
"A.h", "B.h", "C.h" and for implementation "A.cpp", "B.cpp", and "C.cpp"
In the header file of A, if you would like to use either B or C, you have to put at the top
Also to make sure no multiple inclusion errors occur, put your code in the implementation files in this wayCode:#include "C.h"
Replacing A_H_GUARD with B_H_GUARD, or C_H_GUARD depending on the file. This way if you want to include C into a and B you don't have to worry about multiple inclusion compile complaints.Code:#ifndef A_H_GUARD
#define A_H_GUARD
//your A.h declarations go here
#endif
OK, this is what I have as a header:
I get an error saying "GameManager.h: No such file or directory"Code:#include <iostream.h>
using namespace std;
#ifndef GameManager_H_GUARD
#define GameManager_H_GUARD
#include "GameManager.h"
#endif
Yes, there is a GameManager.cpp file.
Edit: Do I need to have the
in every class file?Code:#include <iostream.h>
using namespace std;
<iostream.h> is outdated
<iostream> should be used
using namespace std; should never occur in the header
is GameManager.h is located in the same directory as the file that includes it?
Yes, well, the file isn't called GameManager.h it's GameManger.cpp, don't know if that matters or not.
you should not include cpp-files... So what exactly are you doing?
I have four classes and I made each one it's own .cpp file and they are all in the same project...
Another thing, I saw in a tutorial that I need to have #include <string> in the header when using string variables, is this true?
You should place them (the class declarations) in header files (.h files in this case), and then implement the classes in .cpp files.
OK, I made the classes, except the main() class, into .h files.
What do you mean by implement the clesses in .cpp files?
define the functions in the cpp file
a.ha.cppCode:#ifndef A_H_GUARD
#define A_H_GUARD
class a
{
public:
a();
private:
int i;
};
#endif
Code:#include "a.h"
a::a()
{
i = 0;
}
Header (MyClass.h):Source (MyClass.cpp):Code:#ifndef MYCLASS_H
#define MYCLASS_H
// Any #includes or forward declarations you need go here.
class MyClass
{
public:
MyClass();
void MemberFunction();
private:
int memberVariable;
};
#endif
Main (main.cpp):Code:#include "MyClass.h"
// Any other #includes you need go here.
MyClass::MyClass() : memberVariable(0)
{
}
void MyClass::MemberFunction()
{
// Do stuff
}
If you have a second class that uses the first class, then the cpp file should #include both its own header and the other class's header.Code:// Other #includes you need can go here.
#include "MyClass.h"
// Other #includes you need can go here.
int main()
{
MyClass myVariable;
myVariable.MemberFunction();
}
For example:
Code:#ifndef X_H
#define X_H
// x.h
class X
{
public:
void foo();
};
#endif
Code:// x.cpp
#include "x.h"
void X::foo()
{
// ...
}
OK I'll do that. What about the main class? I can't really do that sort of thing since there's no constructor.
Edit: I'm still getting an error at the
lineCode:#include "GameManager.h"
No header is necessary for the main file that holds the main function. That's what you're talking about, right? See my example of main.cpp above.
>> Edit: I'm still getting an error at the #include "GameManager.h"
You also realize that you typed GameManger in at least one spot in this thread. Make sure your spellings are accurate.
what's the error message?
is the file name correct?
I'm getting very confused on the whole header thing. If someone feels so inclined as to take a look at my code, I would greatly appreciate it it.
http://www.megaupload.com/?d=0QNXJ1QA
http://www.megaupload.com/?d=RWODTUUZ
http://www.megaupload.com/?d=OZ4G851T
http://www.megaupload.com/?d=AR3PDVW7
http://www.megaupload.com/?d=18D890GY
http://www.megaupload.com/?d=BI4VYBT6
http://www.megaupload.com/?d=3RFC46BC
http://www.megaupload.com/?d=7GKP1C3M
can't you post the code here? i have to wait 45 seconds for each dl on megaupload
edit: or better yet, post your error message
Compiler: Default compiler
Building Makefile: "C:\Documents and Settings\Greg\Desktop\RoboMadness\Makefile.win"
Executing make...
make.exe -f "C:\Documents and Settings\Greg\Desktop\RoboMadness\Makefile.win" all
g++.exe -c robo.cpp -o robo.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
In file included from ShopClass.h:4,
from GameManager.h:6,
from robo.cpp:2:
GameManager.h:1:1: unterminated #ifndef
In file included from robo.cpp:2:
GameManager.h:1:1: unterminated #ifndef
robo.cpp:9: error: `string' does not name a type
robo.cpp: In function `int main()':
robo.cpp:16: error: `cout' undeclared (first use this function)
robo.cpp:16: error: (Each undeclared identifier is reported only once for each function it appears in.)
make.exe: *** [robo.o] Error 1
Execution terminated
>> unterminated #ifndef
That means that you are missing the #endif at the bottom, or you have a typo. Post the GameManager.h file, or at least the top and bottom lines.
>> error: `string' does not name a type
You have to #include <string> and either add using namespace std or type std::string.
>> error: `cout' undeclared (first use this function)
You have to #include <iostream> and either add using namespace std or type std::cout.
lol, OK. I'm coming off of Java here so I need to get into the C++ mind set.
GameManager.h
Code:#ifndef ShopClass_H_GUARD
#define ShopClass_H_GUARD
#ifndef BattleClass_H_GUARD
#define BattleClass_H_GUARD
#include "ShopClass.h"
#include "BattleClass.h"
#endif
class GameManager
{
public:
void Manager();
};
in GameManager.h, put the following header guards
and at the end of the file, putCode:#ifndef GAMEMANAGER_H_GUARD
#define GAMEMANAGER_H_GUARD
The header guards are to prevent the header file from begin included twice and prevent getting an error of a class being redeclaredCode:#endif
so you would have...
Code:#ifndef GAMEMANAGER_H_GUARD
#define GAMEMANAGER_GUARD
#include "ShopClass.h"
#include "BattleClass.h"
class GameManager
{
public:
void Manager();
};
#endif
OK, thanks a lot.
Now, how do I use a variable from another class?
This is what I did but I know that it's wrong.
GameManager.h
GameManager.cppCode:#ifndef Gamemanager_H_GUARD
#define GameManager_H_GUARD
#include "ShopClass.h"
#include "BattleClass.h"
#include <iostream>
using namespace std;
class GameManager
{
public:
void Manager();
int money = 200;
int lives = 3;
int attack = 0;
int defense = 0;
string ShopBattle;
int Battle;
int Shop;
};
#endif
Code:#include "ShopClass.h"
#include "BattleClass.h"
#include <iostream>
void GameManager::Manager()
{
int money = 200;
int lives = 3;
int attack = 0;
int defense = 0;
string ShopBattle;
int Battle;
int Shop;
ShopClass shop;
BattleClass battle;
cout << "Current Status:\n";
cout << "Money: $" << money << "\n";
cout << "Lives: " << lives << "\n";
cout << "Attack Points: " << attack << "\n";
cout << "Defense Points: " << defense << "\n";
cout << "Would you like to visit the shop or battle robots?\n(Please enter either Shop or Battle)\n";
cin >> ShopBattle;
if (ShopBattle == "Shop" || ShopBattle == "shop") {
shop.Shop();
} else if (ShopBattle == "Battle" || ShopBattle == "battle") {
battle.Battle();
} else {
cout << "Error: Invalid Input";
}
}
you didn't include "GameManager.h" in your "GameManager.cpp" file
OK, but am I doing the whole variable thing right? Now can I just do "manager.attack = 3"?
These are my errors now:
Code:make.exe -f "C:\Documents and Settings\Greg\Desktop\RoboMadness\Makefile.win" all
g++.exe -c robo.cpp -o robo.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
In file included from BattleClass.h:4,
from GameManager.h:5,
from ShopClass.h:4,
from GameManager.h:4,
from robo.cpp:4:
GameManager.h:13: error: ISO C++ forbids initialization of member `money'
GameManager.h:13: error: making `money' static
GameManager.h:13: error: ISO C++ forbids in-class initialization of non-const static member `money'
GameManager.h:14: error: ISO C++ forbids initialization of member `lives'
GameManager.h:14: error: making `lives' static
GameManager.h:14: error: ISO C++ forbids in-class initialization of non-const static member `lives'
GameManager.h:15: error: ISO C++ forbids initialization of member `attack'
GameManager.h:15: error: making `attack' static
GameManager.h:15: error: ISO C++ forbids in-class initialization of non-const static member `attack'
GameManager.h:16: error: ISO C++ forbids initialization of member `defense'
GameManager.h:16: error: making `defense' static
GameManager.h:16: error: ISO C++ forbids in-class initialization of non-const static member `defense'
In file included from ShopClass.h:4,
from GameManager.h:4,
from robo.cpp:4:
GameManager.h:10: error: redefinition of `class GameManager'
GameManager.h:10: error: previous definition of `class GameManager'
In file included from robo.cpp:4:
GameManager.h:10: error: redefinition of `class GameManager'
GameManager.h:10: error: previous definition of `class GameManager'
make.exe: *** [robo.o] Error 1
Execution terminated
Everything is being redeclared here
Code:#include "ShopClass.h"
#include "BattleClass.h"
#include <iostream>
void GameManager::Manager()
{
int money = 200;
int lives = 3;
int attack = 0;
int defense = 0;
string ShopBattle;
int Battle;
int Shop;
ShopClass shop;
BattleClass battle;
cout << "Current Status:\n";
cout << "Money: $" << money << "\n";
cout << "Lives: " << lives << "\n";
cout << "Attack Points: " << attack << "\n";
cout << "Defense Points: " << defense << "\n";
cout << "Would you like to visit the shop or battle robots?\n(Please enter either Shop or Battle)\n";
cin >> ShopBattle;
if (ShopBattle == "Shop" || ShopBattle == "shop") {
shop.Shop();
} else if (ShopBattle == "Battle" || ShopBattle == "battle") {
battle.Battle();
} else {
cout << "Error: Invalid Input";
}
}
These are the kinds of errors header guards are supposed to preventCode:from GameManager.h:4,
from robo.cpp:4:
GameManager.h:10: error: redefinition of `class GameManager'
GameManager.h:10: error: previous definition of `class GameManager'
In file included from robo.cpp:4:
GameManager.h:10: error: redefinition of `class GameManager'
GameManager.h:10: error: previous definition of `class GameManager'
Since you're getting them, it means that you didn't implement your header guards properly
C++ is case sensitiveCode:#ifndef GameManager_H_GUARD
#define GameManager_H_GUARD
Ok, now I'm getting errors with the main class
and the errors:Code:#include <iostream>
#include <string>
#include "GameManager.h"
using namespace std;
int main()
{
GameManager manager;
cout << "::Welcome to Robo Madness::\n\n";
manager.Manager();
return 0;
}
I thought that you didn't need to make a .h file for the main method. So, what do I do if I want to use a class in the main method?Code:Compiler: Default compiler
Building Makefile: "C:\Documents and Settings\Greg\Desktop\RoboMadness\Makefile.win"
Executing make...
make.exe -f "C:\Documents and Settings\Greg\Desktop\RoboMadness\Makefile.win" all
g++.exe -c robo.cpp -o robo.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
In file included from robo.cpp:4:
GameManager.h:13: error: ISO C++ forbids initialization of member `money'
GameManager.h:13: error: making `money' static
GameManager.h:13: error: ISO C++ forbids in-class initialization of non-const static member `money'
GameManager.h:14: error: ISO C++ forbids initialization of member `lives'
GameManager.h:14: error: making `lives' static
GameManager.h:14: error: ISO C++ forbids in-class initialization of non-const static member `lives'
GameManager.h:15: error: ISO C++ forbids initialization of member `attack'
GameManager.h:15: error: making `attack' static
GameManager.h:15: error: ISO C++ forbids in-class initialization of non-const static member `attack'
GameManager.h:16: error: ISO C++ forbids initialization of member `defense'
GameManager.h:16: error: making `defense' static
GameManager.h:16: error: ISO C++ forbids in-class initialization of non-const static member `defense'
make.exe: *** [robo.o] Error 1
Execution terminated
I dont know how you set up your other files...but making everything in GameManager class public is a no no.
GameManager.h
Code:#ifndef GAMEMANAGER_H
#define GAMEMANAGER_H
#include "ShopClass"
#include "BattleClass"
#include <iostream>
#include <string>
class GAMEMANAGER
{
private:
std::string ShopBattle;
int money;
int lives;
int attack;
int defense;
int battle;
int shop;
// Now you access these variables by the class methods aka functions
public:
GAMEMANAGER();
void SetShopBattle(std::string newShopBattle);
void SetMoney(int newmoney);
void SetLives(int newlives);
void SetAttack(int newattack);
void SetDefense(int newdefense);
void SetBattle(int newbattle);
void SetShop(int newshop);
std::string GetShopBattle();
int GetMoney();
int GetLives();
int GetAttack();
int GetDefense();
int GetBattle();
int GetShop();
};
#endif
GameManager.cpp
Code:#include GameManager.h
GAMEMANAGER::GAMEMANANGER()
{
SetMoney(200);
SetLives(3);
SetAttack(0);
SetDefense(0);
SetBattle(0);
SetShop(0);
}
void GAMEMANAGER::SetShopBattle(std::string newShopBattle)
{
ShopBattle = newShopBattle;
}
void GAMEMANAGER::SetMoney(int newmoney)
{
money = newmoney;
if (money < 0)
money = 0; // You cant have a negative amount of money ;)
}
void GAMEMANAGER::SetLives(int newlives)
{
lives = newlives;
}
void GAMEMANAGER::SetAttack(int newattack)
{
attack = newattack;
}
void GAMEMANAGER::SetDefense(int newdefense)
{
defense = newdefense;
}
void GAMEMANAGER::SetBattle(int newbattle)
{
battle = newbattle;
}
// this is all just redundant code...but it should kinda give you an idea how classes are implemented in c++
OK, that's sort of what I did when I made the program in Java, so I guess I'll do it again. What would the "Get..." variables do?
If I have those variables as private how can I access them in the battle class. This is the sort of thing I need to do:
This is what I made my GameManager.h into:Code:if (PlayersAttack >= 1(which is the robot's attack)) {
he wins
}
And my GameManager.cpp:Code:class GAMEMANAGER
{
private:
std::string ShopBattle;
int money;
int lives;
int attack;
int defense;
int battle;
int shop;
// Now you access these variables by the class methods aka functions
public:
Manager();
void AddMoney(int newmoney);
void Lives(int newlives);
void AddAttack(int newattack);
void AddDefense(int newdefense);
};
Code:GameManager::Manager()
{
}
void GameManager::AddMoney(int amount)
{
money =+ amount;
if (money < 0)
money = 0;
}
void GameManager::Lives()
{
lives =- 1;
}
void GameManager::AddAttack(int amount)
{
attack =+ amount;
}
void GameManager::AddDefense(int amount)
{
defense =+ amount;
}
ShopClass shop;
BattleClass battle;
cout << "Current Status:\n";
cout << "Money: $" << money << "\n";
cout << "Lives: " << lives << "\n";
cout << "Attack Points: " << attack << "\n";
cout << "Defense Points: " << defense << "\n";
cout << "Would you like to visit the shop or battle robots?\n(Please enter either Shop or Battle)\n";
cin >> ShopBattle;
if (ShopBattle == "Shop" || ShopBattle == "shop") {
shop.Shop();
} else if (ShopBattle == "Battle" || ShopBattle == "battle") {
battle.Battle();
} else {
cout << "Error: Invalid Input";
}
}
It's a function, not a variableQuote:
What would the "Get..." variables do?
When you want to use say the money money variable to display or or to do some computing with it, you can't just get the value with "manager.money" since money is private. You'll have to make a function that returns the value of money
Code:class GameManager
{
private:
int money;
//...declare other variables
public:
int GetMoney();
//...declare other functions
};
so instead of directly getting the value with "manager.money", you would call "manager.GetMoney()"Code:int GameManager::GetMoney()
{
return money;
}
Ohhhhhhhhhhhh
Somebody tried explaining that before but you explained it so much better.
Now, I get this
Code:robo.cpp: In function `int main()':
robo.cpp:8: error: `GameManager' undeclared (first use this function)
robo.cpp:8: error: (Each undeclared identifier is reported only once for each function it appears in.)
robo.cpp:8: error: expected `;' before "manager"
robo.cpp:10: error: `manager' undeclared (first use this function)
I fixed some of the errors but I'm getting this one in the robo.cpp file with the main() class.
GameManager.h:21: error: ISO C++ forbids declaration of `Manager' with no type
I don't know what it is but I do have this: GameManager manager; and I have the #include "GameManager.h" in the header.
Also, what is the main() called? Class or function or what?
In your header you declare a class called GAMEMANAGER, but then in your other code you use a class called GameManager.
Those are two different names, case matters in C++.
Yeah, I fixed that before the error.
Are you sure you fixed it all the way? That is what would cause that error, if the spellings or case doesn't match.
>> Also, what is the main() called? Class or function or what?
main is a function like any other function. It is just special in that all programs must have a function called main in global scope and it must have int as its return type and there are some restrictions on the types of parameters it must take. However, it still works pretty much like any other function.
OK, I am still having a butt load of errors: (I fixed the last one)
It looks very redundant so it must be something that I need to learn to do.Code:BattleClass.h:12: error: declaration of `void BattleClass::Battle()'
BattleClass.h:10: error: conflicts with previous declaration `int BattleClass::Battle'
BattleClass.cpp: In member function `void BattleClass::Battle()':
BattleClass.cpp:18: error: no match for 'operator>>' in 'std::cin >> ((BattleClass*)this)->BattleClass::Battle'
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:87: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>&(*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:93: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:102: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:111: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:133: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:164: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:186: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:217: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:239: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:261: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:284: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:306: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:329: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:351: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:373: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:395: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:417: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/istream:646: note: std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&) [with _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/istream:651: note: std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&) [with _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/istream:687: note: std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*) [with _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/istream:692: note: std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*) [with _Traits = std::char_traits<char>]
BattleClass.cpp:19: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:20: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:25: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:27: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:28: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:28: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:33: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:35: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:36: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:36: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:41: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:43: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:44: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:44: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:49: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:51: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:52: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:52: error: invalid use of member (did you forget the `&' ?)
BattleClass.cpp:57: error: invalid use of member (did you forget the `&' ?)
...and my BattleClass.cpp file:
Code:#include "GameManager.h"
#include "ShopClass.h"
#include "BattleClass.h"
#include <iostream>
using namespace std;
void BattleClass::Battle()
{
GameManager manager;
cout << "Welcome to the Battle Arena\n";
cout << "Who would you like to battle?\n";
cout << "1. Robot 1\n";
cout << "2. Robot 2\n";
cout << "3. Robot 3\n";
cout << "4. Robot 4\n";
cout << "5. Robot 5\n";
cin >> Battle;
if (Battle == 1){
if (manager.GetAttack >= 1){
cout << "Attack Successful\n You Won $200\n";
manager.AddMoney(200);
} else {
cout << "You Lost\n";
manager.GetLives =- 1;
}
} else if (Battle == 2){
if (manager.GetAttack >= 2 && manager.GetDefense >= 1){
cout << "Attack Successful\n You Won $300\n";
manager.AddMoney(300);
} else {
cout << "You Lost\n";
manager.GetLives =- 1;
}
} else if (Battle == 3){
if (manager.GetAttack >= 3 && manager.GetDefense >= 2){
cout << "Attack Successful\n You Won $400\n";
manager.AddMoney(400);
} else {
cout << "You Lost\n";
manager.GetLives =- 1;
}
} else if (Battle == 4){
if (manager.GetAttack >= 4 && manager.GetDefense >= 3){
cout << "Attack Successful\n You Won $500\n";
manager.AddMoney(500);
} else {
cout << "You Lost\n";
manager.GetLives =- 1;
}
} else if (Battle == 5){
if (manager.GetAttack >= 5 && manager.GetDefense >= 4){
cout << "Attack Successful\n You Won $600\n";
manager.AddMoney(600);
} else {
cout << "You Lost\n";
manager.GetLives =- 1;
}
} else {
cout << "Error: Invalid Input\n";
}
manager.Manager();
}
When you get a "butt load of errors", always start with the first one, since often fixing that will fix a bunch below it.
In this case, the error message really explains the problem. It points at line 18, which is this line:Then it says:Code:cin >> Battle;
This means that it can't find an operator>> that works with Battle.Code:error: no match for 'operator>>' in 'std::cin >> ((BattleClass*)this)->BattleClass::Battle'
What is Battle? Looking at your code, it is the name of a function. You have an int member variable in there called battle, is that what you wanted? Remember, case matters in C++.
Battle is just the number Robot that he player wants to battle against.
I have the integer in the BattleClass.h file:
class BattleClass
{
private:
int Battle;
You mean you have both a member function and a member variable named Battle inside the BattleClass? The code you posted with the error was a member function of BattleClass called Battle. Now you say you have a member variable named Battle. You can't have both with the same name.
Programming is detail oriented, especially C++. You've got to pay attention to the details like spelling, case, etc. You've got to pay attention to the details when figuring out your errors and when giving information about them to those trying to help. You'll be a much better programmer if you work on that.
OK thanks, I'll try to do that.
So, what do these mean, because I'm still getting them:
invalid use of member (did you forget the `&' ?)
Nevermind, I'm working on it for now. I'm actually finding a lot of my own errors now. They're just little mistakes.
I don't know what to do for this one:
if (restart == "y" || restart == "Y")
is what I have and I'm getting the error:
ISO C++ forbids comparison between pointer and integer
What is restart? I assume it is a single character. If that's the case, then you need to use single quotes around 'y' and 'Y' to indicate that they are characters as well. The double quotes mean they are strings.
Also, you could use tolower() from <cctype>, which would allow you to use only one comparison.
tolower() converts the character passed to it to lowercase. If it's already lowercase or not a letter, it just returns the character it was passed.Code:if(tolower(restart) = 'y')
[edit] I mean ==. Whoops. [/edit]
Psst...
Code:if(tolower(restart) == 'y')
Can I use the tolower for strings also?
No, but you can use it on each character in a string.
OK, compiled and ran. The only thing is, all of the things (money, lives, attack points, and defense points) are way way way higher than they are supposed to be.
I put the Manager function above the other functions in GameManager.cpp and I am now getting these errors:
GameManager.h:13: error: ISO C++ forbids initialization of member `money'
GameManager.h:13: error: making `money' static
GameManager.h:13: error: ISO C++ forbids in-class initialization of non-const static member `money'
for each variable when I print out all the variables when showing the the player stats.
Do you have a separate class instance for each player, or do you keep all the data in one GameManager?
Why is money static?
There is only one player, the person playing.
I didn't make money static. The integer money is private though. I have it here:
That's the GameManager.h file.Code:class GameManager
{
private:
int money = 200;
int lives = 3;
int attack = 0;
int defense = 0;
string battle;
string shop;
char restart;
string ShopBattle;
I don't know if I should give the variables values here or in the Manager() function.
You need to initialize the variables in the constructor (which is probably what you meant by "the Manager() function").
You can initialize class variables in a constructor with the ordinary assignment operator:
Or you can use an initializer list (which you have to use if the variables are constant):Code:class c {
public:
int value;
c(int x);
};
c::c(int x) {
value = x;
}
For multiple variables, the initializer list looks like this:Code:class c {
public:
int value;
c(int x) : value(x);
};
c::c(int x) {
}
The variables should appear in the order they are declared in the class.Code:constuctor() : one(x), two(y), name(firstname) ...;
Also, the constructor can be declared inside the class, making it inline. But that doesn't matter if there's nothing in the body of the constructor.
Code:class constructor {
constructor() : one(x), two(y) {}
};
OK, it's compiling fine now, thanks for all the help.
However, the:
functions aren't working (I have one for each variable). Either that isn't working or the:Code:int GameManager::GetMoney()
{
return money;
}
functions aren't working.Code:void GameManager::AddAttack(int amount)
{
attack =+ amount;
}
Maybe both. I know this because when I played the game I beat every single robot even though I shouldn't and when I should have won money the amount in the stats stayed the same. If you need me to post the code I will.
>> If you need me to post the code I will.
Maybe that would be best.
>> attack =+ amount;
Make sure you copy and paste to prevent typos.
GameManager.cpp
..and an example of code that needs to use some of the variablesCode:#include "ShopClass.h"
#include "BattleClass.h"
#include "GameManager.h"
#include <iostream>
#include <cctype>
using namespace std;
void GameManager::Manager()
{
int money = 200;
int lives = 3;
int attack = 0;
int defense = 0;
ShopClass shop;
BattleClass battle;
cout << "Current Status:\n";
cout << "Money: $" << money << "\n";
cout << "Lives: " << lives << "\n";
cout << "Attack Points: " << attack << "\n";
cout << "Defense Points: " << defense << "\n";
cout << "Would you like to visit the shop or battle robots?\n(Please enter either Shop or Battle)\n";
cin >> ShopBattle;
if (ShopBattle == "Shop" || ShopBattle == "shop") {
shop.Shop();
} else if (ShopBattle == "Battle" || ShopBattle == "battle") {
battle.Battle();
} else {
cout << "Error: Invalid Input";
}
}
int GameManager::GetMoney()
{
return money;
}
int GameManager::GetAttack()
{
return attack;
}
int GameManager::GetDefense()
{
return defense;
}
int GameManager::GetLives()
{
return lives;
}
void GameManager::AddMoney(int amount)
{
money =+ amount;
if (money < 0)
money = 0;
}
void GameManager::Lives()
{
GameManager manager;
lives =- 1;
if (lives == 0)
cout << "Game Over\n";
cout << "Would you like to start again? (y or n)";
cin >> restart;
if (tolower(restart) == 'y') {
money = 200;
lives = 3;
attack = 0;
defense = 0;
manager.Manager();
} else if (tolower(restart) == 'n') {
} else {
cout << "Error: Invalid Input\n";
manager.Lives();
}
}
void GameManager::AddAttack(int amount)
{
attack =+ amount;
}
void GameManager::AddDefense(int amount)
{
defense =+ amount;
}
Code:if (manager.GetAttack() >= 1){
cout << "Attack Successful\nYou Won $200\n\n";
manager.AddMoney(200);
} else {
cout << "You Lost\n\n";
manager.Lives();
}
>> attack =+ amount;
That compiles?
Yeah, should I make it:
attack = attack + amount?
I probably picked that up from Java :x
Still wont work.
It should beThe same for all the other places you have =+, they should be +=. All your =- should be -=. If attack = attack + amount doesn't work then there are probably other problems with your code that this won't fix.Code:attack += amount;
BTW, I guess I see why it compiles.is the same asCode:attack =+ amount;
which is the same asCode:attack = +amount;
Obviously that's not what you want.Code:attack = amount;
I made a few changes and I'm getting
GameManager.h:28: error: `int GameManager::GetMoney()' and `int GameManager::GetMoney()' cannot be overloaded
Tried to look it up, don't know what it means.
Edit: Oops, never mind that.
It's still not doing what I want it to. It wont add money that you win, it wont subtract money when you buy stuff, you can buy anything no matter how much money you have and you can beat any robot. Why wont the functions work?
Read Daved's post.
Are you trying to write a constructor? You should really read about its syntaxQuote:
void GameManager::Manager()
I replaced "void GameManager::Manager()" with "GameManager::GameManager()"
I don't know if I need a deconstructor or not. If I do, what do I put in it, all of the variables?
I'm also getting this error: robo.cpp:12: error: invalid use of `class GameManager'
If your class manages the memory of any of its member variables, then you need to write a destructor to ensure that the memory is deallocated correctly.
When I call on the constructor from a different class do I just type "GameManager();"?
I don't think I did the constructor and destructor correctly
Code:GameManager::GameManager()
{
int money = 200;
int lives = 3;
int attack = 0;
int defense = 0;
ShopClass shop;
BattleClass battle;
cout << "Current Status:\n";
cout << "Money: $" << money << "\n";
cout << "Lives: " << lives << "\n";
cout << "Attack Points: " << attack << "\n";
cout << "Defense Points: " << defense << "\n";
cout << "Would you like to visit the shop or battle robots?\n(Please enter either Shop or Battle)\n";
cin >> ShopBattle;
if (ShopBattle == "Shop" || ShopBattle == "shop") {
ShopClass();
} else if (ShopBattle == "Battle" || ShopBattle == "battle") {
BattleClass();
} else {
cout << "Error: Invalid Input";
}
}
GameManager::~GameManager()
{
delete money;
delete lives;
delete attack;
delete defense;
}
You created local variables named money, lives, attack and defense in your constructor. As stated, you should use an initialisation list to initialise those member variables instead:
Since all four member variables are of a primitive type, you do not need a destructor. To write delete money when you did not write new money is a mistake.Code:GameManager::GameManager() : money(200), lives(3), attack(0), defense(0)
{
ShopClass shop;
BattleClass battle;
// ...
I don't understand when I'm supposed to do that and when I'm not.
Read about constructors and destructors.
I dont know if this is the right way...but this is how I would do it:Quote:
Code:void GameManager::AddMoney(int amount)
{
money =+ amount;
if (money < 0)
money = 0;
}
I would seperate them into two functions first...
Code:void GAMEMANAGER::SetMoney(int newmoney)
{
money = newmoney;
if (money < 0)
money = 0;
}
void GAMEMANAGER::SpendMoney(int amount)
{
SetMoney(money - amount);
}
void GAMEMANAGER::AddMoney(int amount)
{
SetMoney(money + amount);
}
OK, I took a break from my program for a little while and I came back to it and tried to make any necessary changes to it but when I run it then window opens and then closes again. I don't know if I am calling the GameManager's constructor wrong or something in the main() function. I do "GameManager();" when I want to run the constructor for GameManager.cpp.
This is the main() function
and the GameManager.cpp constructorCode:int main()
{
cout << "::Welcome to Robo Madness::\n\n";
GameManager();
return 0;
}
I'm not sure if I'm supposed to put all of that code that's in the constructor in it's own function or not.Code:GameManager::GameManager() : money(200), lives(3), attack(0), defense(0)
{
ShopClass shop;
BattleClass battle;
cout << "Current Status:\n";
cout << "Money: $" << money << "\n";
cout << "Lives: " << lives << "\n";
cout << "Attack Points: " << attack << "\n";
cout << "Defense Points: " << defense << "\n";
cout << "Would you like to visit the shop or battle robots?\n(Please enter either Shop or Battle)\n";
cin >> ShopBattle;
if (ShopBattle == "Shop" || ShopBattle == "shop") {
ShopClass();
} else if (ShopBattle == "Battle" || ShopBattle == "battle") {
BattleClass();
} else {
cout << "Error: Invalid Input";
GameManager();
}
}
Can anyone help?
These are local to the GameManager constructor. When the constructor finishes, you won't have the shop or battle anymore.Code:ShopClass shop;
BattleClass battle;
This is not a way to call the constructor. Constructor is used to construct an instance. Do you see an instance here?Code:ShopClass();
BattleClass();
GameManager();
Really, you are trying to write fairly complicated classes, yet you barely understand what a constructor is, what an instance is etc. May-be you should read some tutorials and practice with simpler exercises first.
hmm... it looks like you are trying to use classes by treating the class name as the name of a function (the constructor), and then attempting to do procedural programming.
What you should do is to determine what you want to model. What is a GameManager? What is it for, and what does it do? Answering this would allow you to design its member functions, especially its public interface. From there you can determine what it must keep track of, and thus decide on what its member variables should be.
Instead of invoking the constructor by treating it as no more than a function call, you would create an object, and use that object. The constructor is just to initialise the state of that object.
So do I just use the constructor to initialize variables and then make a new function and put all the code that I have in the constructor right now in it?
Most likely yes.Quote:
So do I just use the constructor to initialize variables and then make a new function and put all the code that I have in the constructor right now in it?
I keep coming back to my program and I keep having trouble.
BattleClass.cpp: In member function `void BattleClass::BattleMain()':
BattleClass.cpp:11: error: no matching function for call to `GameManager::GameManager()'
GameManager.h:10: note: candidates are: GameManager::GameManager(const GameManager&)
GameManager.h:22: note: GameManager::GameManager(int, int, int, int)
It looks like you do not have a default constructor for GameManager, but there is an attempt to create a GameManager object via the default constructor in BattleClass::BattleMain().