2017-01-31 3 views
-1

をチャーないようにと言い変換エラー、ヘッダファイルそれは "char型*" からの変換はこれが私の.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; 
} 
を呼び出そうとしています

問題の可能性がある人は誰でも見つかりますか?

+0

あなたは 'char'と文字列の違いについて混乱しています。 'char'は単一の文字(例えば、文字)です。 – aschepler

+0

ああ、char nameOfKingdomは1文字しか受け入れることができないことを意味しますか?どのタイプを使用するのですか? –

+0

ascheplerが言ったことの大きなヒント: "charと文字列の違い[。]"この文の2つの型は、すでに 'char'が動作していないことを発見しました。 – user4581301

答えて

2

pKingdom[i].m_namechar[32]nameOfKingdomのタイプはcharです。文字配列と文字を比較することはできません。私は、使用するタイプ

std::string

+0

タイプの問題を修正しましたが、今はそれが望むように動作しません。ヴェイルは配列内にあり、このpKingdom [i] .m_name << "、population" << pKingdom [i] .m_population << endlのように印刷されるべきであるので、ウェールズの部分になければなりません。あたかも配列の中に存在しないかのようにプリントアウトします。だから私は条件が何らかの問題を抱えていると推測しています。助けてもらえますか? –

+0

@GodCoder私はあなたが['std :: string'](http://en.cppreference.com/w/cpp/string/basic_string)を使うための@ user2079303のアドバイスに従わなかったと思います。結果としてあなたは住所を比較しています。そして、私は '' new [] 'の代わりに[' std :: vector'](http://en.cppreference.com/w/cpp/container/vector)を使用します。 –

+0

なししかし、私は、しかし、ボイド表示を文字列を使用しました(王国の王国[]、int型kingdomElement、文字列nameOfKingdom)と、それはまだ –

関連する問題