函数定义不声明参数(不能查找错误)

时间:2017-10-29 16:43:51

标签: c++11

我是C ++的初学者。所以我找不到错误。 " string* toolHolder"存在问题。 。错误信息是;

  17bigthree.cpp:29:10: error: function definition does not declare parameters
  string* toolHolder:
          ^
17bigthree.cpp: In constructor ‘GCharacter::GCharacter(std::__cxx11::string, int)’:
17bigthree.cpp:55:2: error: ‘toolHolder’ was not declared in this scope
  toolHolder = new string[cap];  
  ^
17bigthree.cpp: In copy constructor ‘GCharacter::GCharacter(const GCharacter&)’:
17bigthree.cpp:66:11: error: ‘toolHolder’ was not declared in this scope
  delete []toolHolder;//g
           ^
17bigthree.cpp:71:24: error: ‘const class GCharacter’ has no member named ‘toolHolder’
   toolHolder[i]=source.toolHolder[i];
                        ^
17bigthree.cpp: In member function ‘GCharacter& GCharacter::operator=(const GCharacter&)’:
17bigthree.cpp:87:12: error: ‘toolHolder’ was not declared in this scope
   delete []toolHolder;//g
            ^
17bigthree.cpp:91:25: error: ‘const class GCharacter’ has no member named ‘toolHolder’
    toolHolder[i]=source.toolHolder[i];
                         ^
17bigthree.cpp: In destructor ‘GCharacter::~GCharacter()’:
17bigthree.cpp:99:11: error: ‘toolHolder’ was not declared in this scope
  delete[] toolHolder;
           ^
17bigthree.cpp: In member function ‘void GCharacter::insert(const string&)’:
17bigthree.cpp:107:3: error: ‘toolHolder’ was not declared in this scope
   toolHolder[used]=toolName;
   ^
17bigthree.cpp: In function ‘std::ostream& operator<<(std::ostream&, const GCharacter&)’:
17bigthree.cpp:115:15: error: ‘const class GCharacter’ has no member named ‘toolHolder’
   output<< gc.toolHolder[i] +" | ";

这是一个什么样的问题?我看起来一样的问题,但我无法解决问题。

#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:   //error1

};

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];  //error2
}


GCharacter::GCharacter(const GCharacter& source){  //copy constructor

    cout<<"Copy Constructor Called"<<endl;

    name = source.name;
    capacity = source.capacity;
    used = source.used;
    delete []toolHolder;    //error3
    toolHolder = new string[capacity];


    for(int i= 0; i<used; i++){
        toolHolder[i]=source.toolHolder[i];   //error4
    }

}


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];   //error5

        for(int i= 0; i<used; i++){
            toolHolder[i]=source.toolHolder[i];   //error6
        }
        return *this;
    }
}

GCharacter::~GCharacter(){    //destructor
    cout<<"Destructor Called For"<<name<<"this memory location"<<this<<endl;
    delete[] toolHolder;    //error7
}

void GCharacter::insert(const string& toolName){
    if(used==capacity){
        cout<<"Tool Holder is full.Cannot add any additional tools"<<endl;
    }
    else{
        toolHolder[used]=toolName;  //error8
        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] +" | ";  //error9
    }
    return output<<endl;
}

0 个答案:

没有答案