一个转换错误,它表示没有来自" char *"去char

时间:2017-01-31 00:49:59

标签: c++ type-conversion

这是我的.h,头文件

#ifndef KINGDOM_H_
#define KINGDOM_H_

namespace westeros {

    class Kingdom {
    public:
        char m_name[32];
        int m_population;

    };

    void display(Kingdom pKingdom[], int kingdomElement, char nameOfKingdom);

}
#endif

这是我的.cpp,源文件

#include <iostream>
#include "kingdom.h"
using namespace std;

namespace westeros{


    void display(Kingdom pKingdom[], int kingdomElement, char nameOfKingdom){

        cout << "------------------------------" << endl;
        for (int i = 0; i < kingdomElement; i++) {
            **if(pKingdom[i].m_name == nameOfKingdom){** //it's giving me error right here, visual studio underlining red line below == sign saying operand types are incompatible 
                cout << "Searching for kingdom " << pKingdom[i].m_name << " in Westeros " << endl;
                cout << "------------------------------" << endl;
                cout << pKingdom[i].m_name << ", population " << pKingdom[i].m_population << endl;
            }
            else {
                cout << "------------------------------" << endl;
                cout << "Searching for kingdom " << nameOfKingdom << " in Westeros " << endl;
                cout << "------------------------------" << endl;
                cout << nameOfKingdom << " is not part of Westeros." << endl;
                cout << "------------------------------" << endl;
            }
        }
    }
}

这是我尝试调用的主要文件

#include <iostream>
#include "kingdom.h"

using namespace std;
using namespace westeros;

int main(void)

{

    int count = 0; // the number of kingdoms in the array

    Kingdom* pKingdoms = nullptr;

    //allocating dynamic memory
    pKingdoms = new Kingdom[count];

    display(pKingdoms, count, "Mordor");
    cout << endl;

    display(pKingdoms, count, "The_Vale");
    cout << endl;
    delete[]pKingdoms;
    pKingdoms = nullptr;

    return 0;
}

有人能找到可能出现的问题吗?

1 个答案:

答案 0 :(得分:2)

您的问题是pKingdom[i].m_namechar[32]nameOfKingdom的类型是char。您无法将字符数组与字符进行比较。

  

我会使用哪种类型?

std::string