我无法在C ++ Code :: Blocks中获得第二课

时间:2016-04-23 23:25:01

标签: c++ codeblocks

我刚刚开始学习C ++,目前我正在尝试在两个不同的类中创建新的命名空间。

但我似乎无法在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

1 个答案:

答案 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之间的所有内容都将被丢弃

#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;
}

如果你想同时使用这两个命名空间,最好的方法是在变量声明中加注它并删除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