簡體   English   中英

不是所有的控制路徑都返回布爾值?

[英]Not all control paths return a value for Boolean?

// 我正在使用一個布爾函數,它返回一個 false 和一個 true,但主要不是 //picking 它。 my_string.Is_full 和 my_string.Is_empty 假設說“它不是 //full”和“它不是空的。錯誤的語法?

#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) << " bytes\n\n";
    count = 0;
    A = new New_Type[SIZE];
}

template <class New_Type>
Array_Class<New_Type>::~Array_Class()
{
    cout << "The Destructor has been called.\n\n";
    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 到這里,它確實得到 False

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

//my_String 到這里,它確實得到 False

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 empty\n" << endl;
    }

my_String.Is_Empty(); 應該是假的,但直接變成真的

    my_String.Is_Empty();
    if (true)
    {
        cout << "It is empty" << endl;
    }
    else
    {
        cout << "It is not empty\n" << 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 是否為 true,很明顯。

你應該這樣做:

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

關於編譯器警告,它們是您的函數根本不返回的情況,您應該用簡單的 else 替換“else if”,或者在條件范圍之外添加 return 語句。

你沒有處理所有的案件。

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

}

但是 count > SIZE 怎么樣

您可能希望將其更改為if ( count >= SIZE )或添加一個 else 塊

您在此塊中遇到了類似的問題

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM