Sean S Sean S - 1 month ago 7
C++ Question

"[Classname] does not name a type" even after including .h file

I get this compiler error("CLASSNAME does not name a type") on every function prototype and fucntion in my .cpp file

.h file:

#ifndef MOVIETREE_HPP
#define MOVIETREE_HPP

struct MovieNode
{
...//members and such
};

class MovieTree
{
public:
MovieTree();
~MovieTree();
void printMovieInventory();
...//more functions

protected:
private:
void printMovieInventory(MovieNode * node);
MovieNode* search(std::string title);
MovieNode *root;

};
#endif // MOVIETREE_HPP


.cpp file:

#ifndef MOVIETREE_HPP
#define MOVIETREE_HPP

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

#include "MovieTree.hpp"

using namespace std;

MovieTree::MovieTree(); //error code here and all prototypes and functions below
MovieTree::~MovieTree();
void MovieTree::printMovieInventory();
...//more function prototypes

MovieTree::MovieTree(){
}
MovieTree::~MovieTree(){
}

void MovieTree::printMovieInventory(){
...//body
}
...//more function bodies

#endif


All other forums and questions I've encountered regarding this has the simple solution of including the header file. I've already included that in my code.

I've triple checked and am pretty I spelled everything right. What have I done wrong?

Answer

The problem is these lines in the .cpp file:

#ifndef MOVIETREE_HPP
#define MOVIETREE_HPP
...
#endif

These lines should only be put in the header file, not the program file -- they're used by the header to detect if it's included twice so it doesn't try to redefine everything. By setting it yourself before the #include <movietree.hpp> line, you fool it into thinking that it has already been included, so it doesn't do anything.