语法错误:'<'之前缺少','

syntax error: missing ',' before '<'

本文关键字:lt 错误 语法      更新时间:2023-10-16

我在很长一段时间后才开始用c++编码,也许我在这里错过了一些语法上明显的东西,但我已经搜索了很长一段时间,现在找不到我的问题的参考。我正在尝试为setmultiset创建一个自定义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: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>

可能会有更多的错误,但这是我看到的第一个。

修复那个错误,然后再次编译,修复下一个第一个错误,依此类推。


顺便说一下,在编写了几行代码后进行编译是一个好主意,而不是等到编写了大量代码后才进行编译。