首页 > 解决方案 > 对“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;
}

标签: c++

解决方案


当你调用你的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;
//...

请随时要求任何进一步的澄清和/或解释。


推荐阅读