【问题标题】:Unsure how to set up methods in .cpp file when including a header with virtual methods在包含带有虚拟方法的标头时,不确定如何在 .cpp 文件中设置方法
【发布时间】:2014-02-22 22:58:24
【问题描述】:

好的,所以我是 C++ 新手,并且有一个任务是创建一个带有提供的标题 sortedlist.h 和 node.h 的排序链表。我不需要方法中的任何代码的帮助,只需要如何设置我的类我更习惯于 java 而不是 c++。我的问题是当我尝试在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 在这个文件中我得到了错误:在'{'令牌之前的预期类名,我不确定为什么

#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++ 工作,所以我不知道你是否能展示或解释我应该如何做这件事非常感激。提前感谢您的所有帮助以及我从中学到的一切

【问题讨论】:

  • -1 太多无关代码...

标签: c++ object virtual-functions


【解决方案1】:

你应该放

template <class Elm>

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 2017-08-18
      • 2011-05-06
      • 1970-01-01
      相关资源
      最近更新 更多