Matti Mazur
Matti Mazur

Reputation: 83

I can't get second class in C++ Code::Blocks to work

I just started learning C++ and currently I'm trying to create new namespaces in 2 different classes.

But I can't seem to add second class to my project in CodeBlocks, even though I think I included everything properly, it just looks like compiler ignores the second class and cant import the namespace I created.

Here is the code:

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

using namespace std;

using namespace pokemons;

int main()
{

Pokemon pikachu("Pikachu", 1);

pikachu.pokeAttack();




return 0;
}

Source file:

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

}

Header file:

#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

The first class and namespace works perfectly fine so I'm not including it here. Don't mind the pokemon, I just didn't know what to use for training. Oh, and here is the error 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)) ===|

Adding the first class: source file 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;
    }
}


}

The header file 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

Upvotes: 0

Views: 164

Answers (1)

BufferOverflow
BufferOverflow

Reputation: 543

You have Pokemon class in both namespaces, so you need to make it clear for the compiler which namespace you are referring to.

pokemons::Pokemon
poki2::Pokemon

And you use also the same macro name for both animals.h and Pokemon.h

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

In this case, the Pokemon.h will be included first and the POKEMON_H macro will be defined, so when the animals.h is included, everything between #ifndef POKEMON_H and #endif will be dropped.

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

When it comes to you main.cpp file, you have included both header files, but you are only using pokemons namespace.

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

If you want to use both namespaces, the best way is to spesify it in the declaration of the variable and drop 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

Upvotes: 3

Related Questions