Click here to Skip to main content
15,885,030 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have written a program expected to get output
10
00
10

without changing anything in main function but i got a lot of error, help please

error:

C:\Users\Silas\Desktop\rsp2.cpp:37: error: expected `;' before '(' token
C:\Users\Silas\Desktop\rsp2.cpp:44: error: expected `;' before "bool"
C:\Users\Silas\Desktop\rsp2.cpp:44: error: expected `;' before '(' token
C:\Users\Silas\Desktop\rsp2.cpp:50: error: expected `;' before '}' token

C:\Users\Silas\Desktop\rsp2.cpp:51: error: no `void Scissors::setStrength(int)' member function declared in class `Scissors'
C:\Users\Silas\Desktop\rsp2.cpp:67: error: expected `;' before '(' token
C:\Users\Silas\Desktop\rsp2.cpp:72: error: expected `;' before '}' token
C:\Users\Silas\Desktop\rsp2.cpp: In member function `bool Paper::fight(Scissors)':
C:\Users\Silas\Desktop\rsp2.cpp:15: error: `int Tool::strength' is protected
C:\Users\Silas\Desktop\rsp2.cpp:64: error: within this context
C:\Users\Silas\Desktop\rsp2.cpp: At global scope:
C:\Users\Silas\Desktop\rsp2.cpp:73: error: no `void Paper::setStrength(int)' member function declared in class `Paper'
C:\Users\Silas\Desktop\rsp2.cpp: In member function `bool Rock::fight(Paper)':
C:\Users\Silas\Desktop\rsp2.cpp:15: error: `int Tool::strength' is protected
C:\Users\Silas\Desktop\rsp2.cpp:87: error: within this context
C:\Users\Silas\Desktop\rsp2.cpp: In member function `bool Rock::fight(Scissors)':

C:\Users\Silas\Desktop\rsp2.cpp:15: error: `int Tool::strength' is protected
C:\Users\Silas\Desktop\rsp2.cpp:94: error: within this context
C:\Users\Silas\Desktop\rsp2.cpp: At global scope:
C:\Users\Silas\Desktop\rsp2.cpp:97: error: no `void Rock::setStrength(int)' member function declared in class `Rock'
C:\Users\Silas\Desktop\rsp2.cpp: In function `int main()':
C:\Users\Silas\Desktop\rsp2.cpp:105: error: no matching function for call to `Scissors::Scissors(int)'
C:\Users\Silas\Desktop\rsp2.cpp:35: note: candidates are: Scissors::Scissors()
C:\Users\Silas\Desktop\rsp2.cpp:35: note:                 Scissors::Scissors(const Scissors&)
C:\Users\Silas\Desktop\rsp2.cpp:106: error: no matching function for call to `Paper::Paper(int)'
C:\Users\Silas\Desktop\rsp2.cpp:59: note: candidates are: Paper::Paper()
C:\Users\Silas\Desktop\rsp2.cpp:59: note:                 Paper::Paper(const Paper&)
C:\Users\Silas\Desktop\rsp2.cpp:107: error: no matching function for call to `Rock::Rock(int)'
C:\Users\Silas\Desktop\rsp2.cpp:81: note: candidates are: Rock::Rock()
C:\Users\Silas\Desktop\rsp2.cpp:81: note:                 Rock::Rock(const Rock&)
C:\Users\Silas\Desktop\rsp2.cpp:108: error: 'class Scissors' has no member named 'fight'
C:\Users\Silas\Desktop\rsp2.cpp:108: error: 'class Paper' has no member named 'fight'
C:\Users\Silas\Desktop\rsp2.cpp:109: error: 'class Paper' has no member named 'fight'
C:\Users\Silas\Desktop\rsp2.cpp:109: error: 'class Rock' has no member named 'fight'
C:\Users\Silas\Desktop\rsp2.cpp:110: error: 'class Rock' has no member named 'fight'
C:\Users\Silas\Desktop\rsp2.cpp:110: error: 'class Scissors' has no member named 'fight'

Execution terminated


What I have tried:

C++
#include <iostream>

using namespace std;

class Tool
{
protected:
   int strength;
   char type;

public:
   int result;
   void setStrength (int)
   {
      strength = 0;
      type = ' ';
   }

   int getStrength() { return strength; }
   char getType() {return type; }
};

//SCISSOR CLASS*******************************************

class Scissors: public Tool
{
public:
   bool fight(Paper) {
      Paper p1;
      Scissors s1;
      return p1.strength < s1.strength;
   }

   bool fight(Rock) {
      Scissors s1;
      Rock r1;
      return r1.strength < s1.strength;
   }
};
  
void Scissors::setStrength (int x) {
   strength = x;
   type = 's';
}

//PAPER CLASS*********************************************

class Paper : public Tool
{
public:
   bool fight(Scissors) {
      Paper p1;
      Scissors s1;
      return p1.strength < s1.strength;
   }

