这是我在StackOverflow上的第一篇文章!我的Intro C++类项目遇到问题。我在for循环中的调用中收到E0349错误,“没有运算符匹配这些操作数”。我在这里看过类似的问题,但没有找到解决方案。如果我格式化错误或不了解很多,我深表歉意。这是我上编程的第一个类。
我的代码:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void disp2xInt(int num);
int main() {
// Do "Press any key to continue..." on exit
atexit([] {system("pause"); });
// Print out your name and course
cout << "Jacob" << endl;
cout << "ELET115N" << endl << endl;
for (int i = 0; i < 5; i++) {
cout << disp2xInt(i) << "\n";
cout << "Hello world!!!\n";
}
disp2xInt(9);
return 0;
}
void disp2xInt(int n) {
cout << "2 x " << n;
cout << " = " << n * 2;
cout << endl;
}
最佳答案
您不能cout
返回无效的方法。此外,在这种情况下没有必要,因为该方法本身已经调用了cout
。做就是了 :
int main() {
// Do "Press any key to continue..." on exit
atexit([] {system("pause"); });
// Print out your name and course
cout << "Jacob" << endl;
cout << "ELET115N" << endl << endl;
for (int i = 0; i < 5; i++) {
disp2xInt(i);
cout << "Hello world!!!\n";
}
disp2xInt(9);
return 0;
}
关于c++ - 在for循环C++中调用Void函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64755627/