State design pattern - Compilation error - c++

I get three errors while i try to compile this program.
I am expecting the following output
OFF-ctor Enter 0/1: 0 already OFF Enter 0/1: 1
going from OFF to ON ON-ctor dtor-OFF Enter 0/1: 1
already ON Enter 0/1: 0 going from ON to OFF OFF-ctor
dtor-ON Enter 0/1: 1 going from OFF to ON ON-ctor
dtor-OFF Enter 0/1: 0 going from ON to OFF OFF-ctor
dtor-ON Enter 0/1: 0 already OFF Enter 0/1:
Following is the program
#include <iostream>
using namespace std;
class Machine
{
class State *current;
public:
Machine();
void setCurrent(State *s)
{
current = s;
}
void on();
void off();
};
class State
{
public:
virtual void on(Machine *m)
{
cout << " already ON\n";
}
virtual void off(Machine *m)
{
cout << " already OFF\n";
}
};
void Machine::on()
{
current->on(this);
}
void Machine::off()
{
current->off(this);
}
class ON: public State
{
public:
ON()
{
cout << " ON-ctor ";
};
~ON()
{
cout << " dtor-ON\n";
};
void off(Machine *m);
};
class OFF: public State
{
public:
OFF()
{
cout << " OFF-ctor ";
};
~OFF()
{
cout << " dtor-OFF\n";
};
void on(Machine *m)
{
cout << " going from OFF to ON";
m->setCurrent(new ON());
delete this;
}
};
void ON::off(Machine *m)
{
cout << " going from ON to OFF";
m->setCurrent(new OFF());
delete this;
}
Machine::Machine()
{
current = new OFF();
cout << '\n';
}
int main()
{
void(Machine:: *ptrs[])() =
{
Machine::off, Machine::on//Error2->invalid use of non-static member function 'void Machine::off()'
//Error3->invalid use of non-static member function 'void Machine::on()'
};
Machine fsm;
int num;
while (1)
{
cout << "Enter 0/1: ";
cin >> num;
(fsm. *ptrs[num])(); //Error1->expected unqualified-id before '*' token
}
}
The code was taken from sourcemaking.com under state design pattern.
I ran it in eclipse and linux g++.
Kindly help.

To get a pointer to a member function, you need to use an & (even though it's optional for getting a pointer to a non-member function): &Machine::off, &Machine::on
For the other, you need to realize that .* is a single token, so you need to remove the space between the two characters: (fsm.*ptrs[num])();

void (Machine::*ptrs[])() =
{
&Machine::off, // note: explicit &
&Machine::on
};
Machine fsm;
int num;
while (1)
{
cout << "Enter 0/1: ";
cin >> num;
(fsm.*ptrs[num])(); // note: no space between . and *
}
That still leaves the following warnings:
try.cc:19:22: warning: unused parameter 'm' [-Wunused-parameter]
try.cc:23:22: warning: unused parameter 'm' [-Wunused-parameter]
try.cc: In member function 'virtual void OFF::on(Machine*)':
try.cc:68:20: warning: deleting object of polymorphic class type 'OFF' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
try.cc: In member function 'virtual void ON::off(Machine*)':
try.cc:76:14: warning: deleting object of polymorphic class type 'ON' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]

Related

Basic derived class member override. Challenge Activity 11.3

