C++ 模板类的实例化
在尝试实例化模板化类的数据成员时,我经常遇到以下错误:C++ 模板类的实例化,c++,templates,instantiation,C++,Templates,Instantiation,在尝试实例化模板化类的数据成员时,我经常遇到以下错误: Proj3Aux.h:28: error: ISO C++ forbids declaration of ‘AugmentedBinarySearchTree’ with no type Proj3Aux.h:28: error: expected ‘;’ before ‘<’ token BinaryNode类的定义在该给定代码之上,在ABS cpp文件中,所有方法都以正确的方式定义: template <class Com
Proj3Aux.h:28: error: ISO C++ forbids declaration of ‘AugmentedBinarySearchTree’ with no type
Proj3Aux.h:28: error: expected ‘;’ before ‘<’ token
BinaryNode类的定义在该给定代码之上,在ABS cpp文件中,所有方法都以正确的方式定义:
template <class Comparable>
<return type> AugmentedBinarySearchTree<Comparable>::<method>(<parameters>)
看起来像是名称查找问题。您确定模板类AugmentedBinarySearchTree是在Proj3Aux之前声明的吗?在Proj3Aux.h中,是否包含声明AugmentedBinarySearchTree的标头?@DanielStrul是的,它们位于单独的文件中,@crayzeewulf AugmentedBinarySearchTree.h包含在文件中。它们可能位于单独的文件中,但包含的顺序错误。特别是,为什么在头文件的末尾有一行代码写着include AugmentedBinarySearchTree.cpp???@DanielStrul我也不明白,但是我们不应该编辑讲师提供的ABS代码。
template <class Comparable>
class AugmentedBinarySearchTree
{
public:
/*------------Constructors/Destructors-------------*/
explicit AugmentedBinarySearchTree();
AugmentedBinarySearchTree(const AugmentedBinarySearchTree<Comparable> & rhs);
~AugmentedBinarySearchTree();
/*--------------Facilitators------------------------*/
int remove(const Comparable & x);
bool IsPerfect();
// bool IsComplete(); <-- Extra Credit!
void PrintLevels(int numlevels);
void makeEmpty();
/*---------------Getters---------------------------*/
int RemoveResidue(); /* Assume RemoveResidue will always be called after Prin\
t */
const Comparable & NthElement(int n);
int Rank(const Comparable & x);
const Comparable & Median();
BinaryNode<Comparable> * findMin(BinaryNode<Comparable> *t) const;
/*---------------Setters---------------------------*/
int insert(const Comparable & x);
private:
int insert(const Comparable & x, BinaryNode<Comparable> * & t) const;
int remove(const Comparable & x, BinaryNode<Comparable> * & t) const;
void PrintLevels(queue <BinaryNode<Comparable> *> q, int levels);
void RemoveResidue(BinaryNode<Comparable> * & t, int *deletions) const;
BinaryNode<Comparable> * NthElement(BinaryNode<Comparable> *t, int *nodesVisi\
ted, int n) const;
void Rank(const Comparable & x, BinaryNode<Comparable> *t, int *nodesVisited)\
const;
bool IsPerfect(queue <BinaryNode<Comparable> *> q, int height);
void makeEmpty(BinaryNode<Comparable> * & t) const;
//IsComplete <-- Extra Credit!
/*------------------Members-----------------------------*/
BinarySearchTree<Comparable> m_tree;
BinaryNode<Comparable> *root;
};
#include "AugmentedBinarySearchTree.cpp"
template <class Comparable>
<return type> AugmentedBinarySearchTree<Comparable>::<method>(<parameters>)
#ifndef PROJ3_AUX_H_
#define PROJ3_AUX_H_
#include <iostream>
#include <string>
#include <fstream>
#include "AugmentedBinarySearchTree.h"
using namespace std;
const char COMMENT = '#';
class Proj3Aux
{
private:
//AugmentedBinarySearchTree<int> m_tree;
string m_currentCmd;
vector<int> m_inputs;
public:
/*
* Enum to contain all commands available
*/
enum COMMANDS {PRINT, RESIDUALS, RANK, NTH, MEDIAN, REMOVE, PERFECT,
COMPLETE};
/*
* Proj3Aux
* Default class constructor
* Preconditoins: none
* Postconditions: new instance of Proj3Aux is created
*/
Proj3Aux();