简体   繁体   中英

How to use header files for classes?

I'm having trouble creating an OOP version of Hello World. How would I add the World object definition to my header?

Main.cpp:

#include "stdafx.h"
#include "World.h"
#include <iostream>

int main() {
    World world;
    World world2(5);
    std::cin.get();
    return 0;
}

World.cpp:

#include "stdafx.h"
#include <iostream>

class World {
public:
    World() {
        std::cout << "Hello World!" << std::endl;
    }

    World(int test) {
        std::cout << "Hello World!" << std::endl;
    }

    ~World() {
        std::cout << "Good Bye!" << std::endl;
    }
};

World.h:

class World;
class World(int);

Errors:

1>d:\programming\c++\consoleapplication1\consoleapplication1\world.h(2): error C2062: type 'int' unexpected
1>d:\programming\c++\consoleapplication1\consoleapplication1\main.cpp(6): error C2079: 'world' uses undefined class 'World'
1>d:\programming\c++\consoleapplication1\consoleapplication1\main.cpp(7): error C2079: 'world2' uses undefined class 'World'

Define your class in the header file. You should also definitely use an include guard , to avoid problems when several other files include the same header (not a problem with your current example, but still, it's a very good practice):

// This is the include guard.
#ifndef WORLD_H
#define WORLD_H

class World {
public:
    World();
    World(int test);
    ~World();
};

#endif

And define the member functions of that class in the cpp file:

#include "World.h"
#include <iostream>

World::World()
{
    std::cout << "Hello World!" << std::endl;
}

World::World(int test)
{
    std::cout << "Hello World!" << std::endl;
}

World::~World()
{
    std::cout << "Good Bye!" << std::endl;
}

However, you can also define both the class as well the member functions directly in the header file, if you so wish. In that case, you do not need a .cpp file for your class at all. In order to do that, just delete your current World.h header and rename World.cpp to World.h (and add the include guard, as recommended previously.)

Finally, there's a third way you can do this, which also only needs a header file and no .cpp file, where you define the member functions inside the header, but after the class definition by making use if the inline keyword:

#ifndef WORLD_H
#define WORLD_H

#include <iostream>

class World {
public:
    World();
    World(int test);
    ~World();
};

inline World::World()
{
    std::cout << "Hello World!" << std::endl;
}

inline World::World(int test)
{
    std::cout << "Hello World!" << std::endl;
}

inline World::~World()
{
    std::cout << "Good Bye!" << std::endl;
}

#endif

This is useful when you don't want the interface of your class to be difficult to read, but still want to be able to do without a .cpp file. Note, however, that not using a .cpp file can increase compilation times when you build a project that includes the header in several different files, and will also trigger a rebuild of all those files if you make even the slightest change in the implementations of the member functions in that header file. Because of this, having a .cpp file for each class is a good idea most of the time, since you can edit that freely without it triggering a rebuild of every other source file that includes the header.

Your header should look like this

#ifndef CLASS_World
#define CLASS_World
class World
{
private:
    int m_nValue;

public:
    World()
    int World(int value) 

#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM