【问题标题】:c++ cout and cin doesn't compilesc++ cout 和 cin 无法编译
【发布时间】:2016-06-30 21:28:45
【问题描述】:

我正在尝试使用 Visual Studio 编写一个非常基本的 C++ 代码。 由于“cin”和“cout”,代码无法编译。 这是代码:

#include <iostream>

using namespace std;

class employer
{
private:
    string id;
    float work_hours, over_time_hours,hourly_salary,salary;
public:
    employer()
    {
        id="123456789";
        hourly_salary=25;
        work_hours=0;
        over_time_hours=0;
        salary=0;
    }
    void get_employer()
    {
        cin >> id >> salary >> work_hours >> over_time_hours;
    }
    void print_employer()
    {
        cout << "I.D. #" << id << "\n and his hourly salary is: " << salary << '\n';
    }
        void salary_calculation()
    {
        salary= (work_hours*hourly_salary + 1.5*over_time_hours*hourly_salary);
    }

};
int main(){
    employer employer1;
    employer1.get_employer();
    employer1.salary_calculation();
    employer1.print_employer();
    return 0;
}

编译错误是:

"1>ex1.cpp(21): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)"

【问题讨论】:

标签: c++ iostream


【解决方案1】:

stringoperator&gt;&gt; 重载位于 string 标头中,因此您应该包含它:

#include <string>

【讨论】:

    猜你喜欢
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多