zoukankan      html  css  js  c++  java
  • C++文件错误| 'strcmp' was not declared in this scope|

    今天编写C++代码时,出现了错误,如下:

    'strcmp' was not declared in this scope

    代码部分如下:

    #include<iostream>
    #include<string>
    using namespace std;


    //抽象产品类 男人
    class Man
    {
    public:
    virtual void makeM() = 0;
    };
    //具体产品类 白色男人
    class WhiteMan : public Man
    {
    public:
    void makeM()
    {
    cout << "I'm a white man."<< endl;
    }
    };
    //具体产品类 黄色男人
    class YellowMan : public Man
    {
    public:
    void makeM()
    {
    cout << "I'm a yellow man." << endl;
    }
    };
    //具体产品类 黑色男人
    class BlackMan : public Man
    {
    public:
    void makeM()
    {
    cout << "I'm a black man." << endl;
    }
    };

    while (strcmp(t.c_str(), e.c_str()) != 0)
        {
            cout << "请输入标识符 ( 首字母大写 ):";
            cin >> t;
            if (strcmp(t.c_str(), y.c_str()) == 0)
            {
                man_y->makeM();
                woman_y->makeW();
            }
            else if (strcmp(t.c_str(), w.c_str()) == 0) {
                man_w->makeM();
                woman_w->makeW();
            }
            else if (strcmp(t.c_str(), b.c_str()) == 0) {
                man_b->makeM();
                woman_b->makeW();
            }
            else if (strcmp(t.c_str(), e.c_str()) == 0) {   //输入exit退出系统
                cout << "退出" << endl;
            }
            else {
                cout << "输入的标识符有误,请重新输入!" << endl;
            }
            cout << endl;
        }

    经过在网上查询得知:

    1.必须再加上#include <string.h>才能正确编译执行,即同时存在 

    #include<iostream>
    #include<string>
    #include <string.h>
    using namespace std;

    也就是说strcmp不在C++标准库中,需要单独包含strcmp所在的头文件。

    2.或者加上#include <cstring> ,即c的标准库中也包含这个函数

    #include <cstring>  
    using namespace std;

  • 相关阅读:
    LINUX开发使用的3个远程工具
    NDK 链接第三方静态库的方法
    GMap.NET 显示GIF图标的定制
    从MySQL获取数据
    web2py远程开发
    升级后,使用dynamic报错
    实用的MVVM:ImageView
    node.js 连接 mysql
    vector常见用法
    XCODE中配置使用boost
  • 原文地址:https://www.cnblogs.com/mjhjl/p/15368336.html
Copyright © 2011-2022 走看看