- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个程序的目的是从用户那里接受两个数字,并计算出这两个输入数字的因数,以及它们是否有任何因数是平方数。
当我尝试运行它时,它在 conclusionaryOutput()
的函数调用上抛出错误:
[cquery] address of function 'listSquares' will always evaluate to'true' [-Wpointer-bool-conversion]
和:
use of undeclared identifier 'isASquare1'; did you mean '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
的末尾)时,您必须为其指定实际变量的名称< strong>在调用模块中,而不是使用函数定义中给定的参数名称。 (这可能是对 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;
//...
请随时要求任何进一步的澄清和/或解释。
关于c++ - 对 "conclusionaryOutput(number1, number2, isASquare1, isASquare2)"的调用引发错误,我不确定为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66538452/
我是一名优秀的程序员,十分优秀!