语法错误:'<'之前缺少','
syntax error: missing ',' before '<'
我在很长一段时间后才开始用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
不起作用。
错误:
Severity Code Description Project File Line Suppression State
Error C2143 syntax error: missing ',' before '<' Integer Sets Analyzer c:usersabbasdocumentsmegapersonal projectsinteger sets analyzerinteger sets analyzercmultiset.h 6
Error C2143 syntax error: missing ',' before '<' Integer Sets Analyzer c:usersabbasdocumentsmegapersonal projectsinteger sets analyzerinteger sets analyzercmultiset.h 6
Error C2143 syntax error: missing ',' before '<' Integer Sets Analyzer c:usersabbasdocumentsmegapersonal projectsinteger sets analyzerinteger sets analyzercset.h 7
Error C2143 syntax error: missing ',' before '<' Integer Sets Analyzer c:usersabbasdocumentsmegapersonal projectsinteger sets analyzerinteger sets analyzercmultiset.h 6
Error C2143 syntax error: missing ',' before '<' Integer Sets Analyzer c:usersabbasdocumentsmegapersonal projectsinteger sets analyzerinteger sets analyzercset.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
也有类似的问题。
NB。标准容器不是用来继承的;考虑使用容器(即将容器作为成员变量)。
你不能在你的"cset.h"
头写set<Type>
。必须是std::set<Type>
可能会有更多的错误,但这是我看到的第一个。
修复那个错误,然后再次编译,修复下一个第一个错误,依此类推。
顺便说一下,在编写了几行代码后进行编译是一个好主意,而不是等到编写了大量代码后才进行编译。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- &lt;&lt;操作员在C 中超载错误
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- '&lt;'''&lt;'''&quot的模板类错误''令牌”
- 在尝试超载&lt;&lt;时链接错误2005和1169操作员
- 错误:没有匹配'运算符<<"在'std::cout
- 过载<<比如cout错误
- 重载& lt; & lt;为什么我得到以下错误
- 重载ostream&时转换错误无效;operator< & lt;
- 重载<<运算符错误C2804:二进制'运算符<<'参数太多
- 编译器错误,包括'<'代币
- 重载& lt; & lt;错误