温柔一点……我学习 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/