我花了很长时间才刚刚开始用c++进行编码,也许我在语法上缺少一些明显的东西,但是我已经搜索了很长时间,并且在任何地方都找不到对我的问题的引用。我正在尝试为setmultiset创建一个自定义C++类。

这是我的类(class) 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);
}

最佳答案

class Set : public set<Type>中,它应该是std::set而不是set

否则,编译器会给出语法错误,因为它没有意识到set是一个类模板。

下一部分的multiset也有类似的问题。

注意标准容器并非要从中继承;考虑改用遏制(即将容器作为成员变量)。

关于c++ - 语法错误: ','之前缺少 '<',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39675896/

10-11 01:27