C++ Code will Not Compile With Xcode 6.4

I have been trying to get C++ code that successfully compiles with Visual Studio 2013 to compile with Xcode 6.4, but will not. I have done some research through various sites but the answers do not seem address the issues I am having.


I developed this for use in a programming class. The concept is to use class templates with the LinkedListValue class as a derived class of LinkedList. Here is the LinkedList class header:

*****************************************************************

#ifndef _LINKED_LIST

#define _LINKED_LIST


#include "ListInterface.h"

#include "Node.h"

#include "PrecondViolatedExcep.h"


template<class ItemType>

class LinkedList : public ListInterface<ItemType>

{

private:

Node<ItemType>* headPtr; // Pointer to first node in the chain;

// (contains the first entry in the list)

int itemCount; // Current count of list items


// Locates a specified node in this linked list.

// @pre position is the number of the desired node;

// position >= 1 and position <= itemCount.

// @post The node is found and a pointer to it is returned.

// @param position The number of the node to locate.

// @return A pointer to the node at the given position.

Node<ItemType>* getNodeAt(int position) const;


public:

LinkedList();

LinkedList(const LinkedList<ItemType>& aList);

virtual ~LinkedList();


bool isEmpty() const;

int getLength() const;

bool insert(int newPosition, const ItemType& newEntry);

bool remove(int position);

void clear();


/* @throw PrecondViolatedExcep if position < 1 or

position > getLength(). */

ItemType getEntry(int position) const throw(PrecondViolatedExcep);


/* @throw PrecondViolatedExcep if position < 1 or

position > getLength(). */

void setEntry(int position, const ItemType& newEntry)

throw(PrecondViolatedExcep);


LinkedList<ItemType> & operator = ( const LinkedList<ItemType> & aList );

}; // end LinkedList


#include "LinkedList.cpp"

#endif

***************************************************************************************************


Notice that the #include which attaches the implementation to the header file. (This is the way the text does it.) Therefore, for the compile, all the code should appear as part of the header.


Here is the beginning of the LinkedListValue class header:

********************************************************************************************

#ifndef _LINKED_LIST_VALUE

#define _LINKED_LIST_VALUE


#include "LinkedList.h"


template<class ItemType>

class LinkedListValue : public LinkedList<ItemType>

{

public:


LinkedListValue() { LinkedList<ItemType>::LinkedList(); }

LinkedListValue(const LinkedListValue<ItemType> &list) { *this = list; }

virtual ~LinkedListValue() { clear(); }

*************************************************************************************************


One compile error is that clear is considered an undeclared identifier? Why? I saw that if I did: this->clear(); the compiler is happy. Why should that be? As I understand it, the "this->" prefix is optional. Why is it required here?


While that problem might be bypassed, this one, and others like it, cannot. In LinkedListValue.cpp (which is #included in LinkedListValue.h):

**********************************************************************************

#include <iostream>

using namespace std;


// insert will always insert at the end of the list

template<class ItemType>

bool LinkedListValue<ItemType>::insert(ItemType &val)

{

return LinkedList::insert(getLength() + 1, val);

}

******************************************************************************************

The concept here is to use the base class insert function. The return line gives several compile errors:

  • Undeclared identifier getLength
  • LinkedList not a class, namespace, or scoped enumeration


Why are these errors occuring? I believe that the code is ANSI/ISO compliant. (I will note that while "namespace std;" may provoke comments it is used in the texts and is apprpriate for the course.)


I am new to Xcode and I would definitely appreciate any assistance! Thank you.

Xcode's C++ compiler is generally a little stricter than Visual Studio's, that is, closer to the C++ standard. Without digging up the precise standardese (which you're probably not interested in), there's a rule about how names are looked up inside templates, and when you're "supposed" to have to give the compiler a hint. That explains the 'clear' problem, but the other two issues are actually the same. 'getLength' is doing the exact same thing as 'clear', of course. "LinkedList" is a bit trickier—the compiler doesn't recognize that as the base class without the template arguments, "LinkedList<ItemType>". (Another way to force it to look inside the current class is to write "LinkedListValue::LinkedList", but I find that stranger.)


Anyway, sorry for the pedantry, but may your code be more technically correct. ;-)

C&#43;&#43; Code will Not Compile With Xcode 6.4
 
 
Q