Brandon
Brandon

Reputation: 103

Can't get header files to work

I'm learning about header files and classes right now, and I can't seem to get them to work. I don't see what I'm doing wrong. I've included the errors as comments where the compiler said there were errors. How do I fix this?

main.cpp

#include <iostream>
#include <conio.h>
#include "Header.h" //Error: In file included from C:\Users\Brandon\Desktop\
                             C++ Practice\Header Practice\Main.cpp 

int main()
{
    Header Test;
    Test.Header(); Error: invalid use of `class Header' 

    getch();
    return 0;
}

Header.h

#ifndef Header_H_
 #define Header_H_

 class Header
{
 public:
        void Header(); //Error: return type specification for constructor invalid 
};

#endif // Header_H_

and Header.cpp.

#include "Header.h"

Header::Header()
{
 std::cout << "Everything is working./n" << std::flush;
};

Upvotes: 0

Views: 214

Answers (2)

stellarossa
stellarossa

Reputation: 1800

remove void from void Header(); in Header.h

in Header.cpp, remove the semicolon:

#include "Header.h"

Header::Header()
{
 std::cout << "Everything is working./n" << std::flush;
}; <------ REMOVE SEMICOLON

in main.cpp - you're calling the constructor wrong by doing this Test.Header().

you should be doing either Header Test; or Header *Test = new Header()

Upvotes: 0

igleyy
igleyy

Reputation: 605

Remove void from void Header(); line. Constructors are declared without return type.

Upvotes: 0

Related Questions