简体   繁体   English

对“conclusionaryOutput(number1, number2, isASquare1, isASquare2)”的调用抛出错误,我不知道为什么

[英]the call to “conclusionaryOutput(number1, number2, isASquare1, isASquare2)” is throwing an error and I'm not sure why

The purpose of this program is to accept two numbers from the user, and figure out the two inputted numbers' factors and if they have any factors that are squares.该程序的目的是从用户那里接受两个数字,并计算出两个输入数字的因数,以及它们是否有任何平方数的因数。

When I try to run it, it throws an error on the function call of conclusionaryOutput() :当我尝试运行它时,它会在 function 调用的conclusionaryOutput()中引发错误:

[cquery] address of function 'listSquares' will always evaluate to 'true' [-Wpointer-bool-conversion] [cquery] function 'listSquares' 的地址将始终评估为'true' [-Wpointer-bool-conversion]

and:和:

use of undeclared identifier 'isASquare1';使用未声明的标识符“isASquare1”; did you mean 'listSquares'?你是说'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;
}

When you call your conclusionaryOutput function (at the end of squareInquiry ) you must give it the names of actual variables in the calling module rather than use the names of the parameters given in the function's definition.当您调用您的conclusionaryOutput输出 function (在squareInquiry )时,您必须为其提供调用模块中实际变量的名称,而不是使用函数定义中给出的参数名称。 (This may be a basic misunderstanding of function calling in C++ because, everywhere else in your code, you appear to use the same names for local variables and formal parameters.) (这可能是对 function 在 C++ 中调用的基本误解,因为在代码中的其他任何地方,您似乎对局部变量和形式参数使用相同的名称。)

So, rather than:所以,而不是:

conclusionaryOutput(number1, number2, isASquare1, isASquare2);

you should use:你应该使用:

conclusionaryOutput(number1, number2, containSquareFactor1, containSquareFactor2);

With this change, the program will compile (but with a warning, if you're lucky) … but it (probably) won't work.通过此更改,程序将编译(但如果幸运的话,会出现警告)......但它(可能)不会工作。 What you have forgotten to do (also in the squareInquiry function) is to give the numOfSquares variable an initial value (the compiler is not forced to do this for you).您忘记做的事情(也在squareInquiry函数中)是给numOfSquares变量一个初始值(编译器不会强制为您执行此操作)。

So, near the start of that function, where you declare that variable, give it an initial (zero) value:因此,在声明该变量的 function 的开头附近,给它一个初始(零)值:

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;
//...

Please feek free to ask for any further clarification and/or explanation.请随时要求任何进一步的澄清和/或解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查 number1、number2 和 number3 是否等于 a、b 和 c 但不一定按此顺序 - How to check if number1, number2, and number3 is equal to a, b and c but not necessarily in this order 我收到模板错误,我不确定为什么 - I'm receiving a template error and I'm not sure why 不知道为什么我收到关于std :: size_t不包含参数包的编译错误 - Not sure why I'm getting compilation error about std::size_t not containing a parameter pack COM错误:类未注册(我确定是) - COM Error: Class not registered (I'm sure it is) 为什么使用文字编号进行引用调用时出现“无匹配函数”错误? - Why a "no matching function" error for call by reference with literal number? 引发了C ++读取访问冲突错误,但是我不确定为什么。 瓷砖滑块益智游戏 - C++ Read Access Violation error was thrown, but I'm not sure why. Tile Slider Puzzle Game 为什么初始值设定项列表中的元素数量会导致模棱两可的调用错误? - Why does the number of elements in a initializer list cause an ambiguous call error? 程序停止工作,我确定指针错误 - Program stops working, pointer error i'm sure 在默认构造函数中插入数据后,我没有任何输出,但是我不确定为什么不这样做 - I'm not getting any output after I have inserted data by default constructor but I'm not sure why not 我不确定为什么我的QThread模式被阻止 - I'm not sure why my QThread pattern is blocking
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM