在使用虚方法包含标头时,不确定如何在.cpp文件中设置方法

时间:2014-02-22 22:58:25

标签: c++ object virtual-method

好的,所以我是c ++的新手,并且有一个赋值来创建一个带有提供的头文件sortedlist.h和node.h的有序链表。我不需要方法中的任何代码的帮助只有如何设置我的类我比java更习惯java。我的问题是当我尝试在linkedSortedList.cpp文件中创建方法时出现错误

错误:'Elm'未在此范围内声明

错误:模板参数1无效

这里提供的 sortedList.h 带有一些文档

#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

这里是我开始尝试构建我的列表的地方是我的 linkedSortedList.h 在这个文件中,我收到错误:'{'令牌之前的预期class-name,我不确定为什么

#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 */

这是我在这个文件中的 linkedSortedList.cpp 我几乎所有的方法都会出错:  错误:'Elm'未在此范围内声明

错误:模板参数1无效

错误:'Elm'未在此范围内声明

错误:预期','或';'在'{'令牌

之前
#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(){

 }
;

我认为我要么缺少某些东西,要么只是完全按照我的班级构造,就像我说这是我的第一个c ++工作所以我不知道你是否可以展示或解释我应该如何做到这一点非常感激。提前感谢您提供所有帮助以及我从中学到的一切

2 个答案:

答案 0 :(得分:0)

您需要告诉SortedList它使用的是什么类型:

template <class Elm> class linkedSortedList: public SortedList <Elm>{

答案 1 :(得分:0)

你应该把

template <class Elm>

在cpp文件中的每个方法之前。不仅在第一个之前。如果没有这行,编译器就不知道Elm是模板参数。