我正在为课程分配作业,并且在编译此代码时遇到问题。我在主.cpp文件和类.cpp文件中都使用了#include <string>
。错误是“'strcmp'不是'std'的成员”,但是无论使用std :: strcmp()还是strcmp()都可以理解。
对我在做什么错有任何想法吗?
double temporary::manipulate()
{
if(!std::strcmp(description, "rectangle"))
{
cout << "Strings compare the same." << endl;
return first * second;
}
return -1;
}
最佳答案
您需要分别为<string.h>
或<cstring>
包括strcmp
或std::strcmp
。 <string>
是std:string
和其他相关功能所需的C ++标准库。
请注意,std::strcmp
需要两个const char*
,而不是std::string
。如果description
是std::string
,则可以使用c_str()
方法获取指向基础数据的指针:
if(!std::strcmp(description.c_str(), "rectangle"))
或者只是使用比较运算符。这是更惯用的解决方案:
if(description == "rectangle")
关于c++ - C++在类中对私有(private)成员数据使用strcmp(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21759751/