I have a challenge for the school that states
Define a member function PrintAll() for class PetData that prints output as follows. Hint: Make use of the base class's PrintAll() function.
Name: Fluffy, Age: 5, ID: 4444
Then they give me the code below with a small snippet that I can alter. Between
// FIXME: Add PrintAll() member function
and
/* Your solution goes here */
is the code I added but I'm not getting the correct result.
MyCode
// FIXME: Add PrintAll() member function
void PrintAll() {
AnimalData data;
data.PrintAll();
cout << ", ID: " << idNum;
}
/* Your solution goes here */
Full Code
#include <iostream>
#include <string>
using namespace std;
class AnimalData {
public:
void SetName(string givenName) {
fullName = givenName;
};
void SetAge(int numYears) {
ageYears = numYears;
};
// Other parts omitted
void PrintAll() {
cout << "Name: " << fullName;
cout << ", Age: " << ageYears;
};
private:
int ageYears;
string fullName;
};
class PetData: public AnimalData {
public:
void SetID(int petID) {
idNum = petID;
};
// FIXME: Add PrintAll() member function
void PrintAll(){
AnimalData data;
data.PrintAll();
cout << ", ID: " << idNum;
}
/* Your solution goes here */
private:
int idNum;
};
int main() {
PetData userPet;
userPet.SetName("Fluffy");
userPet.SetAge (5);
userPet.SetID (4444);
userPet.PrintAll();
cout << endl;
return 0;
}
Results I'm getting
Name: , Age: -502747520, ID: 4444
Results I want
Name: Fluffy, Age: 5, ID: 4444
in
void PrintAll(){
AnimalData data;
data.PrintAll();
cout << ", ID: " << idNum;
}
AnimalData data; creates a new, default-initialized AnimalData that is entirely separate from the AnimalData that is part of the current PetData. Since you want to use the AnimalData you already have, discard this line and remove data from the next. This is where things can go very wrong, so I'm going to skip directly to the correct answer: To get the PrintData function for AnimalData, you need to be explicit:
void PrintAll(){
AnimalData::PrintAll();
cout << ", ID: " << idNum;
}
Why You Cannot Simply Remove data
If you remove the object to invoke PrintAll on, data,
void PrintAll(){
PrintAll();
cout << ", ID: " << idNum;
}
this is assumed. this is a PetData, and for PetData the best match for the PrintAll function is the current function. The result is infinite recursion.
The real lesson to be learned here is to be careful when re-using an identifier. In this case, PetData::PrintAll shadows AnimalData::PrintAll, replacing it inside PetData. The function is not virtual, and you do not want virtual in this case, so you do not get an override. You can easily and accidentally do this with functions and variables, leading to confusion about which one is being used by the program.
A (possibly) Better Way to Structure This Program
Take advantage of both inheritance and polymorphism
#include <iostream>
#include <string>
using namespace std;
class AnimalData
{
public:
virtual ~AnimalData() {}; // with polymorphism you must make sure the
// correct destructor is always called.
// Derived classes will override this
// destructor whether you explicitly define
// them or not.
void SetName(string givenName)
{
fullName = givenName;
}
void SetAge(int numYears)
{
ageYears = numYears;
}
// virtual function. Derived classes can, but do not have to, replace
// this function with a version better suited to the derived class
virtual void PrintAll()
{
cout << "Name: " << fullName;
cout << ", Age: " << ageYears;
}
private:
int ageYears;
string fullName;
};
class PetData: public AnimalData
{
public:
void SetID(int petID)
{
idNum = petID;
}
// Replacing virtual function. Note if the base class function is
// virtual, then child class overrides are automatically virtual
void PrintAll() override // override keyword notifies with a compiler
// error if the function does NOT override when
// it should.
{
AnimalData::PrintAll(); // call base class version for basic data
cout << ", ID: " << idNum; // adding derived class-specific stuff
}
private:
int idNum;
};
// can add WildData here to handle wild animals.
int main()
{
PetData userPet;
userPet.SetName("Fluffy");
userPet.SetAge(5);
userPet.SetID(4444);
userPet.PrintAll();
cout << endl;
// add an vanilla animal for demo
AnimalData generic;
generic.SetName("Fang");
generic.SetAge(7);
generic.PrintAll();
cout << endl;
// demonstrate polymorphism
AnimalData * ptr = & generic;
ptr->PrintAll();
cout << endl;
ptr = & userPet;
ptr->PrintAll(); // runtime calls the correct PrintAll function
cout << endl;
return 0;
}
You appear to have a small misunderstanding about how to invoke methods of the superclass AnimalData in PetData:
void PrintAll(){
AnimalData data;
data.PrintAll();
cout << ", ID: " << idNum;
}
This creates a new instance of the class AnimalData, and invokes PrintAll() on that object. Because the fullName and ageYears aren't initialised in this new object, you get the unexpected output. To invoke the same method of the superclass, the syntax is Superclass::Methodname(). So the correct version of AnimalData::PrintAll() is as follows:
void PrintAll(){
AnimalData::PrintAll();
cout << ", ID: " << idNum;
}
Here's your problem.
void PrintAll(){
AnimalData data;
data.PrintAll();
cout << ", ID: " << idNum;
}
This function creates a new object of class AnimalData (which will be constructed according to the default constructor) and then calls the AnimalData::PrintAll method on that object. What you wanted to do was call the AnimalData::PrintAll method on this object.
void PrintAll(){
AnimalData::PrintAll(); // call base class method
cout << ", ID: " << idNum;
}
So this one was alot simpler than I thought. It's really a matter of calling the printAll method from PetData, and appending the ID num.
public void printAll(){
super.printAll();
System.out.print(", ID: " + idNum);
}

