I'm a beginner in C++, the following example code is very simple but I'm getting a puzzling error. Field has incomplete type "ItemType"
The code is from Data Abstraction & Problem Solving with C++ by Frank Carrano. Pg. 32 - 33, Pg. 37
I'm using Xcode 6.0 (6A267n)
on OSX Yosemite Beta 10.10
In header file (Listing C1-03)
/** @file PlainBox.h */
#ifndef __PlainBox__
#define __PlainBox__
//Indicates this is a template definition
template<class ItemType>
// Declaration for the class PlainBox
class PlainBox
{
private:
// Data field
ItemType item; //error here
public:
// Default constructor
PlainBox();
// Paramerized constructor
PlainBox(const ItemType& theItem);
// Method to change the value of the data field
void setItem(const ItemType& theItem);
// Method to return item form the data field
ItemType getItem() const;
}; // end PlainBox
#include "PlainBox.cpp"
#endif /* defined(__PlainBox__PlainBox__) */
In cpp file (listing C1-04)
/** @file PlainBox.cpp */
#include "PlainBox.h"
template<class ItemType>
PlainBox <ItemType> :: PlainBox ()
{
} //end default constructor
template<class ItemType>
PlainBox <ItemType> ::PlainBox(const ItemType& theItem)
{
item = theItem;
} //end constructor
template<class ItemType>
void PlainBox <ItemType> ::setItem(const ItemType &theItem)
{
item = theItem;
} //end setItem
template<class ItemType>
ItemType PlainBox <ItemType> ::getItem() const
{
return item;
} //end getItem
EDIT: Turns out I have to remove the .cpp file form the Xcode project and remove the #include
in the .cpp file.