Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: 'x' does not name a type

When I try to declare an instance of my class 'Game' I receive the compile error "error: 'Game' does not name a type" for main.cpp.

If probably doesn't matter but i'm using codeblocks.

Relevant code from Game.cpp

#include "../include/main.h"

class Game
{
    private:

    public:
};

Relevant code from Main.cpp

#include "../include/main.h"

Game g; //this is the line it is referring to

int main(int argc, char* args[])
{
    return 0;
}

I'm only starting to learn c++ so i probably overlooked something obvious :(

like image 382
mcjohnalds45 Avatar asked Dec 10 '25 19:12

mcjohnalds45


1 Answers

Include the declaration for "Game" in a header

notepad main.h =>

#ifndef MAIN_H
#define MAIN_H

class Game
{
    private:
      ...
    public:
      ...
};
#endif
// main.h

notepad main.cpp =>

#include "main.h"

Game g; // We should be OK now :)

int 
main(int argc, char* args[])
{
    return 0;
}

gcc -g -Wall -pedantic -I../include -o main main.cpp

Note how you:

1) Define your classes (along with any typedefs, constants, etc) in a header

2) #include the header in any .cpp file that needs those definitions

3) Compile with "-I" to specify the directory (or directories) containing your headers

'Hope that helps

like image 99
paulsm4 Avatar answered Dec 13 '25 08:12

paulsm4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!