C ++函数和奇怪的错误

时间:2016-09-28 18:00:32

标签: c++ debugging compiler-errors logic

所以我用C ++编写了一个小型的Rock,Paper,Scissors游戏结构,并且我遇到了一些我不理解的错误。

解决方案

函数string numberToWord (int x)不能在函数main中。由于编译器的工作方式,它必须是一个单独的方法。我只是把它移出来然后工作正常。

上一个问题

所以我用C ++编写了一个小型的Rock,Paper,Scissors游戏结构,并且我遇到了一些我不理解的错误。

首先是代码需要&#39 ;;'在NumberToWord函数中,但它不应该是函数。

另一个错误是随机的其他一个声明,它似乎并不喜欢。

也许我错过了一些东西,我不知道,但这应该是一个简单的修复。

#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main()
{
    int seed = static_cast <int> (time(0)); //Sets the random seed
    srand(seed);

    int winCount = 0;

    string numberToWord (int x) {
        string outputChoice;

        if (x == 0) { outputChoice = "Rock"; }
        else if (x == 1) { outputChoice = "Paper"; }
        else if (x == 2) { outputChoice = "Scissors"; }

        return outputChoice;
    }

    while (winCount < 3) {
        int computerChoice = rand() % 4;
        int userChoice;

        cout << userChoice << endl;

        cout << "Please Enter 0 for Rock, 1 for Paper, or 2 for Scissors: "; //Asks for user input
        cin >> userChoice; //Inputs user input to variable

        if (userChoice == computerChoice) {
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "Draw!" << endl;
        }
        else if ((userChoice == 1) && (computerChoice == 2)) { //Rock v Paper
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "Compuer wins!" << endl;
        }
        else if ((userChoice == 1) && (computerChoice == 3)) { //Rock v Scissors
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "You win!" << endl;
            winCount += 1;
        }
        else if ((userChoice == 2) && (computerChoice == 1)) { //Paper v Rock
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "You win!" << endl;
            winCount += 1;
        }
        else if ((userChoice == 2) && (computerChoice == 3)) { //Paper v Scissors
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "Compuer wins!" << endl;
        }
        else if ((userChoice == 3) && (computerChoice == 1)) { //Scissors v Rock
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "Compuer wins!" << endl;
        }
        else if ((userChoice == 3) && (computerChoice == 2)) { //Scissors v Paper
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "You win!" << endl;
            winCount += 1;
        }
    }


    return 0;
}

感谢您的帮助!

Errors

第2部分

简单地说,该程序并不像&#39;&lt;&lt;&lt;&#;;我在许多其他程序的变量中使用它很好,但这次当我使用字符串变量时它会抛出一个错误。我查找了C ++字符串变量,看起来我正确地做了,所以我不知道错误的原因。

参考文献:

http://www.cplusplus.com/doc/tutorial/basic_io/

http://www.cplusplus.com/doc/tutorial/variables/

void displayOutput(int comp, int user, string winner) {
    string compOutputChoice = "";
    string userOutputChoice = "";

    /*
    if (comp == 0) { compOutputChoice = "Rock"; }
    else if (comp == 1) { compOutputChoice = "Paper"; }
    else if (comp == 2) { compOutputChoice = "Scissors"; }

    if (user == 0) { userOutputChoice = "Rock"; }
    else if (user == 1) { userOutputChoice = "Paper"; }
    else if (user == 2) { userOutputChoice = "Scissors"; }
    */

    cout << "Compuer Choose: " << compOutputChoice << endl;
    cout << "You Choose: " << userOutputChoice << endl;
    //cout << winner << endl;

    return;
}

错误:

错误(有效)无操作员&#34;&lt;&lt;&#34; 32

错误(有效)无操作员&#34;&lt;&lt;&#34; 33

错误C2679二进制&#39;&lt;&lt;&#39;:找不到带有&#39; std :: string&#39;类型的右手操作数的运算符(或者没有可接受的转换)32

错误C2679二进制&#39;&lt;&lt;&#39;:找不到带有&#39; std :: string&#39;类型的右手操作数的运算符(或没有可接受的转换)33

2 个答案:

答案 0 :(得分:1)

函数Dim bin() As Byte Dim stream As MemoryStream = New MemoryStream documentoPDF.Save(stream, false) bin = stream.ToArray Response.ClearHeaders Response.Clear Response.Buffer = true Response.ContentType = "application/pdf" Response.AddHeader("content-length", bin.Length.ToString) Response.AddHeader("content-disposition", "attachment; filename="""" + txtnombre.Text + ".pdf, "") Response.OutputStream.Write(bin, 0, bin.Length) 嵌套在string numberToWord (int x)函数中。这不是有效的C ++。

GCC编译器 支持嵌套函数作为扩展,但它不是标准的一部分,其他编译器(我知道)不接受它。只是不要这样做。将函数移出main(或者,如果有意义,将其设为lambda)。

答案 1 :(得分:1)

问题很简单。 numberToWord不能是main的内部函数。如果您使用较新的C ++,请将其移到main之外或将其更改为lambda。

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    def _amount_line(self):
        tax_obj = self.env['account.tax']
        cur_obj = self.env['res.currency']
        res = {}
        for line in self:
            print line.tax_id
            price = self._calc_line_base_price(line)
            qty = self._calc_line_quantity(line)
            print" price:{} & quantity: {}".format(price,qty)
            taxes = tax_obj.compute_all(line.tax_id, price, qty,
                                        line.product_id,
                                        line.order_id.partner_id)
            cur = line.order_id.pricelist_id.currency_id
            res[line.id] = cur_obj.round(cur, taxes['total'])
        return res

    remise_palier = fields.Float('Remise palier (%)')
    remise_total = fields.Float('Remise totale (%)')
    price_subtotal = fields.Float(compute='_amount_line', string='Subtotal')