2

紹介コースワーク用にガソリン ポンプ プログラムを作成するように依頼されましたが、実行に問題があります。現時点では、コードの 完全なビルド ダイアログをここでコンパイルしようとしているときに、これがコンパイラーが出力する主なものです。

1>m:\visual studio 2010\projects\referral\referral\main.cpp(56): エラー C2678: バイナリ '>>': 型 'std::istream' の左側のオペランドを取る演算子が見つかりません (または受け入れ可能な変換がない)

#include <iostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <ctime>
#include <cmath>
#include <string>
#include <Windows.h>

using namespace std;
int reciept();
int pump;
int petrol;

int main()
{
    bool exit = false;
    int code;
    string p1w ("Waiting");
    string p2w ("Waiting");
    string p3w ("Waiting");
    string p4w ("Waiting");
    string p1r ("Ready");
    string p2r ("Ready");
    string p3r ("Ready");
    string p4r ("Ready");

    if (GetAsyncKeyState(VK_ESCAPE))
    {
        exit = true;
    }

    cout << "***************************************************" << endl;
    cout << "*Liquid Gold v1.0.0 Last revised 18/07/13         *" << endl;
    cout << "*The process of processing transactions is simple,*" << endl;
    cout << "*activate a pump by entering its code shown below.*" << endl;
    cout << "*After pump operation is verified (generally 10   *" << endl;
    cout << "*seconds though this may vary) the attendant      *" << endl;
    cout << "* will be able to enter the amount of petrol to 3 *" << endl;
    cout << "*decimal places which will then be converted based*" << endl;
    cout << "*on a predetermined price (which can be altered in*" << endl;
    cout << "*price.txt) and once this process is complete a   *" << endl;
    cout << "*receipt will be created (you will need seperate  *" << endl;
    cout << "*software in order to print this recipt) and the  *" << endl;
    cout << "*transaction will be terminated.                  *" << endl;
    cout << "*© Simple Software Solutions 2013                 *" << endl;
    cout << "***************************************************" << endl << endl;
    system("Pause");

    while (exit == false)
    {

        cout << "       Pump (1) - " << p1w << "        Pump (2) - " << p2w << endl << endl << endl;
        cout << "       Pump (3) - " << p3w << "        Pump (4) - " << p4w << endl << endl << endl;

        cin >> "Please enter a pump code:" >> code;

        if (code == 1)
        {
            swap (p1w, p1r);
            pump = 1;
            cin >> "Please enter the amount of petrol deposited" >> petrol;
            break;
        } 

        else if (code == 2)
        {
            swap (p2w, p2r);
            pump = 2;
            cin >> "Please enter the amount of petrol deposited" >> petrol;
            break;
        }

        else if (code == 3)
        {
            swap (p3w, p3r);
            pump = 3;
            cin >> "Please enter the amount of petrol deposited" >> petrol;
            break;
        }

        else if (code == 4)
        {
            swap (p4w, p4r);
            pump = 4;
            cin >> "Please enter the amount of petrol deposited" >> petrol;
            break;
        }
        else
        {
            cout << "Invalid pump code entered";

        }
        reciept();
        {
             ofstream transactions;
             transactions.open ("reciept.txt");
             transactions << "****************************************************/n";
             transactions << "                        SALE                        /n";
             transactions << "****************************************************/n /n";
        }
    }

    return 0;
}

私は周りを見回しましたが、そのエラーに対して見つけることができる唯一の解決策は、既に行ったものを含めることであり、他の解決策は見られません。

私よりも注意深い人は、全体を見て、どこが間違っているのか教えてくれますか?

また、私のコードが非効率的な混乱であることは承知しており、そのことをお詫び申し上げます。

4

2 に答える 2

14

変化する

cin >> "Please enter a pump code:" >> code;

cout << "Please enter a pump code: ";
cin >> code;

コード内のすべてを変更する必要がありcin >> "string"ます。これは、ユーザーに入力を求めるという意味ではありません。代わりに、実際には文字列リテラルに書き込もうとしています。

于 2013-08-05T15:11:10.643 に答える
4

ヤンの答えの上に色を追加しただけで、これはタイトルで示唆されている「バイナリエラー」ではありません。エラー メッセージは を参照してbinary'>>'います。>>は二項演算子であり、二項演算子は両側に 1 つずつ、合計 2 つのオペランドを取ります。+および-次の二項演算子として機能しています。

1 + 2
var1 - var2

単項演算子はオペランドを 1 つだけ取ります。&および-次の単項演算子として機能しています。

my_pointer = &n;
int var3 = -5;

表示されるエラー メッセージの重要な部分:

バイナリ '>>' : タイプ 'std::istream' の左側のオペランドを取る演算子が見つかりません (または、受け入れ可能な変換がありません)

最後のビット、「または受け入れ可能な変換がありません」。>>の左側のオペランドを取る演算子は確かにありstd::istreamます>>が、文字列リテラルを代入できないため、右側の文字列リテラルを取る演算子は定義されていません。この場合、std::cin >> myvarは から何かを取得std::cinし、それを変数 に入れようとしますmyvarが、 のような文字列リテラルに何かを詰め込む方法はあり"Please enter a pump code:"ません。

"Please enter a pump code:" = 5;

これは明らかにナンセンスです。

于 2013-08-05T15:22:14.370 に答える