C++ 编译时错误

c++ compile time error

本文关键字:编译时错误 C++      更新时间:2023-10-16

我正在学习 c++ 并尝试一些东西......下面的代码给了我一个编译时错误,谁能向我解释为什么,我有点困惑......我假设这是cin >> playagain声明。谢谢你的帮助。(另外,如果我犯了任何其他一般的C ++错误,请告诉我)

这是错误:

错误

1 错误 C2679:二进制">>":找不到采用类型为"std::string"的右操作数的运算符(或者没有可接受的转换) C:\用户\Abdo\文档\Visual Studio 2012\项目\控制台应用程序1\控制台应用程序1\控制台应用程序1.cpp 45 1 控制台应用程序 1

无论如何,包括#include <string>解决了问题,谢谢0x499602D2

#include "stdafx.h"
#include <iostream>
using namespace std;
class calculatorc1 {
public:
    calculatorc1();
    ~calculatorc1();
    int multnums(int a, int b);
protected:
    int result;
};
calculatorc1::calculatorc1() {
}
calculatorc1::~calculatorc1() {
}

int calculatorc1::multnums(int a, int b) {
    int result = a * b;
    return result;
}

int main()
{
    string playagain;
    bool calcing = true;
    while (calcing) {
    calculatorc1 c;
    int x;
    int y;
    cout << "first numn";
    cin >> x;
    cout << "secondn";
    cin >> y;
    cout << c.multnums(x, y) << "n";
    cout << "mul again? (y/n)n";
    cin >> playagain;
    if (playagain == "n") {
        calcing = false;
        system("pause");
    }
    }
}
我相信

问题是你没有包含<string>标头,这就是为什么你会收到编译时错误的原因,因为程序中没有定义string。您将需要此行来解决此问题:

#include <string>

我复制了这个文件并运行了它,并得到了两个错误。

第一个是#include "stdafx.h"上的编译器错误(说它找不到文件)。一旦我删除了它,程序编译得很好。

第二个错误是在运行时,在我乘以数字之后。它说没有找到"暂停"(它在某些环境中有效;在我的环境中没有)。

这适用于纯整数类型,但没有错误处理。 44.0导致它出错。

你能描述一下你所看到的,以及你期待的吗?