organizing class files

This is the continuing problem I have been having in C++, I use the CodeBlocks IDE, I am trying to get class files seperated into header files, the only example I have seen in my book shows the class declaration in a header file (.h or .hpp) while all its functions and constructor are in the main file. Whenever I make a new class file in Codeblocks it creates a header and .cpp file for it, but I dont know how to correctly format the .cpp file or how to correctly #include it in my main... Can someone explain how to do so, with code below?

here is code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
class Cat
{
    public:
        Cat(int initialAge);
        ~Cat();
        int getAge();
        void setAge(int age);
        void meow();
    private:
        int cAge;
};
Cat::Cat(int initialAge)
{
    cAge = initialAge;
}
Cat::~Cat() {}
int Cat::getAge()
{
    return cAge;
}
void Cat::setAge(int age)
{
    cAge = age;
}
void Cat::meow()
{
    std::cout<<"Meow.\n";
}
int main()
{
    using namespace std;
    Cat frisky(5);
    frisky.meow();
    cout<<"Frisky is a cat who is "<<frisky.getAge()<<" years old.\n";
    frisky.meow();
    return 0;
}
You could split the project into three files.

The original file has:
1
2
3
4
5
6
7
8
9
10
11
12
#include "Cat.h"
#include <iostream>

int main()
{
    using namespace std;
    Cat frisky(5);
    frisky.meow();
    cout<<"Frisky is a cat who is "<<frisky.getAge()<<" years old.\n";
    frisky.meow();
    return 0;
}

Cat.h has:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef CAT_H
#define CAT_H

class Cat
{
    public:
        Cat(int initialAge);
        ~Cat();
        int getAge();
        void setAge(int age);
        void meow();
    private:
        int cAge;
};

#endif 


Cat.cpp has:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "Cat.h"
#include <iostream>

Cat::Cat(int initialAge)
{
    cAge = initialAge;
}

Cat::~Cat()
{
}

int Cat::getAge()
{
    return cAge;
}

void Cat::setAge(int age)
{
    cAge = age;
}

void Cat::meow()
{
    std::cout<<"Meow.\n";
}
Last edited on
so whats the purpose of the #ifndef, #define, and #endif, I mean it kinda seems self evident to be a definition of a class... but why don't you have to include the Cat.cpp in the main? Is it... inherited from the cat.h when u include it?
#ifndef, #define, and #endif is used here to create an include guard. It prevents the header content from being expended if the header file is included more than once.

You should never include .cpp files. Instead all the .cpp file code should be linked together by the linker (that runs after the compiler). If you use an IDE and have a project it will probably link the files automatically.
i put all these in there word for word and im still getting the same errors:

Cat.h: No such file or directory

'Cat' has not been declared

ISO C++ forbids declaration of 'Cat' with no type

what should I do?
Last edited on
Did you create the file Cat.h?
yes I think so, do you use codeblocks?
No I don't use Code::Blocks.

The first error states the problem. It can't find Cat.h. The two other errors are a consequence of the first error.

Did you create the files in the same project as the rest of your files from within Code::Blocks? If you created Cat.h in some other location than the rest of your files it will probably not find it.
no they are in the same project, this project is made of 2 folders sources and headers. sources contains main.cpp and Cat.cpp while header contains Cat.h, but they are all in the same project. Also which IDE do you use???
The two directories "sources" and "headers" are created automatically when you create the project, right? I'm sorry, I don't know why it doesn't work.

Also which IDE do you use???
I use Geany.
is there any way i can post a picture or something and show you my code as it is in the IDE?

this is it word for word:
main.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include "Cat.h"
#include <iostream>
int main()
{
    using namespace std;
    Cat frisky(5);
    frisky.meow();
    cout<<"Frisky is a cat who is "<<frisky.getAge()<<" years old.\n";
    frisky.meow();
    return 0;
}


Cat.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Cat.h"
#include <iostream>

Cat::Cat(int initialAge)
{
    cAge = initialAge;
}
Cat::~Cat() {}
int Cat::getAge()
{
    return cAge;
}
void Cat::setAge(int age)
{
    cAge = age;
}
void Cat::meow()
{
    std::cout<<"Meow.\n";
}*/


Cat.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef CAT_H
#define CAT_H


class Cat
{
    public:
        Cat(int initialAge);
        ~Cat();
        int getAge();
        void setAge(int age);
        void meow();
    private:
        int cAge;
};

#endif // CAT_H 
Last edited on
I FOUND OUT WHAT WAS WRONG!! It kind of figures but the problem was... well it couldnt find the file, as in I replaced:

#include "Cat.h"

with the entire file path

#include "E:\C++\Chapter10\include\Cat.h"

It will be really annoying in the future if i have to rediclare it on every computer, is there a way to fix that, as in to get #include "Cat.h" to work?
Put it with your project and it should all work
i think it was allways in the project... yea im sure, but im wondering if theres a way to get relative file names like Cat.h to work, a function like in java mabye?
All three files ought to be in the same directory. If you do that you won't have a problem. If you move the header file to a different directory form the .cpp files, your environment needs to be configured to find it.
so the entirety of the #include files has to be in the same file? there is absolutely no function or method that can retrieve its URL for you?
so the entirety of the #include files has to be in the same file?
I am not sure what you mean. You have three files. One is the original .cpp file, and you ought to have created to new files, Cat.h and Cat.cpp. These two new files should be in the same directory as the original .cpp file.

To be honest, I would have expected you to place the two new files in the same directory as the original file. I mean, why would you put them elsewhere?

there is absolutely no function or method that can retrieve its URL for you?
Again, I am not sure what you mean by this either. The include file rules say that if the header is included with "" (as in #include "Cat.h" ), the compiler will look in the same directory as the source file, and if it doesn't find it, down the include path. If it's included with <> (as in #include <Cat.h> ), the compiler will not look in the same directory as the source file, but down the include path.

Typically, libraries are specified with <> (as in #include <iostream> ) and "" for local project files.
Last edited on
Topic archived. No new replies allowed.