问题描述
我刚开始用c ++开始编码,也许我在这里遗漏了一些语法上很明显的东西,但是我现在已经搜索了很长时间,并且无法在任何地方找到我的问题的参考。我正在尝试为 set
和 multiset
创建自定义C ++类。
I just started coding in c++ after a long while, and perhaps I am missing something syntactically obvious here, but I have searched for good long while now and can't find a reference to my problem anywhere. I am trying to create a custom C++ class for set
and 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> ;>
在我的驱动程序类中。但是对于每个文件而言,在类的上述头文件中得到以下错误两次:set set:public< Type>
和 class MultiSet:public multiset<输入>
。
What I am trying to do here is create a Set<MultiSet<int>>
in my driver class. But get the following error twice for each file instead in the above header files at class Set : public set<Type>
and class MultiSet : public multiset<Type>
.
syntax error: missing ',' before '<'
我不知道如何解决此错误。
I don't know how to resolve this error.
如果我只使用 set< MultiSet< int>>
一切正常:没有错误没有警告(我必须添加使用命名空间std;
在模板之前)。但是,当我使用 Set< MultiSet< int>>
时,它会给出错误,使用命名空间std
不起作用。
If I use just set<MultiSet<int>>
everything works fine: No Errors no warnings (I do have to add using namespace std;
before the template). However when I use Set<MultiSet<int>>
it gives the error and using namespace std
doesn't work.
编辑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
是一个类模板。
The compiler gives a syntax error otherwise because it doesn't realize set
is a class template.
下一部分 multiset
存在类似问题。
There is a similar problem with multiset
in the next part.
NB。标准容器不是要继承的;考虑使用包容(即将容器作为成员变量)。
NB. Standard containers are not meant to be inherited from; consider using containment instead (i.e. have the container as a member variable).
这篇关于语法错误:在'<'之前缺少','的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!