You should put
template <class Elm>
before every method in cpp file. Not only before the first one. Without this line compiler does not know that Elm is a template argument.
Domanda
Ok so I am new to c++ and have a assignment to create a sorted linked list with provided headers sortedlist.h and the node.h of course. I dont need help with any code within methods only how to set up my classes I'm more used to java than c++. my issue is when I tried creating the methods in the linkedSortedList.cpp file I get errors such
error: 'Elm' was not declared in this scope
error: template argument 1 is invalid
here is the provided sortedList.h with some documentation
#ifndef _SortedListClass_
#define _SortedListClass_
template <class Elm> class SortedList {
public:
// -------------------------------------------------------------------
// Pure virtual functions -- you must implement each of the following
// functions in your implementation:
// -------------------------------------------------------------------
// Clear the list. Free any dynamic storage.
virtual void clear() = 0;
// Insert a value into the list. Return true if successful, false
// if failure.
virtual bool insert(Elm newvalue) = 0;
// Get AND DELETE the first element of the list, placing it into the
// return variable "value". If the list is empty, return false, otherwise
// return true.
virtual bool getfirst(Elm &returnvalue) = 0;
// Print out the entire list to cout. Print an appropriate message
// if the list is empty. Note: the "const" keyword indicates that
// this function cannot change the contents of the list.
virtual void print() const = 0;
// Check to see if "value" is in the list. If it is found in the list,
// return true, otherwise return false. Like print(), this function is
// declared with the "const" keyword, and so cannot change the contents
// of the list.
virtual bool find(Elm searchvalue) const = 0;
// Return the number of items in the list
virtual int size() const = 0;
};
#endif
here is where I start trying to build my list here is my linkedSortedList.h in this file I get the error: expected class-name before '{' token and I am unsure why
#ifndef LINKEDSORTEDLIST_H
#define LINKEDSORTEDLIST_H
#include "SortedList.h"
template <class Elm> class linkedSortedList: public SortedList{
public:
linkedSortedList();
~linkedSortedList();
void clear() = 0;
// Insert a value into the list. Return true if successful, false
// if failure.
bool insert(Elm newvalue) = 0;
// Get AND DELETE the first element of the list, placing it into the
// return variable "value". If the list is empty, return false, otherwise
// return true.
bool getfirst(Elm &returnvalue) = 0;
// Print out the entire list to cout. Print an appropriate message
// if the list is empty. Note: the "const" keyword indicates that
// this function cannot change the contents of the list.
void print() const = 0;
// Check to see if "value" is in the list. If it is found in the list,
// return true, otherwise return false. Like print(), this function is
// declared with the "const" keyword, and so cannot change the contents
// of the list.
bool find(Elm searchvalue) const = 0;
// Return the number of items in the list
int size() const = 0;
private:
};
#endif /* LINKEDSORTEDLIST_H */
And Here is my linkedSortedList.cpp in this file I get error on just about all of my methods saying: error: 'Elm' was not declared in this scope
error: template argument 1 is invalid
error: 'Elm' was not declared in this scope
error: expected ',' or ';' before '{' token
#include "linkedSortedList.h"
#include "LinkedNode.h"
template <class Elm>
linkedSortedList<Elm>::linkedSortedList() {
}
linkedSortedList<Elm>::~linkedSortedList() {
}
// Clear the list. Free any dynamic storage.
void linkedSortedList<Elm>::clear(){
}
// Insert a value into the list. Return true if successful, false
// if failure.
bool linkedSortedList<Elm>::insert(Elm newvalue){
}
// Get AND DELETE the first element of the list, placing it into the
// return variable "value". If the list is empty, return false, otherwise
// return true.
bool linkedSortedList<Elm>::getfirst(Elm &returnvalue){
}
// Print out the entire list to cout. Print an appropriate message
// if the list is empty. Note: the "const" keyword indicates that
// this function cannot change the contents of the list.
void linkedSortedList<Elm>::print(){
}
// Check to see if "value" is in the list. If it is found in the list,
// return true, otherwise return false. Like print(), this function is
// declared with the "const" keyword, and so cannot change the contents
// of the list.
bool linkedSortedList<Elm>::find(Elm searchvalue){
}
// Return the number of items in the list
int linkedSortedList<Elm>::size(){
}
;
I figure I am either missing something or just comppletely off with my class construction like I said this is my first c++ work so I don't know much if you could show or explain how I should be doing this that'd be much appreciated. Thank you in advance for all the help and everything I learn from it
Soluzione
You should put
template <class Elm>
before every method in cpp file. Not only before the first one. Without this line compiler does not know that Elm is a template argument.
Altri suggerimenti
You need to tell the SortedList what type it is using:
template <class Elm> class linkedSortedList: public SortedList <Elm>{