0

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" Error

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.

Xcode drop down menu Remove Reference

tomtclai
  • 333
  • 6
  • 18
  • Not related to your actual problem, but `__PlainBox__PlainBox__` is a reserved identifier and you shouldn't really include `stdio.h` in C++ code. What command are you using to compile this code? And paste the full set of compiler error messages. – T.C. Sep 25 '14 at 22:55
  • 1
    When do you get this error? Does compiling the above code give you the error or do you need to use the class template to get the error. BTW, if this is code copied from a book meant to teach you C++ I'd recommend you get a different book: it contains multiple more or less severe issues you want to avoid in real code. For example, when instantiated with a built-in type, `item` goes uninitialized in the constructor, the other constructor unnecessarily first constructs `item` just to assign over it, it uses reserved names, and in my opinion use of `` has rarely a place in C++. – Dietmar Kühl Sep 25 '14 at 23:41
  • I'm compiling this code on Xcode, command + R. I'm getting this error as soon as I typed it. Xcode shows a red flag on that line. – tomtclai Sep 26 '14 at 18:48
  • I cut and pasted the contents of the two files (except the parts with `__PlainBox__PlainBox__`) to http://www.compileonline.com/compile_cpp_online.php --this compiles the code with no problem, and I was even able to instantiate a `PlainBox` in my `main()` function and print its value. – David K Sep 26 '14 at 20:31
  • 1
    Might there be a problem with the fact that you've included the .cpp file in the .h file, and then included the .h file in the .cpp file? It appears to me that the .cpp file ends up containing two copies of all the function implementations. – David K Sep 26 '14 at 20:33
  • This seems either Xcode specific or has to do with separate compilation. I used the website and it compiled on the website. I have included the Listings from the support website for the book. I tried moving all the code to header file and I see the same error still on Xcode – tomtclai Sep 26 '14 at 20:58
  • Turns out I had to remove the reference to cpp file from Xcode, seems odd because then I can't see the .cpp file within Xcode anymore. David was right about not including .h file in the .cpp file. – tomtclai Sep 28 '14 at 15:29

1 Answers1

0

This link leads me to believe you should try putting everything in the header. Though other threads even linking that article say otherwise, it's worth a try.

Also, this sounds silly, but try moving the template keyword to directly before the class keyword (take out the line of comments). It didn't matter using the g++ compiler, but I know that some vcc versions are picky about that, and maybe even the LLVM. Something like

template <typename T>
class myclass{
}

or even

template <typename T> class myclass{
}
Community
  • 1
  • 1
Dan
  • 51
  • 1
  • 4
  • If I `#include` the .cpp file in the .h file, and then remove the reference to .cpp file from Xcode, then it works. The key point is that I had to remove it from Xcode without removing the file. Otherwise it complains about unidentified names. – tomtclai Sep 28 '14 at 15:33
  • Weird. Maybe Xcode is adding it's own compile line to the different files? – Dan Sep 29 '14 at 05:58
  • This may be a silly question but what is a compile line? – tomtclai Sep 29 '14 at 16:55
  • 1
    Xcode at one point used a utility called Make. It's a common compilation scripting utility, and Xcode may still use it to this day, I'm not sure. A compile line is my own misnomer. It's a line in a makefile which would be used to compile a code file (e.g. a .cpp); such a ``line'' might actually be several lines or distributed across a whole section. Really, I meant that Xcode was trying to compile the .cpp even though it just describes a templated class. – Dan Sep 29 '14 at 18:46