c++ - 对“conclusionaryOutput(number1,number2,isASquare1,isASquare2)”的调用引发错误,我不知道为什么
问题描述
该程序的目的是从用户那里接受两个数字,并计算出两个输入数字的因数,以及它们是否有任何平方数的因数。
当我尝试运行它时,它会在以下函数调用中引发错误conclusionaryOutput()
:
[cquery] 函数“listSquares”的地址将始终评估为“真”[-Wpointer-bool-conversion]
和:
使用未声明的标识符“isASquare1”;你的意思是“listSquares”吗?
#include <iostream>
using namespace std;
void conclusionaryOutput(int number1, int number2, bool isASquare1, bool isASquare2){
if(isASquare1 == false && isASquare2 == false){
cout << "Therefore the ordered pair (" << number1 << "," << number2 << ") is SWEET." << endl;
}
else if(isASquare1 == false && isASquare2 == false){
cout << "Therefore the ordered pair (" << number1 << "," << number2 << ") is SOUR." << endl;
}
else if(isASquare1 == true && isASquare2 == false){
cout << "Therefore the ordered pair (" << number1 << "," << number2 << ") is SALTY." << endl;
}
else{
cout << "Therefore the ordered pair (" << number1 << "," << number2 << ") is BITTER." << endl;
}
}
void listSquares(int divisor, int numOfSquares){
if (numOfSquares !=1){
cout << ", " << divisor;
}
else{
cout << divisor;
}
}
void squareInquiry(int number1, int number2){
bool containSquareFactor1;
bool containSquareFactor2;
int counter;
int divisor;
int numOfSquares;
counter = 2;
while (counter <= 70){
divisor = counter * counter;
if (number1 % divisor == 0){
numOfSquares++;
listSquares(divisor, numOfSquares);
}
counter++;
}
if (numOfSquares > 0){
cout << number1 << " has these factors (>1) that are square: " << numOfSquares << endl;
cout << number1 << "is not square-free" << endl;
containSquareFactor1 = true;
}
else{
cout << number1 << " has these factors (>1) that are square: (none) " << endl;
cout << number1 << "is square-free" << endl;
containSquareFactor1= false;
}
counter =2;
numOfSquares =0;
while(counter <=70){
divisor = counter * counter;
if (number2 % divisor == 0){
numOfSquares++;
listSquares(divisor, numOfSquares);
}
counter++;
}
if(numOfSquares > 0){
cout << number2 << " has these factors (>1) that are square: " << numOfSquares << endl;
cout << number2 << "is not square-free" << endl;
containSquareFactor2 = true;
}
else{
cout << number2 << " has these factors (>1) that are square: (none) " << endl;
cout << number2 << "is square-free" << endl;
containSquareFactor2= false;
}
conclusionaryOutput(number1, number2, isASquare1, isASquare2);
}
int main(){
int firstInt;
int secondInt;
cout << "Enter the 1st integer of the pair, between 2 and 5000: ";
cin >> firstInt;
while(firstInt < 2 || firstInt > 5000){
cout << "Invalid entry, Please try again: ";
cin >> firstInt;
}
cout << "Enter the 2nd integer of the pair, between 2 and 5000: ";
cin >> secondInt;
while(secondInt <2 || secondInt > 5000){
cout << "Invalid entry, Please try again: ";
cin >> secondInt;
}
squareInquiry(firstInt, secondInt);
return 0;
}
解决方案
当你调用你的conclusionaryOutput
函数时(在 的末尾squareInquiry
),你必须给它调用模块中实际变量的名称,而不是使用函数定义中给出的参数名称。(这可能是对 C++ 中函数调用的基本误解,因为在代码中的其他任何地方,您似乎对局部变量和形式参数使用相同的名称。)
所以,而不是:
conclusionaryOutput(number1, number2, isASquare1, isASquare2);
你应该使用:
conclusionaryOutput(number1, number2, containSquareFactor1, containSquareFactor2);
通过此更改,程序将编译(但如果幸运的话,会出现警告)......但它(可能)不会工作。您忘记做的事情(也在squareInquiry
函数中)是给numOfSquares
变量一个初始值(编译器不会强制为您执行此操作)。
因此,在该函数的开头附近,您声明该变量的位置,给它一个初始(零)值:
void squareInquiry(int number1, int number2)
{
bool containSquareFactor1;
bool containSquareFactor2;
int counter;
int divisor;
int numOfSquares = 0;// Don't use this (below) without initializing it (to zero)
counter = 2;
//...
请随时要求任何进一步的澄清和/或解释。
推荐阅读
- netsuite - 在 suitescript 2.0 中使用 search.create 搜索文件
- mysql - 将表中的多行加入到数组中以用于连接表
- javascript - 当我将鼠标悬停在 html 按钮上时,电子中的单击事件不起作用并且鼠标图标没有改变?
- reactjs - Redux:redux thunk 永远不会被调用
- spring-boot - MockMvc 将字符集附加到内容类型标头
- javascript - 使用 Moment js 从今天开始获得 2 周的天数
- android - ML kit 对象检测适当的数据集和模型
- python - 根据python中嵌套字典的键值从列表中删除字典
- angular - Angular/AngularJS 应用程序依赖注入仅适用于 @Inject() 装饰器
- arrays - 为什么 3[0] 和 3[1] 在 Ruby 中的结果为 1?