   bool fight(Rock) {
      Paper p1;
      Rock r1;
      return r1.strength < p1.strength;
   }
};

void Paper::setStrength (int x) {
   strength = x;
   type = 'p';
}

//ROCK CLASS***********************************************

class Rock : public Tool
{
public:
   bool fight(Paper) {
      Paper p1;
      Rock r1;
      return p1.strength < r1.strength;
   }

   bool fight(Scissors) {
      Rock r1;
      Scissors s1;
      return r1.strength < s1.strength;
   }
};

void Rock::setStrength (int x) {
   strength = x;
   type = 'r';
}

//MAIN FUNCTION********************************************

int main() {
   Scissors s1(5);
   Paper p1(7);
   Rock r1(15);
   cout << s1.fight(p1) << p1.fight(s1) << endl;
   cout << p1.fight(r1) << r1.fight(p1) << endl;
   cout << r1.fight(s1) << s1.fight(r1) << endl;
   return 0;
}
Posted
Updated 15-Feb-18 11:29am
v4

Here are a few issues I could spot:

- fight method (in all three subclassses) : you only declare an argument type (Paper, Rock, etc.), but you do not provide any name to your parameters; thus, you cannot use them in the method. Moreover, in these methods, you create new unitialized instances of objects inside the method, whereas you should instead use the current instance AND the parameter.
For example:
C++
bool fight(Paper) {
   Paper p1;
   Scissors s1;
   return p1.strength < s1.strength;
}

would rather be
C++
bool Fight(Paper p1) {
   return p1.strength < strength;
}

... and so on for other occurrences of Fight method.

- getType method: you could just assign the type variable its value in the constructors of the subclasses; there is no reason to reset this value in the setStrength method, whose responsibility is not to deal with the type variable.
For example:
C++
class Tool {
   // ...
protected:
   Tool(const char t) : type(t) { }
};

class Scissors : public Tool {
public:
   Scissors() : Tool('s') { }
};

// ... and so on


- in the main method, you are using constructors whose declarations/definitions you did not show.

I will not correct your whole code, though, for a couple of reasons:
- you will learn much more by identifying, understanding and resolving the issues yourself, rather than someone doing it for you.
- this task is quite beyond the time I am willing/can afford to spend on a quick answer post.

I hope these few lines will put you on the right way, though.
Kindly.
 
Share this answer
 
v2
The following code fixes the compilation errors. However it doesn't fix
  • The (possible) logical errors (I don't even know game details).
  • The poor OOP approach: For instance, your derived classes do take a negligible advantage from inheritance.
  • #include <iostream>
    
    using namespace std;
    
    class Tool
    {
    protected:
       int strength;
       char type;
    
    public:
       int result;
       void setStrength (int s = 0)
       {
          strength = s;
          type = ' ';
       }
    
       int getStrength() { return strength; }
       char getType() {return type; }
    };
    
    //SCISSOR CLASS*******************************************
    
    class Paper;
    class Rock;
    
    class Scissors: public Tool
    {
    public:
      //Scissors(int s):strength(s){}
      Scissors(int s){ strength = s;}
      bool fight(Paper & p);
      bool fight(Rock & r);
      void setStrength(int x);
    };
    
    void Scissors::setStrength (int x)
    {
       strength = x;
       type = 's';
    }
    
    //PAPER CLASS*********************************************
    
    class Paper : public Tool
    {
    public:
      Paper(int s){strength = s;}
      bool fight(Scissors & s);
      bool fight(Rock & r);
      void setStrength(int x);
    };
    
    void Paper::setStrength (int x) {
       strength = x;
       type = 'p';
    }
    
    //ROCK CLASS***********************************************
    
    class Rock : public Tool
    {
    public:
      Rock(int s){strength = s;}
      bool fight(Paper & p);
      bool fight(Scissors & s);
      void setStrength(int x);
    };
    
    void Rock::setStrength (int x)
    {
       strength = x;
       type = 'r';
    }
    
    bool Scissors::fight(Paper & p)
    {
      return p.getStrength() < strength;
    }
    bool Scissors::fight(Rock & r)
    {
      return strength < r.getStrength();
    }
    bool Paper::fight(Scissors & s)
    {
      return strength < s.getStrength();
    }
    bool Paper::fight(Rock & r)
    {
      return r.getStrength() < strength;
    }
    bool Rock::fight(Paper & p)
    {
      return strength < p.getStrength();
    }
    bool Rock::fight(Scissors & s)
    {
      return strength < s.getStrength();
    }
    //MAIN FUNCTION********************************************
    
    int main() {
       Scissors s1(5);
       Paper p1(7);
       Rock r1(15);
       cout << s1.fight(p1) << p1.fight(s1) << endl;
       cout << p1.fight(r1) << r1.fight(p1) << endl;
       cout << r1.fight(s1) << s1.fight(r1) << endl;
       return 0;
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900