Using header file with class

I have a main cpp file where I want to use a class from another cpp file. The other cpp file, console.cpp, has a class with a constructor and a destructor. I want to use the class from console.cpp in main.cpp but I don't know how. I tried making a header file but I was very confused.

main.cpp:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "console.h"

int main()
{
    console con;
    con(50, 50);
    std::cin.get();
    return 0;
}


console.cpp:
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
#include <windows.h>
class console{
public:
    HANDLE                     hstdout;
    CONSOLE_SCREEN_BUFFER_INFO csbi;

    //creating the console
    console(unsigned width, unsigned height)
    {
        SMALL_RECT  r;
        COORD       c;
        hstdout = GetStdHandle( STD_OUTPUT_HANDLE );

        r.Left   = 0;
        r.Top    = 0;
        r.Right  = width - 1;
        r.Bottom = height - 1;
        SetConsoleWindowInfo( hstdout, TRUE, &r );

        c.X = width;
        c.Y = height;
        SetConsoleScreenBufferSize( hstdout, c );
        }

    ~console()
    {
    SetConsoleTextAttribute(    hstdout,        csbi.wAttributes );
    SetConsoleScreenBufferSize( hstdout,        csbi.dwSize      );
    SetConsoleWindowInfo(       hstdout, TRUE, &csbi.srWindow    );
    }
};


console.h:
1
2
3
4
#ifndef CONSOLE_H
#define CONSOLE_H
//here I don't know what to put :c
#endif 
Last edited on
Since the code in console.cpp is inline you don't need two files. Copy the code from console.cpp into console.h and get rid of console.cpp.
Thomas is correct, but since I already copied this to make sure it worked, I might as well just post it:

You should put the declaration of the console class and any functions you want inside the console.h header file, not the console.cpp file.

Then, the console.cpp should #include the console.h file.

You don't actually *need* to put anything in console.cpp, since you have your class declaration with inline functions, but it's good practice to separate the implementation of the functions from the declaration of the functions.

console.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#ifndef CONSOLE_H
#define CONSOLE_H

#include <windows.h>

class console{
  public:
    HANDLE                     hstdout;
    CONSOLE_SCREEN_BUFFER_INFO csbi;

    //creating the console
    console(unsigned width, unsigned height);

    ~console();
};

#endif  


main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "console.h"

int main()
{
    console con(50, 50); // NOTICE: the fix here. You must use the 2x constructor you defined.
    // Because you didn't define a default constructor
    
    std::cin.get();
    return 0;
}


console.cpp
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

#include <windows.h>

#include "console.h"

console::console(unsigned width, unsigned height)
{
    SMALL_RECT  r;
    COORD       c;
    hstdout = GetStdHandle( STD_OUTPUT_HANDLE );

    r.Left   = 0;
    r.Top    = 0;
    r.Right  = width - 1;
    r.Bottom = height - 1;
    SetConsoleWindowInfo( hstdout, TRUE, &r );

    c.X = width;
    c.Y = height;
    SetConsoleScreenBufferSize( hstdout, c );
}

console::~console()
{
    SetConsoleTextAttribute(    hstdout,        csbi.wAttributes );
    SetConsoleScreenBufferSize( hstdout,        csbi.dwSize      );
    SetConsoleWindowInfo(       hstdout, TRUE, &csbi.srWindow    );
}
Thanks both of you!
Topic archived. No new replies allowed.