2016-04-23 3 views
0

私はちょうどC++を学び始めました。現在は2つの異なるクラスで新しい名前空間を作成しようとしています。私はC++で2番目のクラスを取得できません:: ::ブロックが動作する

しかし、私はすべてのコードが適切に含まれていると思っていますが、コンパイラが2番目のクラスを無視して作成した名前空間をインポートできないように見えますが、CodeBlocksの2番目のクラスをプロジェクトに追加できないようです。

#include <iostream> 
#include "Pokemon.h" 
#include "animals.h" 

using namespace std; 

using namespace pokemons; 

int main() 
{ 

Pokemon pikachu("Pikachu", 1); 

pikachu.pokeAttack(); 




return 0; 
} 

ソースファイル:

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

using namespace std; 

namespace pokemons{ 

Pokemon::Pokemon() 
{ 
cout << "I choose you, " << name << endl; 
} 

Pokemon::~Pokemon() 
{ 
cout << "Come back to Pokeball, " << name << endl; 
} 

Pokemon::Pokemon(string name, int type){ 
     this->name = name; 
     this->type = type; 

     cout << "I choose you, " << name << "!!!" << endl; 
    } 

void Pokemon::pokeAttack(){ 
if (type = 1){ 

    cout << name << " used tackle" << endl; 
} 
} 

} 

ヘッダファイル:

#ifndef POKEMON_H 
#define POKEMON_H 

#include <string> 

namespace pokemons{ 


class Pokemon 
{ 
public: 
    Pokemon(); 
    virtual ~Pokemon(); 
    void pokeAttack(); 
    Pokemon(std::string name, int type); 
protected: 
private: 
    std::string name; 
    int type; 
}; 

} 

#endif // POKEMON_H 

ファーストクラスと名前空間はので、私は含めていないよ完全に正常に動作します。ここ

コードですそれはここにある。ポケモンに気にしないで、私はちょうどトレーニングのために何を使うべきか分からなかった。 ああ、ここでエラーがhttp://prntscr.com/aw12nj

||=== Build: Debug in namespacesTrening (compiler: GNU GCC Compiler) ===| 
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp|7|error: 'pokemons' is not a namespace-name| 
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp|7|error: expected namespace-name before ';' token| 
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp||In function 'int main()':| 
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp|12|error: 'Pokemon' was not declared in this scope| 
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp|12|note:  suggested alternative:| 
include\Pokemon.h|9|note: 'poki2::Pokemon'| 
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp|12|error: expected ';' before 'pikachu'| 
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp|14|error: 'pikachu' was not declared in this scope| 
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| 

あるファーストクラスの追加: ソースファイルをPokemon.cpp

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



namespace poki2{ 

Pokemon::Pokemon() 
{ 
    cout << "I choose you, " << name << endl; 
} 

Pokemon::~Pokemon() 
{ 
    cout << "Come back to Pokeball, " << name << endl; 
} 

Pokemon::Pokemon(string name, int type){ 
      this->name = name; 
      this->type = type; 

      cout << "I choose you, " << name << "!!!" << endl; 
     } 



void Pokemon::pokeAttack(){ 
    if (type = 1){ 

     cout << name << " used thunderbolt!!!" << endl; 
    } 
} 


} 

ヘッダファイルPokemon.h

#ifndef POKEMON_H 
#define POKEMON_H 

#include <iostream> 
using namespace std; 

namespace poki2 { 

class Pokemon 
{ 
    public: 
     Pokemon(); 
     virtual ~Pokemon(); 
     Pokemon(string name, int type); 
     void pokeAttack(); 

    protected: 
    private: 
     string name; 
     int type; 
}; 

} 



#endif // POKEMON_H 
+0

をドロップすることです。それを別の場所に置くことは壊れやすくなります。 – Carcigenicate

+0

おそらく、実装ファイル 'pokemon.cpp'に' #include "Pokemon.h" 'が必要です。 – ArchbishopOfBanterbury

+0

あなたが正しくあなたを理解していれば、それらは含まれていて、働いています –

答えて

3

あなたはPokemonを持っていますクラスの両方の名前空間で使用するため、あなたが参照している名前空間のコンパイラを明確にする必要があります。

pokemons::Pokemon 
poki2::Pokemon 

そして、あなたはanimals.hPokemon.hこの場合

#ifndef POKEMON_H 
#define POKEMON_H 
[...] 
#endif 

の両方で同じマクロ名、Pokemon.hが最初に含まれることになるとPOKEMON_Hマクロが定義されますので、animals.hがあるときにも使用します含まれている場合、#ifndef POKEMON_H#endifの間のものはすべて削除されます。それはあなたmain.cppファイルに来るとき

#include "Pokemon.h" 
#include "animals.h" 

、あなたは両方のヘッダファイルが含まれている、しかし、あなただけのpokemons名前空間を使用しています。

#include <iostream> 
#include "Pokemon.h" 
#include "animals.h" 

using namespace std; 

using namespace pokemons; // You have selected pokemons namespace. 

int main() 
{ 
    Pokemon pikachu("Pikachu", 1); 

    pikachu.pokeAttack(); 

    return 0; 
} 

あなたは、両方の名前空間を使用したい場合は、最良の方法は、ここで直接エラーテキストを貼り付けてください、変数の宣言でそれをspesifyとusing namespace

pokemons::Pokemon pikachu("Pikachu", 1); // Instance of Pokemon class in namespace pokemons 
poki2::Pokemon pikachu2("Pikachu", 1) // Instance of Pokemons class in namespace poki2