Proper C++ header file?

Started by
25 comments, last by Clobslee 9 years, 5 months ago

I don't like to ask for help on here more than once a week, but I've not been able to make any progress today because of this random error I'm getting. I've created many classes with their own header file but I have no idea why this will not compile.

TileMap.cpp


#include"TileMap.h"
#include<iostream>
#include<string>
#include<vector>

//----------------------------

#include<SFML/Graphics.hpp>
#include<SFML/Window.hpp>
#include<SFML/System.hpp>

//----------------------------

using namespace std;

//Overload Constructors
TileMap::TileMap(string inName, string inLoc)
{
        setName(inName);
        setLoc(inLoc);
}

//Mutators
void TileMap::setName(string inName){name=inName;}
void TileMap::setLoc(string inLoc){loc=inLoc;}

//Accessors
string TileMap::getName(){return name;}
string TileMap::getLocd(){return loc;}

TileMap.h


#ifndef TILEMAP_H
#define TILEMAP_H

#include <iostream>
#include <vector>
#include <string>

class TileMap()
{
        public:
                TileMap(std::string name, std::string loc);
                void setName(std::string inName);
                void setLoc(std::string inLoc);
                std::string getName();
                std::string getLoc();

        private:
                std::string name;
                std::string loc;
};


#endif

Error


In file included from TileMap.cpp:1:0:
TileMap.h:8:15: error: expected unqualified-id before ‘)’ token
TileMap.cpp:17:45: error: invalid use of incomplete type ‘struct TileMap’
TileMap.h:8:7: error: forward declaration of ‘struct TileMap’
TileMap.cpp:24:36: error: invalid use of incomplete type ‘struct TileMap’
TileMap.h:8:7: error: forward declaration of ‘struct TileMap’
TileMap.cpp:25:34: error: invalid use of incomplete type ‘struct TileMap’
TileMap.h:8:7: error: forward declaration of ‘struct TileMap’
TileMap.cpp:28:25: error: invalid use of incomplete type ‘struct TileMap’
TileMap.h:8:7: error: forward declaration of ‘struct TileMap’
TileMap.cpp:29:25: error: invalid use of incomplete type ‘struct TileMap’
TileMap.h:8:7: error: forward declaration of ‘struct TileMap’

Please inform me of the small error I have been searching for all day... I'm past the point of caring about embarrassment.

Tips?

Advertisement

Where is the compiler output?




class TileMap()

No parentheses at the class definition.

Where is the compiler output?

Hmm...? I'm compiling with gcc from the terminal command line. The only thing being output to the terminal that isn't posted here is the command I used to compile.

g++ -c TileMap.cpp




class TileMap()

No parentheses at the class definition.

Wow. It's funny that something that simple can waste so much of my time... Thank you!

Another thing to verify with the headers is that you class definitions have ; at the end ie. class x{...};

It causes rather ambiguous errors.

Cheers!

Are you using an IDE? I think your issue would have been detected and highlighted by smart syntax correction.

Where is the compiler output?

Hmm...? I'm compiling with g++ from the terminal command line. The only thing being output to the terminal that isn't posted here is the command I used to compile.

g++ -c TileMap.cpp

Ah, now it's there. It wasn't when I posted.

For a proper header file you should not include a bunch of files you do not need. Only add them if you cant forward declare a class and prefer having the include in the cpp file, but minimize these, too.

And use descriptive names where you dont need to guess if, for example, loc is location (cant be, a string location is weird?), lines of code(uhh no, why?) or something else.

It would be good to learn about how to use const references and the constructor initializer list, to see how these prevent useless copying.

Try following the "tell, dont ask" principle and only add get/set methods if you cant replace them by methods that do the real work inside the class. The remaining one-line-methods could then be put into the header so they can be inlined.

Are you using an IDE? I think your issue would have been detected and highlighted by smart syntax correction.

Nope! I have used code blocks and eclipse in the past but I prefer not to use one.. I'm still in school and the few cs professors that we have here seem to hand out paper tests, where I get no IDE or syntax correction. Obviously, something simple like this taking a large portion of time is a drawback.

This topic is closed to new replies.

Advertisement