0

These are my class functions for the header file :

public:
    hunter(string aSpecies);         // create a hunter of the given species
    void recordKills(string kill);   // add a new kill to the end of the hunter's list of kills
    string *theKills();              // return a pointer to the array of all kills by this hunter
    int numberOfKills();             // how many kills have been recorded

and class variables:

private:
    string kill;
    int numberkilled;
    string kills[20];

I am not sure on how to handle "string *theKills()"

I have tried doing it in this way:

string hunter::*theKills(){
    pointer = kills;
    return pointer;
}

as with the * it doesn't recognise kills as a part of my class variable, but we are supposed to use the same function names.

O'Neil
  • 3,790
  • 4
  • 16
  • 30
Darko
  • 23
  • 3

1 Answers1

0

The syntax goes as follows:

<return type> <class name>::<function name>(<parameters>);

which in your case is:

  • <return type> is string *
  • <class name> is hunter
  • <function name> is theKills
  • <parameters> : none
string * hunter::theKills() {
    return kills; // you don't need a temporary pointer variable
}

Saving you the trouble of using pointers, I'd advise you to use a std::array instead of your C array string kills[20]:

std::array<std::string, 20> kills;

Remember to add const qualifier to every member function which doesn't modify any member of your class.

I'm guessing the use of using namespace std; which is bad practice.

Community
  • 1
  • 1
O'Neil
  • 3,790
  • 4
  • 16
  • 30
  • what is the difference between the std::array and C array. also thank you for your help with the syntax your a champ – Darko May 04 '17 at 02:46
  • thanks heaps mate, i know it seems small, but your help is vastly appreciated! – Darko May 04 '17 at 02:58