我花了很长时间才开始使用c ++进行编码,也许我在语法上缺少一些明显的东西,但是我已经搜索了很长时间了,在任何地方都找不到对我的问题的参考。我试图创建一个自定义的C ++类set
和multiset
。
这是我的班级cset.h
#pragma once
#include <set>
#include "cmultiset.h"
template <class Type>
class Set : public set<Type>
{
private:
public:
void add(Type &);
};
这是我的cmultiset.h
#pragma once
#include <set>
template <class Type>
class MultiSet : public multiset<Type>
{
private:
public:
bool operator < (MultiSet <Type> &);
};
我在这里想要做的是Set<MultiSet<int>>
在我的驱动程序类中创建一个。但出现以下错误两次,每次文件,而不是在上面的头文件class Set : public set<Type>
和class MultiSet : public multiset<Type>
。
syntax error: missing ',' before '<'
我不知道如何解决此错误。
如果我只使用set<MultiSet<int>>
一切,一切正常:No Errors no warning(我必须using namespace std;
在模板之前添加)。但是,当我使用Set<MultiSet<int>>
它会给出错误,using namespace std
并且不起作用。
编辑1:
错误:
Severity Code Description Project File Line Suppression State
Error C2143 syntax error: missing ',' before '<' Integer Sets Analyzer c:\users\abbas\documents\mega\personal projects\integer sets analyzer\integer sets analyzer\cmultiset.h 6
Error C2143 syntax error: missing ',' before '<' Integer Sets Analyzer c:\users\abbas\documents\mega\personal projects\integer sets analyzer\integer sets analyzer\cmultiset.h 6
Error C2143 syntax error: missing ',' before '<' Integer Sets Analyzer c:\users\abbas\documents\mega\personal projects\integer sets analyzer\integer sets analyzer\cset.h 7
Error C2143 syntax error: missing ',' before '<' Integer Sets Analyzer c:\users\abbas\documents\mega\personal projects\integer sets analyzer\integer sets analyzer\cmultiset.h 6
Error C2143 syntax error: missing ',' before '<' Integer Sets Analyzer c:\users\abbas\documents\mega\personal projects\integer sets analyzer\integer sets analyzer\cset.h 7
编辑2:
这是我的main.cpp
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include "cmultiset.h"
#include "cset.h"
using namespace std;
int main()
{
Set<MultiSet <int>> intSet;
intSet.clear();
_getch();
return 0;
}
这是我的MultiSet.cpp
#pragma once
#include "stdafx.h"
#include "cmultiset.h"
using namespace std;
template <class Type>
bool MultiSet<Type>::operator < (MultiSet<Type> & cmpSet)
{
if (this->size() < cmpSet.size())
{
return true;
}
else if (this->size() > cmpSet.size())
{
return false;
}
for (multiset<Type>::iterator it = this->begin(), jt = cmpSet.begin(); it != this->end(), jt != cmpSet.end(); ++it, ++jt)
{
if (*it < *jt)
return true;
}
return false;
}
这是我的Set.cpp。
#pragma once
#include "stdafx.h"
#include "cset.h"
using namespace std;
template <class Type>
void Set<Type> :: add(Type & entry)
{
set<Type>::insert(entry);
}
在class Set : public set<Type>
,它应该std::set
代替set
。
否则,编译器会给出语法错误,因为它没有意识到set
是一个类模板。
multiset
下一部分存在类似的问题。
注意 标准容器不是要从中继承;考虑改用遏制(例如,将容器作为成员变量)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句