将字符串放入条件 (C++)

put a string in condition (c++)

本文关键字:C++ 条件 字符串      更新时间:2023-10-16

为什么我会收到此行的错误?

void Student::SetName(const string newName)
{
 if(newName!=NULL) //could not deduce template argument for 'const T1 *' from 'int'
{
     .....
}

有什么想法吗?

可能的解决方案:

if(!newName.empty())
if(newName.size()) // If size = 0 so no caracters in string
if(newName == "") // Empty string
这不是

C#,C++ 中的字符串不是可为 null 的类型。只有指针实际上可以是 NULL,除非使用指针,否则您无法在不为变量分配一些基本值的情况下定义变量C++。

您的代码可能如下所示:

if(!newName.empty())
    ....