Hey guys,

I come over from C, wanting to learn C++. I have a nice little book but figured I could use a project to get me going.

So I magicly crafted code, and ran into this problem: C:\Code\Rapture\World.h|7|error: ISO C++ forbids declaration of `Breeture' with no type| With this code:

Breeture.h:

#ifndef BREETUREH
#define BREETUREH

#include <vector>
#include "World.h"
class Breeture{
    friend class World;

    public:
    //Creates the breeture
    Breeture();

    //Pwns the breeture.
    void Die();
    ~Breeture();

    void Age();

    void outputDNA();
    void outputAge();

    void outputData();

    private:
    bool alive;
    unsigned int number;

    unsigned int age;
    unsigned int DNA[2];
    unsigned int causeofdeath;

    unsigned int kids;

    unsigned int deathChance;

    vector<*Breeture> parents;
    vector<*Breeture> children;
    vector<*Breeture> partners;
};
#endif

World.h:

#ifndef WORLDH
#define WORLDH
#include <vector>
#include "Breeture.h"
class World{
    public:
    void NewBreeture(const Breeture *const baby);
    void BaiBreeture(const Breeture *const deceased);

    /*This function ages all living breetures*/
    void AgeBreetures();

    /*Does a death-check. World makes Breetures die.*/
    void DeathCheck();

    /*World also makes breetures mate.*/
    void BirthCheck();

    //int StoreFamilyTree(string *location);

    private:
    unsigned int worldPopulation;
    unsigned int totalBreetures;


    /* And...
       The oldest breeture
       The breeture with the most direct-offspring*/
    Breeture oldest;
    Breeture mostChildren;

    /*Holds a vector of living creatures, for speedy aging*/
    vector<*Breeture> livingBreetures;

    /*Holds a tree of vector describing parent/child relationships*/
    vector<*Breeture> familyTree;
    */
};
#endif

Why doesn't World.h know about the breeture class?

Thanks in advance,
Nick

Recommended Answers

All 8 Replies

Breeture.h includes world.h
world.h includes Breeture.h
and so on....

You can't have two classes containing instances of the other class. One of them needs to contain only pointers.

class world;
class Breeture {
  world *w;  // ONLY pointers to worlds in this class
};

and

#include "Breeture.h"
class world {
    Breeture b;  // free to have instances in this class
};

Breeture.h or cpp doesn't have a instance of the world class in it, or you didn't mean to say that? Breeture needs to include the header file of world because it it's his friend, but it doesn't create an instance of world itself.

Breeture.h includes world.h
world.h includes Breeture.h
and so on....

And that won't happen with the #ifndef "protection", if I'm right.

Hmz, this seemed to be the problem:

For being a class's friend, the class that has the friend statement does not need the header file for the friend class/function, apparently.

Thanks.

New problem arose:

Almost the same code as before:

Breeture.h

#ifndef BREETUREH
#define BREETUREH

#include <vector>
class Breeture{
    friend class World;

    public:
    //Creates the breeture
    Breeture();

    //Pwns the breeture.
    void Die();
    ~Breeture();

    void Age();

    void outputDNA();
    void outputAge();

    void outputData();

    private:
    bool alive;
    unsigned int number;

    unsigned int age;
    unsigned int DNA[2];
    unsigned int causeofdeath;

    unsigned int kids;

    unsigned int deathChance;

    vector<*Breeture> parents;
    vector<*Breeture> children;
    vector<*Breeture> partners;
};
#endif

Main.cpp

#include <cstdio>
#include "Breeture.h"

int main(){
    printf("Sizeofbreeture: %d", sizeof(Breeture));
    return 0;
}

Errors while compiling:

C:\Code\Rapture\Breeture.h|35|error: ISO C++ forbids declaration of `vector' with no type|
C:\Code\Rapture\Breeture.h|35|error: expected `;' before '<' token|
C:\Code\Rapture\Breeture.h|36|error: ISO C++ forbids declaration of `vector' with no type|
C:\Code\Rapture\Breeture.h|36|error: expected `;' before '<' token|
C:\Code\Rapture\Breeture.h|37|error: ISO C++ forbids declaration of `vector' with no type|
C:\Code\Rapture\Breeture.h|37|error: expected `;' before '<' token|
||=== Build finished: 6 errors, 0 warnings ===|

And that won't happen with the #ifndef "protection", if I'm right.

I'm not so sure: if file A #includes file B, and file B #includes file A, and file A is #included by the main project first, then this will happen:

file A #includes file B
file B #includes file A (the code within the file will not be looked at, but the #ifndef will still need to be processed)
file A #includes file B
file B #includes file A
and so on...

the chain will never end (if I am correct, which I admit I may not be)

Also, (again not 100% sure), Breeture.h doesn't need to know anything about the class World (it needs to know it exists, but nothing else). so I think you can replace #include "World.h" with class World; (like a prototype).

Once again - I'm not too sure on any of this, but I think this should solve the problem.

Yes, thanks. You are off in the first part, but the second was right. :)

However, now I'm having problems trying to create a vector of pointers to the Breeture class. Compiler complains that *Breeture isn't a type. How would I do this?

Thanks in advance,

Try changing this code in your Breeture class:

vector<*Breeture> parents;
    vector<*Breeture> children;
    vector<*Breeture> partners;

to this:

std::vector<Breeture*> parents;
    std::vector<Breeture*> children;
    std::vector<Breeture*> partners;

That was really stupid. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.