我刚开始学习C ++,目前正在尝试在2个不同的类中创建新的名称空间。
但是我似乎无法在CodeBlocks中的项目中添加第二个类,即使我认为我正确地包含了所有内容,也好像编译器忽略了第二个类并且无法导入我创建的名称空间。
这是代码:
#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
最佳答案
在两个名称空间中都具有Pokemon
类,因此需要向编译器明确说明要引用的名称空间。
pokemons::Pokemon
poki2::Pokemon
而且您对
animals.h
和Pokemon.h
使用相同的宏名称#ifndef POKEMON_H
#define POKEMON_H
[...]
#endif
在这种情况下,将首先包含
Pokemon.h
,并且将定义POKEMON_H宏,因此,当包含animals.h
时,将删除#ifndef POKEMON_H
和#endif
之间的所有内容。#include "Pokemon.h"
#include "animals.h"
当涉及到
main.cpp
文件时,您已经包含了两个头文件,但是您仅使用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;
}
如果要同时使用两个名称空间,最好的方法是在变量的声明中将其spiseize并删除
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
关于c++ - 我无法在C++ Code::Blocks中获得第二类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36817593/