EDIT: Fixed the error
This is my first time working with templates and am getting nasty syntax errors. I have no idea where the error is since the compiler is telling me I am missing a semi-colon, followed by hundreds of errors. Everything appears fine, however. I am almost certain no semi-colons are missing.
Can anyone help me find this needle in a haystack?? PS The error occurs on the definition of the constructor for the List and says missing ';' before '<' (i.e. the code below)
template <typename T>
class Node
{
template <typename T>
friend class List<T>;
public:
//Default constructor
Node();
//Copy constructor
Node(const Node<T> & copy);
//Overloaded assignment operator
Node<T> &operator=(const Node<T> & rhs);
//Destructor
~Node();
//Methods
T CreateNode(const T & T);
T &getData();
private:
//Data members
T data;
Node * mNext;
};
I can't guarantee it's the only problem, but this certainly looks suspicious:
T &List<T>::Front() const
T &List<T>::Back() const
void Purge();
At a guess, you intended that to declare three separate functions, in which case it is missing a couple of semicolons, and should look more like this:
T &List<T>::Front() const;
T &List<T>::Back() const;
void Purge();
...or, quite possibly:
T &Front() const;
T &Back() const;
void Purge();
Found the error. It was in my node.h file. The code was
friend class List<T>;
I removed the and it worked. :)
Related
I'm trying to implement an operator overload function based on a header file that was given to me, but I'm not understanding one this. Here's what I've been given:
template<class Type>
myClass<Type>& myClass<Type>::operator =(const myClass<Type> &);
My question is in relation to the parameter passed. (const myClass &) indicate a value passed, but how to I reference that value within the function? Normally if I have (const myClass &myValue), I would reference that with myValue=whatever. But I'm not sure how to handle this one.
This is the header file that i'm trying to implement. I'm not asking for anyone to solve this, but I would like some clarifications:
template<class Type>
struct nodeType{
Type value;
nodeType<Type> *next;
nodeType<Type> *prev;
};
template <class Type>
class sortedListADT {
public:
const sortedListADT<Type>& operator=(const sortedListADT<Type> &);
bool isEmpty() const;
bool search(const Type& searchElem) const;
void removeElement(const Type& remElem);
void insertElement(const Type& newElem);
Type front() const;
Type back() const;
void printInOrder() const;
void printRevOrder() const;
void destroyList();
sortedListADT();
sortedListADT(const sortedListADT<Type>& otherList);
~sortedListADT();
private:
nodeType<Type> *first;
nodeType<Type> *last;
int numElements;
void copyList(const sortedListADT<Type>& otherList);
};
how to I reference that value within the function?
We just can't because the parameter is unnamed and for us to use it inside the function it must have a name(to refer to it by that name).
It seems that you're trying to implement the overloaded operator= which you can do as shown below:
template <class Type>
class sortedListADT {
public:
//this is a declaration
const sortedListADT<Type>& operator=(const sortedListADT<Type> &);
//other code as before
};
template<typename Type>
//-----------------------------------------------------------------------------------vvvvvvvvvvvvv--->added a name for the paramter
const sortedListADT<Type>& sortedListADT<Type>::operator=(const sortedListADT<Type> &namedParameer)
{
//add your code here
//don't forget to have a return statement here
}
Working demo
I am having an issue with implementing a template Linked List class.
I've made linked lists before, but not as a template.
In the .cpp file (implementation) it seems that the member pointer variables aren't being recognized as members and using -> after these pointers doesn't work.
There is no underline of syntax error anywhere near the issue, but when I highlight the variable, the description says "unknown" and the details of the variable
(What's even stranger is that nothing i place in the scope can get the underline syntax error.)
// ListLinked.h
#include <iostream>
using namespace std;
template <typename DataType>
class List {
private:
struct ListNode {
DataType dataItem;
ListNode* next;
};
ListNode* head;
ListNode* cursor;
public:
List(int ignored = 0);
List(const List& other);
List& operator=(const List& other);
~List();
void insert(const DataType& newDataItem);
void remove();
void replace(const DataType& newDataItem);
void clear();
bool isEmpty() const;
bool isFull() const;
void gotoBeginning();
void gotoEnd();
bool gotoNext();
bool gotoPrior();
void showStructure() const;
DataType getCursor() const;
};
//ListLinked.cpp (just the simplest one of the many member functions that use ->)
template <typename DataType>
bool List<DataType>::gotoNext(){
if (cursor->next != NULL) //"next" is not seen as a member pointer variable
cursor = cursor->next;
}
// all the other member functions start with
// template <typename DataType>
// [type] List<DataType>:: {...}
I suspect that something is wrong with my visual express 2010, but i'm not sure
When you use templated class/function, you need to have the whole implementation in source file. Put all the template reference to your .h file as a first move ;)
If you want to keep your header clean without the function code, you can create a file named linkedlist.impl and put in your definition. At the end of your header, before your guard, add #include "linkedlist.impl".
i tried to build up my own linked list class and got a problem with the = operator overloading. as far as i know, we should use const parameter when overloading the assignment operator, say using linked_list<T>& operator=( const linked_list<T>& a). however, the complier gave me errors unless i put linked_list<T>& a instead. the complier would stop at
if(this->head==a.front()), giving me Error
11 error C2662: 'linked_list<T>::front' : cannot convert 'this' pointer from 'const linked_list<T>' to 'linked_list<T> &'
below are the details.
#ifndef _LINKED_LIST_H_
#define _LINKED_LIST_H_
template <class T>
struct node
{
T data;
node<T>* next;
};
template <class T>
class linked_list
{
private:
node<T>* head;
public:
linked_list<T>();
linked_list<T>(const linked_list<T>& a);
~linked_list<T>();
linked_list<T>& operator=(const linked_list<T>& a);
bool isEmpty();
int size() const;
void insert_front(T a);
void insert_end(T a);
void erase_end();
void erase_front();
void print() const;
void erase(node<T>* a);
node<T>*& front()
{
node<T>* ptr = new node<T>;
ptr = head;
return ptr;
}
void setFront(node<T>* a);
};
#endif
template <class T>
linked_list<T>& linked_list<T>::operator=(const linked_list<T>& a)
{
if (this->head == a.front()) // the error mentioned happened here. however,
// if no const in the parameter, it would be
// no error
{
return *this;
}
while (head != nullptr) erase_front();
node<T>* copy;
copy = a.front();
while (copy->next != nullptr)
{
insert_end(copy->data);
copy = copy->next;
}
return *this;
}
anybody can help? thanks.
When an accessor returns a reference to an owned structure, it's usually a good idea to implement two versions: One which is non-const and returns a non-const reference, and one which is const and returns a const reference. That way it can be used in both mutating and non-mutating contexts. front() would be a good candidate for this.
Though a side note -- you probably don't want to expose your nodes in the public linked_list interface, particularly non-const references to them. That's the sort of thing to encapsulate entirely in the class.
The problem is that front() is not a const member function, and you're trying to call it on a const instance.
I am working on making a program and part of it requires the use of a BST template including a structure for the node. When I try to compile the code in g++ I get a compile error (and a few others that appear to be caused by the same problem.
Below is the part of my code that is causing the problem.
#include <iostream>
template <typename Comparable> class BinarySearchTree {
public:
BinarySearchTree();
BinarySearchTree( const BinarySearchTree &rhs);
~BinarySearchTree();
const Comparable & findMax() const;
void insert( const Comparable &x);
const BinarySearchTree & operator=(const BinarySearchTree &rhs);
/* ..a bunch of other binary search tree related functions... */
private:
struct BinaryNode {
Comparable element;/* why is this line causing problems? */
BinaryNode *left;
BinaryNode *right;
BinaryNode(const Comparable &theElement, BinaryNode *lt, BinaryNode *rt);
: element(theElement), left(lt), right(rt){}
};
/* ... some more functions ... */
};
int main() {
return 0;
}
Compiling this with g++ causes the following error message:
Line 16:invalid use of non-static data member BinarySearchTree<Comparable>::BinaryNode::element
Apologies for the stupid question. This code is extremely similar to some code in my textbook and copying it just produces this error.
BinaryNode(const Comparable &theElement, BinaryNode *lt, BinaryNode *rt); //<-- This..
: element(theElement), left(lt), right(rt){}
has a semi-colon.. Remove it and it'll work just fine.
This is my first foray into C++ templates, and I'm trying to construct a BinaryTree template to help me with a Project Euler problem; however, I seem to be getting an error where BinaryTree class doesn't recognize all the constructors of the BinaryTreeNode! Here's a snippet of the code.
template <class T>
class BinaryTreeNode
{
private:
BinaryTreeNode<T>* _left;
BinaryTreeNode<T>* _right;
T* _value;
public:
BinaryTreeNode();
explicit BinaryTreeNode(const T& value) : _value(&(T(value))) {}
BinaryTreeNode(BinaryTreeNode<T>& left, BinaryTreeNode<T>& right, const T& value) :
_left(&left), _right(&right), _value(&(T(value))){}
};
The BinaryTree class
#include "BinaryTreeNode.h"
template <class T>
class BinaryTree
{
private:
BinaryTreeNode<T>* _root;
BinaryTreeNode<T>* _current;
unsigned int size;
public:
BinaryTree() : size(0), _root(0), _current(0) { }
explicit BinaryTree(BinaryTree<T>& leftTree, BinaryTree<T>& rightTree, const T& value) :
size(leftTree.Size() + rightTree.Size() + 1), _root(leftTree.Root(), rightTree.Root(), value), _current(_root) {}
explicit BinaryTree(const T& value) : size(1), _root(value) {}
const BinaryTreeNode<T>& Root() const { return *_root;}
};
I'm getting these errors.
error C2359: 'BinaryTree<T>::_root' : member of non-class type requires single initializer expression
error C2440: 'initializing' : cannot convert from 'const int' to 'BinaryTreeNode<T> *'
error C2439: 'BinaryTree<T>::_root' : member could not be initialized
The BinaryTreeNode constructor of (BinaryTreeNode<T>&, BinaryTreeNode<T>&, const T& value) works when I include it in my main code, but it doesn't seem to work under my BinaryTree template. Anyone know why?
In your initialization expression _root(leftTree.Root(), rightTree.Root(), value), _root is a pointer. You can only initialize it to another pointer. Perhaps you mean to initialize it to a pointer to a new node constructed on those arguments?
This could be done like this: (updated after your edit)
_root(new BinaryTreeNode<T>(leftTree.Root(), rightTree.Root(), value))
However, this is very dangerous (think about an exception in the allocation), and you should probably avoid using raw pointers in your class design and instead use smart managing pointers.
Similarly, the initializer _root(value) does the wrong thing, you might want:
_root(new BinaryTreeNode<T>(value))
(Also note that you should initialize members in their order of declaration.)
Update: I changed the first constructor call following your edit, but as #Luc says, your constructors take non-const arguments but Root() only provides a const reference, so you still need to fix that.
You have missed ; after both class declarations!
template <class T>
class BinaryTreeNode
{
private:
BinaryTreeNode<T>* _left;
BinaryTreeNode<T>* _right;
T* _value;
public:
BinaryTreeNode();
explicit BinaryTreeNode(const T& value) : _value(&(T(value))) {}
BinaryTreeNode(BinaryTreeNode<T>& left, BinaryTreeNode<T>& right, const T& value) :
_left(&left), _right(&right), _value(&(T(value))){}
};
template <class T>
class BinaryTree
{
private:
BinaryTreeNode<T>* _root;
BinaryTreeNode<T>* _current;
unsigned int size;
public:
BinaryTree() : size(0), _root(0), _current(0) { }
explicit BinaryTree(BinaryTree<T>& leftTree, BinaryTree<T>& rightTree, const T& value) :
size(leftTree.Size() + rightTree.Size() + 1), _root(leftTree.Root(), rightTree.Root(), value), _current(_root) {}
explicit BinaryTree(const T& value) : size(1), _root(value) {}
};
I believe that you need a constructor in the form BinaryTree<T>();