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

时间:2016-09-24 11:37:26

标签: c++ templates inheritance

我刚开始用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:\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);
}

2 个答案:

答案 0 :(得分:1)

您无法在set<Type>标题中写下"cset.h"。这需要std::set<Type>

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

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

顺便说一下,编写几行代码时编译是一个好主意,而不是在编写大量代码之前等待编译。

答案 1 :(得分:1)

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

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

下一部分multiset存在类似的问题。

NB。标准容器不应继承;考虑使用包含代替(即将容器作为成员变量)。