如何更正我证明的代码中的错误 [英] How do I correct errors from the code I proved

查看:59
本文介绍了如何更正我证明的代码中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个预计会得到输出的节目

10

00

10


没有改变主要功能但我有很多错误,请帮助



错误:



 C:\ Users \Silas\Desktop\rsp2.cpp:37:错误:预期`;'在'('令牌
C:\ Users \Silas\Desktop\rsp2.cpp:44:错误:预期`;'之前bool
C:\ Users \ Silas \Desktop\rsp2.cpp:44:错误:预期`;'之前'('令牌
C:\ Users \Silas\Desktop\rsp2.cpp:50:错误:预期`; 'before'}'token

C:\ Users \Silas\Desktop\rsp2.cpp:51:错误:没有`void Scissors :: setStrength(int)'成员函数声明在class'Scissors'
C:\ Users \Silas\Desktop\rsp2.cpp:67:错误:预期`;'之前'('token
C:\ Users \ Siglas \Desktop\rsp2.cpp:72:错误:预期`;'之前'}'令牌
C:\ Users \Silas\Desktop\rsp2.cpp:在成员函数`bool Paper: :fight(Scissors)':
C:\ Users \Silas\Desktop\rsp2.cpp:15:错误:`int tool :: strength'受保护
C:\ Users \Silas\Desktop\rsp2.cpp:64:错误:在此上下文中
C:\ Users \Silas\Desktop \rsp2.cpp:在全局范围:
C:\Users \Silas\Desktop\rsp2.cpp:73:错误:没有`void Paper :: setStrength(int)'成员函数声明在class`Paper'
C:\ Users \Silas\Desktop\rsp2.cpp:在成员函数`bool Rock :: fight(Paper)':
C:\ Users \ Silas\Desktop\rsp2.cpp:15:错误:`int Tool :: strength'受保护
C:\ Users \Silas\Desktop\rsp2.cpp:87:错误:在此内context
C:\ Users \Silas\Desktop\rsp2.cpp:在成员函数`bool Rock :: fight(Scissors)':

C:\ Users \\ \\ Silas\Desktop\rsp2.cpp:15:错误:`int Tool :: strength'受保护
C:\ Users \Silas\Desktop\rsp2.cpp:94:错误:内这个上下文
C:\ Users \Silas\Desktop\rsp2.cpp:在全球范围内:
C:\ Users \Silas\Desktop\rsp2.cpp:97:错误:没有`void Rock :: setStrength(int)'在类'Rock'中声明的成员函数
C: \\ Users\Silas\Desktop\rsp2.cpp:在函数`int main()'中:
C:\ Users \Silas\Desktop\rsp2.cpp:105:错误:无匹配调用`Scissors :: Scissors(int)'的函数
C:\ Users \Silas\Desktop\rsp2.cpp:35:注意:候选人是:Scissors :: Scissors()
C:\ Users \Silas\Desktop\rsp2.cpp:35:注意:Scissors ::剪刀(const剪刀&)
C:\ Users \Silas\Desktop\rsp2。 cpp:106:错误:没有匹配函数调用`Paper :: Paper(int)'
C:\ Users \Silas\Desktop\rsp2.cpp:59:注意:候选人是:纸:: Paper()
C:\ Users \Silas\Desktop\rsp2.cpp:59:注意:Paper :: Paper(const Paper&)
C:\ Users \ Silas\Desktop\rsp2.cpp:107:错误:没有匹配函数来调用`Rock :: Rock(int)'
C:\ Users \Silas\Desktop\rsp2.cpp: 81:注意:候选人是:Rock :: Rock()
C:\ Users \\ Silas\Desktop\rsp2.cpp:81:注意:Rock :: Rock(const Rock&)
C:\ Users \Silas\Desktop\rsp2.cpp:108:错误:' class Scissors'没有名为'fight'的成员
C:\ Users \Silas\Desktop\rsp2.cpp:108:错误:'class Paper'没有名为'fight'的成员
C:\ Users \Silas\Desktop\rsp2.cpp:109:错误:'class Paper'没有名为'fight'的成员
C:\ Users \Silas\Desktop\rsp2 .cpp:109:错误:'class Rock'没有名为'fight'的成员
C:\ Users \Silas\Desktop\rsp2.cpp:110:错误:'class Rock'没有会员名为'fight'
C:\ Users \Silas\Desktop\rsp2.cpp:110:错误:'class Scissors'没有名为'fight'的成员

执行终止





我的尝试:



  #include   <   iostream  >  

using namespace std;

class 工具
{
受保护
int strength;
char 类型;

public
int 结果;
void setStrength( int
{
strength = 0 ;
type = ' ';
}

int getStrength(){ return strength; }
char getType(){ return 类型; }
};

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

class 剪刀: public 工具
{
public
bool fight(Paper){
Paper p1;
剪刀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';
}

// 论文类********* ************************************

class 论文: public 工具
{
公共
bool fight(Scissors){
论文p1;
剪刀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 ********* **************************************

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

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

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

// 主要功能********* ***********************************

int main(){
Scissors s1( 5 );
论文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 ;
}

解决方案

以下是我可以发现的一些问题:



- 战斗方法(在所有三个子类中):你只声明一个参数类型( Paper Rock 等等),但您没有为参数提供任何名称;因此,你不能在方法中使用它们。此外,在这些方法中,您在方法内创建新的单元化对象实例,而您应该使用当前实例和参数。

例如:

  bool  fight(Paper){
论文p1;
剪刀s1;
return p1.strength< s1.strength;
}



宁可是

  bool 战斗(论文p1){
return p1.strength<强度;
}



...等等 Fight 方法。



- getType 方法:您可以只分配类型变量子类构造函数中的值;没有理由在 setStrength 方法中重置此值,该方法的责任不是处理类型变量。

例如:

  class 工具{
// ...
受保护
工具( const char t):type(t){}
};

class 剪刀: public 工具{
public
Scissors():工具(' s'){}
};

// ......依此类推





- 在 main 方法中,您使用的构造函数的声明/定义未显示。



我不会更正你的整个代码,原因如下:

- 你会学习更多的是通过自己识别,理解和解决问题,而不是有人为你做这件事。

- 这项任务超出了我愿意/能够花在快速回答帖子上的时间。



我希望这几行能让你走上正确的道路。

亲切。


< blockquote>以下代码修复了编译错误。但它没有解决

  • (可能的)逻辑错误(我甚至不知道游戏细节)。
  • 穷人 OOP 方法:例如,你的派生类确实从继承中获得了微不足道的优势。
  •  #include< iostream>使用namespace std 

    ;

    class工具
    {
    protected:
    int strength;
    字符类型;

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

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

    //剪刀类************************************** *****

    类论文;
    class Rock;

    class剪刀:public工具
    {
    public:
    //剪刀(int s):strength(s){}
    Scissors(int s ){strong = s;}
    bool fight(Paper& p);
    bool fight(Rock& r);
    void setStrength(int x);
    };

    void Scissors :: setStrength(int x)
    {
    strength = x;
    type ='s';
    }

    //纸张类********************************* ************

    类论文:公共工具
    {
    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()<强度;
    }
    bool Scissors :: fight(Rock& r)
    {
    返回力量< r.getStrength();
    }
    bool Paper :: fight(Scissors& s)
    {
    返回力量< s.getStrength();
    }
    bool Paper :: fight(Rock& r)
    {
    return r.getStrength()<强度;
    }
    bool Rock :: fight(Paper& p)
    {
    返回力量< p.getStrength();
    }
    bool Rock :: fight(Scissors& s)
    {
    返回力量< s.getStrength();
    }
    //主要功能************************************* *******

    int main(){
    Scissors s1(5);
    文件第1页(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;
    返回0;
    }


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:

#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;
}

解决方案

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:

bool fight(Paper) {
   Paper p1;
   Scissors s1;
   return p1.strength < s1.strength;
}


would rather be

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:

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.


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;
    }


这篇关于如何更正我证明的代码中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