I am beginner in C++. So I can't find the mistake. There is a problem for "string* toolHolder
" .The error message is that;
function definition does not declare parameters
string* toolHolder:
What kind of a problem is this? I look same problems but I can't solve the problem.
#include <iostream>
#include <string>
#include <new>
using namespace std;
class GCharacter{
friend ostream &operator<<( ostream &output, const GCharacter &gc );
public:
static const int DEFAULT_CAPACITY = 5;
GCharacter(string name = "John",int capacity= DEFAULT_CAPACITY); //constructor
GCharacter(const GCharacter& source); //copy constructor
GCharacter& operator=(const GCharacter& source); //overloaded assignment
~GCharacter(); // Destructor
void insert(const string& toolName); // insert a new tool into the tool holder
private:
string name;
int capacity;
int used;
string* toolHolder:
};
int main(){
GCharacter gc1;
gc1.insert("potion");
gc1.insert("crossbow");
gc1.insert("candle");
gc1.insert("cloak");
gc1.insert("sword");
gc1.insert("book of spells");
cout<<gc1<<endl;
}
GCharacter::GCharacter(string n,int cap){ //constructor
name=n;
capacity=cap;
used=0;
toolHolder = new string[cap];
}
GCharacter::GCharacter(const GCharacter& source){ //copy constructor
cout<<"Copy Constructor Called"<<endl;
name = source.name;
capacity = source.capacity;
used = source.used;
delete []toolHolder;//g
toolHolder = new string[capacity];
for(int i= 0; i<used; i++){
toolHolder[i]=source.toolHolder[i];
}
}
GCharacter& GCharacter::operator=(const GCharacter& source ){ //overloaded assigment operator
cout<<"Overloaded Assigment Called"<<endl;
if(this == &source){ // gc1=gc1
return *this;
}
else{
name = source.name;
capacity = source.capacity;
used = source.used;
delete []toolHolder;//g
toolHolder = new string[capacity];//g
for(int i= 0; i<used; i++){
toolHolder[i]=source.toolHolder[i];
}
return *this;
}
}
GCharacter::~GCharacter(){ //destructor
cout<<"Destructor Called For"<<name<<"this memory location"<<this<<endl;
delete[] toolHolder;
}
void GCharacter::insert(const string& toolName){
if(used==capacity){
cout<<"Tool Holder is full.Cannot add any additional tools"<<endl;
}
else{
toolHolder[used]=toolName;
used++;
}
}
ostream &operator<<( ostream &output, const GCharacter &gc ){ // overloaded ostream
output<<"Game Character"<<gc.name<<"\nhas the following tools:"<<endl<<"| ";
for(int i=0; i<gc.used;i++){
output<< gc.toolHolder[i] +" | ";
}
return output<<endl;
}
Aucun commentaire:
Enregistrer un commentaire