c++ - 'operator*' 错误不匹配

标签 c++ c++11

各位程序员大家好!

我打算编写一个小程序,根据用户输入的小时数和工资来计算不同时间段的总工资。我设法制作了一小部分程序,但是当我尝试运行它并测试它时,出现以下错误:

Line 33: error: no match for 'operator*' in 'pay * hours_day'

我尝试用谷歌搜索这个问题,但我真的很困惑是什么原因造成的。

这是我的完整程序代码:

#include <iostream>
#include <random>
#include <time.h>
#include <cstdlib>
#include <string>

using namespace std;

/*

*Program Flowchart*

- Find out how much a worker gets paid per hour, and then find out how many hours they work
a day, and then multiply that to get the total pay in a week or a month or a year.

*/

// global variables

string pay;
float hours_day;


// function that takes the amount of money and calculates the total pay in a week
int total_pay_week() {
    cout << "Type in your salary per hour." << endl;
    cin >> pay;
    cout << "Ok, how many days do you work per day?" << endl;
    cin >> hours_day;

    float total_day = pay * hours_day;

    float total_week = total_day * 7;

    cout << "Your total pay in a week is " << total_week << "if you worked " << hours_day << " per day. " << endl;

}


int main() {

    total_pay_week();

}

这是我的程序的一个非常早期的版本!我只是想知道是什么原因导致了这个问题。

最佳答案

正如 @101010 暗示的那样:pay 是一个 string,而 hours_day 是一个 float,而有些语言允许将字符串与整数相乘,而 c++11(或任何其他形式的 c)不允许,更不用说允许字符串和 float 相乘了。

关于c++ - 'operator*' 错误不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30989236/

相关文章:

c++ - 将从方法返回的原始指针存储到智能指针中

c++ - C++ 11条件变量语义

c++ - 对象、右值引用、常量引用之间的重载解析

c++ - 如何有条件地终止 OpenMP 中的并行区域?

c++ - 编写 C++ 运算符重载?

C++ 在异常时显示堆栈跟踪

c++ - 将 crtp 与公共(public)接口(interface)类一起使用时,如何避免虚拟调用开销?

c++ - 是否有一些忍者技巧可以在声明后将变量设为常量?

c++ - 从没有 key 的散列中查找 unordered_map 中的桶

c++ - 增强算法