Using print() function in copy constructor C++

The task is to create class, which counts the objects of its type, in every moment. Here is my code. The errors are:
1. the object has type qualifiers that are not compatible with the member function "counter::print";
2. return value type does not match the function type; - this was obwious really!
I corrected the errors and it gives me new one that I can't fix;
1. 'void counter::print(void)': cannot convert 'this' pointer from 'const counter' to 'counter &'
class counter {
private:
static int count;
public:
counter();
counter(const counter &from);
void print() const;
~counter();
};
counter::counter() {
++count;
}
counter::counter(const counter &from) {
++count;
cout << "Copy constructor:\t";
from.print(); // here is the error
}
void counter::print() const{
cout << "\t Number of objects = " << count << endl;
}
counter::~counter() {
--count;
cout << "Destructor:\t\t";
print();
}
int counter::count = 0;
counter f(counter x);
void main() {
counter c;
cout << "After constructing of c:";
c.print();
cout << "Calling f()" << endl;
f(c);
cout << "After calling f():";
c.print();
}
counter f(counter x) {
cout << "Argument inside f():\t";
x.print();
return x;
}
Firstly, change:
void print();
to:
void print() const;
because (a) it's a const method anyway and (b) you're trying to call it in a const context within your constructor.
For the second error here:
void f(counter x) {
cout << "Argument inside f():\t";
x.print();
return x; // 2 - nd error
}
it should be fairly obvious that you can't return a value from a void function. Either change it to:
counter f(counter x) {
cout << "Argument inside f():\t";
x.print();
return x;
}
or simply don't return anything:
void f(counter x) {
cout << "Argument inside f():\t";
x.print();
}
void print();
You have declared a non-const member function, which means it cannot be called by const instances.
from.print(); // 1 - st error
Here the type of from is const counter& which means it cannot call the print function. Instead make your print function const.
void print() const;
void counter::print() const { ... }
return x; // 2 - nd error
Why are you returning anything from a function with a void return type?

Polymorphism program

