我正在写两个模板化课程(出于学术原因):
Vector.cpp -
#ifndef __CS_VECTOR_H_
#define __CS_VECTOR_H_
#include <cstdlib>
#include "Set.cpp"
template <class Type>
class Vector
{
public:
Vector(unsigned int capacity = DEFAULT_CAPACITY);
Vector(const Vector<Type>& rhs);
~Vector();
unsigned int capacity() const;
unsigned int size() const;
bool empty() const;
void push_back(const Type& data);
bool remove(const Type& data);
void clear();
bool at(unsigned int pos, Type& data) const;
int get_array_size() const;
Type& operator[](unsigned int pos) const;
Vector& operator=(const Vector& rhs);
private:
static const unsigned int DEFAULT_CAPACITY = 3;
void generate_larger_array(unsigned int capacity);
int array_size_ = 0;
int array_capacity_;
Type *array_;
};
//Function definitions here.
//#include "Set.cpp"
#endif
Set.cpp -
#ifndef __CS_SET_H_
#define __CS_SET_H_
#include "Vector.cpp"
template <class Comparable>
class Set
{
public:
unsigned int size() const;
bool empty() const;
bool contains(const Comparable& data) const;
bool insert(const Comparable& data);
bool remove(const Comparable& data);
void clear();
int get_size();
private:
int element_;
Vector<Comparable> set_;
};
//Function definitions here
//#include "Vector.cpp"
#endif
Main.cpp的
#include "Set.cpp"
#include "Vector.cpp"
当我编译代码时,收到错误告诉我:
error: ‘Vector’ does not name a type Vector<Comparable> set_;
在Set中声明我的set_对象时
这些都是包含在main.cpp文件中的cpp文件。 set cpp文件确实包含#include vector.cpp,反之亦然
main.cpp只有用于测试类功能的代码。尝试编译main.cpp文件时收到此错误。 main.cpp文件是由我的教师编写的,所以我不完全确定我应该在这里发布它。