并非所有控制路径都返回布尔值

Not all control paths return a value for Boolean?

本文关键字:返回 布尔值 路径 控制      更新时间:2023-10-16

//我使用了一个布尔函数,它返回false和true,但main并没有//拾取它。my_string.Is_full和my_stringin.Is_empty被假设为"it Is not//full"answers"it Is not empty。语法错误?

#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
template <class New_Type>
class Array_Class
{
public:
    Array_Class();
    ~Array_Class();
    void Add(New_Type item);
    void Print();
    void PrintB();
    bool Is_Empty();
    bool Is_Full();
private:
    New_Type *A;
    New_Type *B;
    int count;
};

template <class New_Type>
Array_Class<New_Type>::Array_Class()
{
    cout << "You are inside the default constructor.n";
    cout << "New_Type has a size of " << sizeof(New_Type) << " bytesnn";
    count = 0;
    A = new New_Type[SIZE];
}
template <class New_Type>
Array_Class<New_Type>::~Array_Class()
{
    cout << "The Destructor has been called.nn";
    delete[] A;
    count = 0;
    A = 0;
}

template <class New_Type>
void Array_Class<New_Type>::Add(New_Type item)
{
    if (count<SIZE)
    {
        A[count++] = item;
    }
    else
    {
        cout << "The array is full.n";
    }
}

template <class New_Type>
void Array_Class<New_Type>::Print()
{
    int i;
    for (i = 0; i<count; i++)
    {
        cout << "A[" << i << "] = " << A[i] << endl;
    }
}

//my_String转到此处,它确实得到假

template <class New_Type>
bool Array_Class<New_Type>::Is_Full()
{
    if (count == SIZE)
    {
        return true;
    }
    else if (count < SIZE)
    {
        return false;
    }
}

//my_String转到此处,它确实得到假

template <class New_Type>
bool Array_Class<New_Type>::Is_Empty()
{
    if (count == 0)
    {
        return true;
    }
    else if (count > 0)
    {
        return false;
    }
}
int main()
{
    Array_Class<string> my_String;
    Array_Class<int> my_Ints;
    Array_Class<char> my_Chars;
    my_String.Add("Hello");
    my_String.Add("GoodBye");
    my_String.Add("ComeHere");
    my_String.Add("SayNo");
    my_Chars.Add('a');
    my_Chars.Add('b');
    my_Chars.Add('c');
    my_Chars.Add('d');
    my_Chars.Add('e');
    my_Chars.Add('f');
    my_Chars.Add('g');
    my_String.Print();
    my_Ints.Print();
    my_Chars.Print();
    cout << endl;
    my_Ints.Is_Empty();
    if (true)
    {
        cout << "It is empty" << endl;
    }
    else
    {
        cout << "It is not emptyn" << endl;
    }

my_String.Is_Empty((;应该是假的,但直接变成了真的

    my_String.Is_Empty();
    if (true)
    {
        cout << "It is empty" << endl;
    }
    else
    {
        cout << "It is not emptyn" << endl;
    }
    cout << endl;

    my_Chars.Is_Full();
    if (true)
    {
        cout << "It is full" << endl;
    }
    else if (false)
    {
        cout << "It is not full" << endl;
    }

my_String.Is_Full((;应该是假的,但直接变成了真的

    my_String.Is_Full();
    if (true)
    {
        cout << "It is full" << endl;
    }
    else if (false)
    {
        cout << "It is not full" << endl;
    }
    return 0;
}
my_String.Is_Full();
if (true)
{
    cout << "It is full" << endl;
}

这是不正确的:您调用is_Full((,它返回false,但您没有使用返回值。然后你检查true是否是真的,很明显是真的。

你应该改为:

if (my_String.Is_Full())
{
    cout << "It is full" << endl;
}
else
{
    ...
}

关于编译器警告,在这些情况下,函数根本不返回,您应该用简单的else替换"else-if",或者在条件作用域之外添加返回语句。

您并没有处理好所有的案例。

template <class New_Type>
bool Array_Class<New_Type>::Is_Full()
{
    if (count == SIZE)
    {
        return true;
    }
    else if (count < SIZE)
    {
        return false;
    }
}

但计数>尺寸怎么样

您可能想将其更改为if ( count >= SIZE )或添加其他块

你在这个区块中有类似的问题

bool Array_Class<New_Type>::Is_Empty()
{
    if (count == 0)
    {
        return true;
    }
    else if (count > 0)
    {
        return false;
    }
}