I am needing help with polymorphism. I have no clue how to work with this. I have to write a program that creates an Orc and a Human and demonstrates the behavior of the classes. I am just needing some help with setting this program up. I have set up the classes with the information, but how do I get the createCharacter function to work? Would I have characterTotal = characterStrength + characterDexterity + characterIntelligence in the createCharacter function? I know my program is not correct right now and I have some errors and things, but I am still just trying to get a better understanding of this.
UPDATE:
I am having trouble with the createCharacter function. It is a pure virtual function and I am needing some help on how to get it to work.
For Human class createCharacter will:
Get the values of STR, DEX and INT. Will calculate the total of the values.
(Let’s assume STR = 17, DEX = 12 and INT = 10. It will store 37 into characterTotal.Itwill print out a message: “The strong human Paladin, has a total scoreof 37.” (Strong adjective comes due to STR being 17. If something is above 17 you should say something related. STR = strong, DEX = dexterous, INT =
intelligent).
For Orc class createCharacter will:
Get the values of STR, DEX and INT. Will calculate the total of the values.
However Orcs receive -2 to INT and DEX. They receive +2 to STR. (Let’s
assume STR = 16, DEX = 10 and INT = 8. It will store 16+2,10-2,8-2 = 28 into
characterTotal.
It will print out a message “The berserker Orc has a total score of 28.” (Here the
Orcs get their adjectives from their clan names so you do not need to do
something specific to STR, DEX or INT.)
CODE:
//character.h
#ifndef CHARACTER_H
#define CHARACTER_H
using namespace std;
class Character
{
protected:
float characterTotal;
public:
virtual void createCharacter() = 0; //Pure virtual function
};
#endif
//human.h
#ifndef HUMAN_H
#define HUMAN_H
#include "Character.h"
using namespace std;
class Human
{
private:
int characterStrength;
int characterDexterity;
int characterIntelligence;
string characterType;
public:
Human();//Constructor
int getStength
{
cout << "Enter a number from 0 to 18\n";
cin >> characterStrength;
return characterStrength;
}
int getDexterity
{
cout << "Enter a number from 0 to 18\n";
cin >> characterDexterity;
return CharacterDexterity;
}
int getIntelligence
{
cout << "Enter a number from 0 to 18\n";
cin >> characterIntelligence;
return characterIntelligence;
}
string getType
{
cout << "Please choose one of the following\n";
cout << "A -- Paladin \n";
cout << "B -- Ranger \n";
cout << "C -- Wizard \n";\
cin >> characterType;
return characterType;
}
};
#endif
//orc.h
#ifndef ORC_H
#define ORC_H
#include "Character.h"
#include "Human.h"
using namespace std;
class orc
{
private:
int characterStrength;
int characterDexterity;
int characterIntelligence;
string characterClan;
public:
orc(); //Constructor
int getStrength
{
cout << "Enter a number between 0 to 18\n";
cin >> characterStrength;
return characterStrength;
}
int getDexterity
{
cout << "Enter a number between 0 to 18\n";
cin >> characterDexterity;
return characterDexterity;
}
int getIntelligence
{
cout << "Enter a number between 0 to 18\n";
cin >> characterIntelligence;
return characterIntelligence;
}
string getClan
{
cout << "Please choose one of the following\n";
cout << "A -- Barbarian \n";
cout << "B -- Berserker \n";
cout << "C -- Vanguard \n";\
cin >> characterClan;
return characterClan;
}
};
#endif
Example: A class called Account posesses a container with Character. Your classes Human and Orc inherit both from Character. Inside of your Human class you may want to set spell ABC to all characters which are created (in createCharacter). But if the player creates an Orc, you might want to set another spell XYZ to it, instead of ABC. Base classes come in handy here. In this example you see one of these abstract functions in action (Character::initCharacter).
You can put in more abstract functions inside of Character IF the classes which are supposed to inherit from Character MUST / NEED TO implement these functions.
This is only an example. If you want to do it properly, you need more than this and would have to modify all for database access and further abstraction.
enum CharacterType
{
CHAR_INVALID = 0x0,
CHAR_HUMAN = 0x1,
CHAR_ORC = 0x4
};
class Character
{
public:
Character(unsigned int charGUID) //You might want to set data in the constructor already
: m_charGUID(charGUID) //right after it has been load from Account::LoadAccountInformation()
{
std::cout << "Constructor of \"Character\"" << std::endl;
}
virtual ~Character(void) //Cleanup if needed
{
std::cout << "Destructor of \"Character\"" << std::endl;
}
virtual void createCharacter(void) = 0;
virtual void initCharacter(void) = 0;
CharacterType GetTypeID(void)
{ return m_typeID; }
protected:
void SetTypeID(CharacterType ct)
{ m_typeID = ct; }
private:
Character(const Character &);
unsigned int m_charGUID;
CharacterType m_typeID;
};
class Human : public Character
{
public:
Human(unsigned int charGUID)
: Character(charGUID)
{
SetTypeID(CHAR_HUMAN);
std::cout << "Constructor of \"Human\"" << std::endl;
}
virtual ~Human(void) //Cleanup if needed
{
std::cout << "Destructor of \"Human\"" << std::endl;
}
void createCharacter(void) override
{
//Set data...
}
void initCharacter(void) override
{
std::cout << "You initialized a character of type \"Human\"" << std::endl;
}
};
class Orc : public Character
{
public:
Orc(unsigned int charGUID)
: Character(charGUID)
{
SetTypeID(CHAR_ORC);
std::cout << "Constructor of \"Orc\"" << std::endl;
}
virtual ~Orc(void) //Cleanup if needed
{
std::cout << "Destructor of \"Orc\"" << std::endl;
}
void createCharacter(void) override
{
//Set data...
}
void initCharacter(void) override
{
std::cout << "You initialized a character of type \"Orc\"" << std::endl;
}
};
class Account
{
public:
Account(unsigned int accountGUID)
{ m_accGUID = accountGUID; }
//#Return: False if load from database failed
bool LoadAccountInformation(void) //You could give it data also
{
//You could also load data directly from a database here if you'd like to
//Here are just some sample values (partially hardcoded)
characters.clear();
const int charsOnAccount = 1; //Load from database
for (int i = 0; i < charsOnAccount; ++i)
{
CharacterType ct = CHAR_HUMAN; //Load from database
unsigned int characterGUID = i;
switch (ct)
{
case CHAR_HUMAN:
{
characters[characterGUID] = std::move(std::shared_ptr<Character>(new Human(characterGUID)));
} break;
case CHAR_ORC:
{
characters[characterGUID] = std::move(std::shared_ptr<Character>(new Orc(characterGUID)));
} break;
default:
{
std::cout << "Invalid character type: " << ct << std::endl; //Or log to file
} break;
}
}
return true;
}
void InitCharacters(void)
{
for (auto itr = std::begin(characters); itr != std::end(characters); ++itr)
itr->second->initCharacter();
}
private:
//A unique account-GUID
unsigned int m_accGUID;
//Let's say a unique char-GUID and the Character object
std::map<unsigned int, std::shared_ptr<Character> > characters;
//And more information...
};
int main(int argc, char *argv[])
{
Account ac = Account(1);
ac.LoadAccountInformation();
ac.InitCharacters();
return 0;
}

Need some clarification on how the state pattern works

#include <iostream>
using namespace std;
class Machine
{
class State *current;
public:
Machine();
void setCurrent(State *s)
{
current = s;
}
void on();
void off();
};
class State
{
public:
virtual void on(Machine *m)
{
cout << " already ON\n";
}
virtual void off(Machine *m)
{
cout << " already OFF\n";
}
};
void Machine::on()
{
current->on(this);
}
void Machine::off()
{
current->off(this);
}
class ON: public State
{
public:
ON()
{
cout << " ON-ctor ";
};
~ON()
{
cout << " dtor-ON\n";
};
void off(Machine *m);
};
class OFF: public State
{
public:
OFF()
{
cout << " OFF-ctor ";
};
~OFF()
{
cout << " dtor-OFF\n";
};
void on(Machine *m)
{
cout << " going from OFF to ON";
m->setCurrent(new ON());
delete this;
}
};
void ON::off(Machine *m)
{
cout << " going from ON to OFF";
m->setCurrent(new OFF());
delete this;
}
Machine::Machine()
{
current = new OFF();
cout << '\n';
}
int main()
{
void(Machine:: *ptrs[])() =
{
Machine::off, Machine::on
};
Machine fsm;
int num;
while (1)
{
cout << "Enter 0/1: ";
cin >> num;
(fsm. *ptrs[num])();
}
}
There are a few bits of code I don't completely understand.
First, what does this do exactly?
(fsm. *ptrs[num])();
It looks like it's calling a default constructor of state, but I am not totally sure. Also, I don't understand where the on and off method is called. I think the object machine is the calling object for the on and off method, but I am not even sure.
Lastly, why do we destroy this?
void on(Machine *m)
{
cout << " going from OFF to ON";
m->setCurrent(new ON());
delete this;
}
Is it only for memory management?
I have rewritten the code with two function pointers and some comments:
Instead of array of function pointers, I have used 2 diff pointers and I am using if else for making the decision for switching state.
Main:
int main()
{
void (Machine::*offptr)() = &Machine::off; //offptr is a member funct pointer that now points to Machine::off function
void (Machine::*onptr)() = &Machine::on; //onptr is a member funct pointer that now points to Machine::on function
Machine fsm;
int num;
while (1)
{
cout<<"Enter 0/1: ";
cin>>num;
if( num == 0 )
{
(fsm.*offptr)(); //Here your are calling the function pointed to by the offptr (i.e., Machine::off) using the pointer
}
else if( num == 1 )
{
(fsm.*onptr)(); //Here your are calling the function pointed to by the onptr (i.e., Machine::on) using the pointer
}
}
}
In your example, all the decision is taken with the help of pointer array indices it self. So if user presses 0 the function pointed by ptrs[0] will be called and for 1 the function pointed by ptr[1] will be called. But since there is no check to make sure the user entered 0/1, the program will crash if the user enters something other than 0 or 1.
void on(Machine *m)
{
cout << " going from OFF to ON";
m->setCurrent(new ON()); //Here you are changing the state of the machine from OFF to ON (Note: call comes to this function only if the previous state was OFF).
delete this; //The previous state instance (OFF state pointed by this pointer) of the machine is no more required. So you are deleting it.
}

Multidimensional Array trouble (bounds)

here is the code with the problem:
#ifndef WEAPONS_H_INCLUDED
#define WEAPONS_H_INCLUDED
#include "Battleship header.h"
void Player::torpedo(string enemyEvtTracker[][10], string myhitboard[][10])
{
string bcn; //board column number
cout << "Enter board column number (1-9): "; cin >> bcn; flush();
if(bcn!="1"&&bcn!="2"&&bcn!="3"&&bcn!="4"&&bcn!="5"&&bcn!="6"&&bcn!="7"&&bcn!="8"&&bcn!="9")
{cout <<endl<< "That is not a valid number.";}
return;
}
#endif // WEAPONS_H_INCLUDED
here is the class Player:
#ifndef BATTLESHIPPLAYERCLASS_H_INCLUDED
#define BATTLESHIPPLAYERCLASS_H_INCLUDED
using namespace std;
class Player // define a class for a player
{
void torpedo(string enemyEvtTracker[10][10], string myhitboard[10][10]);
void cannon();
void scatter();
void menu();
friend int main(int, char*[]); //Let main access the protected members
friend int routine_END(void);
public:
void displaydata()
{cout << money << endl << gunpowder << endl << ammo_cannon << endl << ammo_4hit << endl;}
string savename;
int gunpowder;
int ammo_cannon;
int ammo_4hit; string gun_4;
int ammo_scatter; string gun_s;
int money;
Player(string name){money=18000; gunpowder=100;ammo_cannon=20; ammo_4hit=0; ammo_scatter=0; gun_4="OFF"; gun_s="OFF";playername=name;} //Define the constructor
void simplegame(void) {gunpowder=99999999; ammo_cannon=999999999; ammo_scatter=4; gun_s="ON";}
void getname(string *playername, int option)
{
if (option==1)
{cout << "Enter your name here player 1:"; cin >> *playername;}
else {cout << "Enter your name here player 2:"; cin >> *playername;}
}
string playername;
char mainRowGuess;
int check_transaction (int mymoney, int moneyowed)
{
if (mymoney-moneyowed<<0) {return 0;}
else {return 1;}
}
void change_Answer_to_number(char row,int* outputRow)
{
if (row=='A'||row=='a'){*outputRow =1;}
else if (row=='B'||row=='b'){*outputRow =2;}
else if (row=='C'||row=='c'){*outputRow =3;}
else if (row=='D'||row=='d'){*outputRow =4;}
else if (row=='E'||row=='e'){*outputRow =5;}
else if (row=='F'||row=='f'){*outputRow =6;}
else if (row=='G'||row=='g'){*outputRow =7;}
else if (row=='H'||row=='h'){*outputRow =8;}
else if (row=='I'||row=='i'){*outputRow =9;}
else {*outputRow = 0;}
}
void changeCharToNumber(char row, int* outputRow)
{
if (row=='1'){*outputRow=1;}
else if (row=='2'){*outputRow=2;}
else if (row=='3'){*outputRow=3;}
else if (row=='4'){*outputRow=4;}
else if (row=='5'){*outputRow=5;}
else if (row=='6'){*outputRow=6;}
else if (row=='7'){*outputRow=7;}
else if (row=='8'){*outputRow=8;}
else if (row=='9'){*outputRow=9;}
else {cout << "Unexpected Error." << endl; *outputRow=0;}
}
char airRowStart; char airColStart; char aircraftDirec;
char destRowStart; char destColStart; char destroyerDirec;
char subRowStart; char subColStart; char subDirec;
char patrolStart; char patrolDirec;
/// START MENU FUNCTION
void error_money(void) {cout << "Not enough money!";}
char startRowAircraftCarrier;
char startRowDestroyer;
char startRowSub;
char startRowPatrolBoat;
friend int routine_END (void);
friend void menu (int* gunpowder, int* ammo_cannon, int* ammo_4hit, int* ammo_scatter, int* money, string* gun_4, string* gun_s);
};
#endif // BATTLESHIPPLAYERCLASS_H_INCLUDED
and this generates the following build log...
-------------- Build: Debug in Advanced Battleship Obj
[ 50.0%] Compiling: main.cpp
C:\Advanced_Battleship_Revised_5111\main.cpp:32:
warning: ignoring #pragma comment In
file included from
C:\Advanced_Battleship_Revised_5111\/imputoutput.h:9,
from C:\Advanced_Battleship_Revised_5111\/Battleship
header.h:3,
from C:\Advanced_Battleship_Revised_5111\main.cpp:25:
C:\Advanced_Battleship_Revised_5111\/BattleshipPlayerClass.h:74:
warning: 'int routine_END()' is
already a friend of class 'Player'
C:\Advanced_Battleship_Revised_5111\/BattleshipPlayerClass.h:
In member function 'int
Player::check_transaction(int, int)':
C:\Advanced_Battleship_Revised_5111\/BattleshipPlayerClass.h:33:
warning: suggest parentheses around
'-' inside '<<' In file included from
C:\Advanced_Battleship_Revised_5111\main.cpp:27:
C:\Advanced_Battleship_Revised_5111\/BattleshipMenu.h:
In member function 'void
Player::menu()':
C:\Advanced_Battleship_Revised_5111\/BattleshipMenu.h:118:
warning: label 'GUNPOWDER_MENU_1'
defined but not used
C:\Advanced_Battleship_Revised_5111\/BattleshipMenu.h:166:
warning: label
'CIN_WEAPON_OPTION_SCATTER_CANNON'
defined but not used In file included
from
C:\Advanced_Battleship_Revised_5111\main.cpp:30:
C:\Advanced_Battleship_Revised_5111\/weapons.h:
At global scope:
> C:\Advanced_Battleship_Revised_5111\/weapons.h:5: error: declaration of
'enemyEvtTracker' as multidimensional
array must have bounds for all
dimensions except the first
C:\Advanced_Battleship_Revised_5111\/weapons.h:5: error: expected ')' before ',' token
C:\Advanced_Battleship_Revised_5111\/weapons.h:5: error: expected constructor,
destructor, or type conversion before
',' token
C:\Advanced_Battleship_Revised_5111\/weapons.h:5: error: expected constructor,
destructor, or type conversion before
'myhitboard' Process terminated with
status 1 (0 minutes, 1 seconds) 4
errors, 5 warnings
Player::torpedo(string enemyEvtTracker[10][10], string myhitboard[10][10])
{
//..
}
This is the definition of the member function. Where is the declaration?
My guess is that in the declaration you've not mentioned the size of the array, as you did in the definition. It seems you've written simply enemyEvtTracker [][]? See the definition of Player class, and verify how you've declared torpedo member function in it.