【问题标题】:syntax error: missing ',' before '<'语法错误:在 '<' 之前缺少 ','
【发布时间】:2016-09-24 11:37:25
【问题描述】:

很长一段时间后,我才开始用 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&lt;MultiSet&lt;int&gt;&gt;。但是在class Set : public set&lt;Type&gt;class MultiSet : public multiset&lt;Type&gt;的上述头文件中,每个文件都会出现两次以下错误。

syntax error: missing ',' before '<'

我不知道如何解决这个错误。

如果我只使用set&lt;MultiSet&lt;int&gt;&gt; 一切正常:没有错误没有警告(我必须在模板前添加using namespace std;)。但是,当我使用 Set&lt;MultiSet&lt;int&gt;&gt; 时,它会给出错误,而 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:

这是我的 ma​​in.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&lt;Type&gt; 应该是 public std::set&lt;Type&gt; 。与多集相同的问题

标签: c++ templates inheritance


【解决方案1】:

class Set : public set&lt;Type&gt; 中,应该是std::set 而不是set

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

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

注意。标准容器并不意味着继承;考虑改用包含(即将容器作为成员变量)。

【讨论】:

    【解决方案2】:

    您不能在 "cset.h" 标头中写入 set&lt;Type&gt;。那必须是std::set&lt;Type&gt;

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

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


    顺便说一句,编写几行代码后进行编译是个好主意,不要等到编写大量代码后再编译。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多