gpt4 book ai didi

c++ - "error: invalid operands of types ' int ' and ' const char [2] ' to binary ' 运算符<< '|"

转载 作者:行者123 更新时间:2023-11-28 02:20:57 24 4
gpt4 key购买 nike

首先,我是初学者。我在网上看到过类似的问题,错误与我相同,但没有一个与我的很相似,因此我无法理解我做错了什么。

我应该让一个斐波那契数列显示到我选择的数字(作为讲葡萄牙语的人,我必须解释一下,“primeiro”的意思是第一个,“segundo”第二个,“terceiro”第三个。)代码是这样的

#include <iostream>
using namespace std;
int main(){
int n;
int primeiro, segundo, terceiro;

primeiro = 1;
segundo = 1;
cout << "Insira o Valor Limite: ";
cin >> n;
cout << " " << endl;

while(terceiro < n){

cout << primeiro << " " ;
cout << segundo << " " ;
terceiro = primeiro + segundo;
cout << terceiro << " " ;

primeiro = segundo << " ";
segundo = terceiro << " " ;
cout << " " << endl;

}

return 0;
}

错误出现在以下两行

primeiro = segundo << " ";
segundo = terceiro << " ";

是这样的

"error: invalid operands of types 'int' and 'const char [2]' to binary 'operator<<'|"

请帮忙

最佳答案

那么您在这两行中实际尝试做什么?在我看来,您可能希望将“primeiro”和“segundo”变量设置为新值并继续循环。所以不需要“<<”运算符。将这两行更改为

primeiro = segundo;
segundo = terceiro;

此外,您还需要将 terceiro 初始化为某个值...

关于c++ - "error: invalid operands of types ' int ' and ' const char [2] ' to binary ' 运算符<< '|",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32497227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com