c++ - 新手错误 "expression must have integral or enum type"

标签 c++

温柔一点……我学习 C++ 已经 5 周了。我已经挖了又挖,但无法弄清楚为什么 Visual Studio Express(和在线编译器)会抛出关于此的错误。

请注意,为了清楚起见,我已经包含了所有声明——大多数都用于代码的不同部分。出现错误的行是这一行:newsharePrice = perchangeEnd * profitLoss << '\n';

我得到的错误是 c2296,左操作数的类型为 double 。我不知道为什么它不喜欢这个......我乘以其他 double 就好了。

double numberShares,
    sharePrice,
     profitLoss,
    profitGain,
    commissionPaid,
    commissionCost,
    sharesCost,
    totalshareCost,
    newtotalshareCost,
    newcommissionCost,
    newsharePrice;
double perChange;
double perchangeEnd;

const int minVALUE = 1;
const int maxVALUE = 100;
int seed = time(0);
srand (seed);
perChange = (rand() % (maxVALUE - minVALUE + 1)) + minVALUE;
cout << perChange << '\n';
perchangeEnd = perChange / 100;

int flip = rand() % 2 + 1;
if (flip == 1)
    profitLoss = 1;
else
    profitLoss = -1;

newsharePrice = perchangeEnd * profitLoss << '\n';
newsharePrice = newsharePrice + sharePrice;
cout << newsharePrice << '\n'; 
newtotalshareCost = numberShares * newsharePrice;
cout << "You've now paid " << newtotalshareCost << " for your shares." << '\n';
newcommissionCost = newtotalshareCost * commissionRate;
cout << "The new amount of commission for this is " << newcommissionCost << " ." << '/n';

最佳答案

好吧,只读有问题的行:

   newsharePrice = perchangeEnd * profitLoss << '\n';
//                                          ▲▲▲▲▲▲▲▲

那个<< '\n'不是乘法的一部分;从你的cout复制面食失败线条?

在这种情况下,编译器别无选择,只能假设您正在尝试执行按位左移操作,而这不能在 double 上执行。 ;仅适用于整数。

关于c++ - 新手错误 "expression must have integral or enum type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32783069/

相关文章:

c++ - 具有未知变量类型的文件的最佳数据结构

c++ - 别名模板特化

c++ - 转发可从函数指针类型间接推导的参数

c++ - 变量自动分配

c++ - 按常量偏移多段线

c++ - 如何在滚动区动态插入项目并在Qt中加载滚动条?

C++ 更新函数参数

c++ - 内联函数和一个定义规则

C++ 查找文本文件中每个单词的字符数

c++ - qt图形场景中qtgraphics项的对齐