因此对于类,我们将使用从模板化类List派生的模板化类Set来执行set操作。编译时,编译器会抱怨Set尚未在当前作用域中声明。显然我已经包含了Set的.h文件和cpp中List的.h文件。我觉得我可能会以某种方式破坏构造函数,但对于什么是错误却无能为力。非常感谢任何见解。
编译投诉:
p07.cpp: In function ‘void SetMgr(std::istream&, std::istream&, std::istream&, std::ostream&)’:
p07.cpp:43:2: error: ‘Set’ was not declared in this scope
p07.cpp:43:6: error: expected primary-expression before ‘int’
p07.cpp:43:6: error: expected ‘;’ before ‘int’
p07.cpp:44:2: error: ‘s1’ was not declared in this scope
p07.cpp:47:6: error: expected primary-expression before ‘int’
p07.cpp:47:6: error: expected ‘;’ before ‘int’
p07.cpp:48:2: error: ‘s2’ was not declared in this scope
p07.cpp:51:6: error: expected primary-expression before ‘int’
p07.cpp:51:6: error: expected ‘;’ before ‘int’
p07.cpp:52:2: error: ‘s3’ was not declared in this scope
p07.cpp:55:6: error: expected primary-expression before ‘int’
p07.cpp:55:6: error: expected ‘;’ before ‘int’
p07.cpp:56:2: error: ‘I’ was not declared in this scope
p07.cpp:60:6: error: expected primary-expression before ‘int’
p07.cpp:60:6: error: expected ‘;’ before ‘int’
p07.cpp:61:2: error: ‘U’ was not declared in this scope
p07.cpp:65:6: error: expected primary-expression before ‘int’
p07.cpp:65:6: error: expected ‘;’ before ‘int’
p07.cpp:66:2: error: ‘D’ was not declared in this scope
p07.cpp包括:
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <fstream>
#include <string>
#include <limits.h>
using namespace std;
#include "List07.h"
#include "Set07.h"
编译器在p07.cpp中抱怨的函数:
void SetMgr(istream& i1, istream& i2, istream& i3, ostream& o) {
//Next Line is line 43 where errors begin
Set<int> s1(INT_MIN, i1);
s1.Print(cout, "set 1");
s1.Print(o, "set 1");
Set<int> s2(INT_MIN, i2);
s2.Print(cout, "set 2");
s2.Print(o, "set 2");
Set<int> s3(INT_MIN, i3);
s3.Print(cout, "set 3");
s3.Print(o, "set 3");
Set<int> I(INT_MIN);
I.Intersection(s1, s2);
I.Print(cout, "set I");
I.Print(o, "set I");
Set<int> U(INT_MIN);
U.Union(s2, s3);
U.Print(cout, "set U");
U.Print(o, "set U");
Set<int> D(INT_MIN);
D.Difference(U, I);
D.Print(cout, "set D");
D.Print(o, "set D");
}
List07.h类声明和构造函数: 模板 class List { int size; int count; int游标; T * L;
public:
void Empty(void){int sz=size; if(L)delete[] L; New(sz);}
List(T m, int sz=100):size(sz), count(0), cursor(0), MIN(m) {L=new T[size];L[0]=MIN;}
List(T m, istream&i, int sz=100):size(sz), count(0), cursor(0), MIN(m)
{
L=new T[size]; L[0]=MIN; Scan(i);
}
};
Set07.h包含List07.h,类声明和构造函数:
#ifndef List07_h
#define List07_h 1
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <fstream>
#include <string>
#include <limits.h>
#include "List07.h"
template <class T>
class Set <T> public: List<T> {
public:
Set(T m):List<T>(m, sz){}
Set(T m, istream& i, ):List<T>(m, i){}
~Set(){~List();}
};
#endif
答案 0 :(得分:0)
你应该改变(文件Set07.h):
#ifndef List07_h
#define List07_h 1
类似于:
#ifndef Set07_h
#define Set07_h
祝你好运!