并非所有控制路径都返回布尔值
Not all control paths return a value for Boolean?
//我使用了一个布尔函数,它返回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;
}
}
相关文章:
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 如何使用返回布尔值的函数?
- 返回布尔值在 cocos2dx 的 onTouchBegan() 中是什么意思?
- 在开关情况下返回布尔值
- 让二传手返回布尔值是好习惯吗?
- 从 lambda 表达式返回布尔值时出错
- 在 if 语句中返回布尔值的 lambda 表达式
- Windows HANDLE RAII 管理,如果返回布尔值而不是句柄怎么办?
- 读者班级游戏板:正确返回布尔值
- 试图返回布尔值时,程序崩溃
- 如何使用 boost::bind 将返回 int 的函数转换为返回布尔值的函数
- 返回布尔值的函数的命名约定是什么
- 函数返回布尔值,只设置整个寄存器的1个字节
- 返回布尔值时,为什么要使用"!!
- 从成员变量指针返回布尔值
- 从 Circle 类返回布尔值 C++
- 返回布尔值
- C++如何使用返回布尔值的函数来确定素数
- 让函数返回布尔值和地址?C++
- 不让我返回布尔值给函数