【发布时间】:2016-09-24 11:37:25
【问题描述】:
很长一段时间后,我才开始用 C++ 编码,也许我在这里遗漏了一些语法上很明显的东西,但是我已经搜索了很长时间,但在任何地方都找不到对我的问题的引用。我正在尝试为 set 和 multiset 创建一个自定义 C++ 类。
这是我的课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>> 一切正常:没有错误没有警告(我必须在模板前添加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);
}
【问题讨论】:
-
坏想法继承自
std::container-s,顺便说一句。 -
请尝试创建一个Minimal, Complete, and Verifiable Example 并向我们展示。并且还包括完整的错误输出,完整且未经编辑(即使它很长,因为它往往是涉及模板时)。
-
@Abbas 如果您曾经删除存储库,那么这个问题已经过时,因此您必须在此处粘贴代码。 5 个文件对于单个 SO 问题来说是巨大的。
-
@Abbas 但不是全部!创建一个minimal reproducible example。
-
public set<Type>应该是public std::set<Type>。与多集相同的问题
标签: c++ templates inheritance