私はちょうど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
をドロップすることです。それを別の場所に置くことは壊れやすくなります。 – Carcigenicate
おそらく、実装ファイル 'pokemon.cpp'に' #include "Pokemon.h" 'が必要です。 – ArchbishopOfBanterbury
あなたが正しくあなたを理解していれば、それらは含まれていて、働いています –