新手错误与"expression must have integral or enum type"

Newbie error with "expression must have integral or enum type"

本文关键字:or integral enum type have must 错误 expression 新手      更新时间:2023-10-16

温柔点…我学c++已经5周了。我挖了又挖,不明白为什么Visual Studio Express(和在线编译器)会抛出错误。

请注意,为了清晰起见,我包含了所有声明——大多数声明在代码的不同部分使用。得到错误的行是这样的:newsharePrice = perchangeEnd * profitLoss << 'n';

我得到的错误是c2296,左操作数的类型是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 s上无法执行;

虽然编译错误现在已修复,但您的域错误仍然存在(今天是星期五,不是吗?)为什么股价波动会以任何方式影响你的佣金?你已经有了自己的位置。您还可以使用浮点精度来衡量您的股票数量。虽然在某些情况下,您的股份数量可能不均匀,但这种情况很少发生。你是真的在解释这个还是只是错误地使用了double?大多数系统会将共享数计算为整数。此外,你可以有负的位置,这之后所有的计算将给负佣金!经纪人不会同意;)。最后,但并非最不重要的是,美国佣金很少以交易价值的百分比表示。它通常以每股几美分的形式收费(对大多数零售经纪人来说,这是固定的交易成本)。