简体   繁体   中英

syntax error: missing ',' before '<'

I just started coding in c++ after a long while, and perhaps I am missing something syntactically obvious here, but I have searched for good long while now and can't find a reference to my problem anywhere. I am trying to create a custom C++ class for set and multiset .

Here is my class cset.h

#pragma once
#include <set>
#include "cmultiset.h"

template <class Type>

class Set : public set<Type>
{
private:

public:
    void add(Type &);
};

And here is my cmultiset.h

#pragma once
#include <set>

template <class Type>

class MultiSet : public multiset<Type>
{
private:

public:
    bool operator < (MultiSet <Type> &);
};

What I am trying to do here is create a Set<MultiSet<int>> in my driver class. But get the following error twice for each file instead in the above header files at class Set : public set<Type> and class MultiSet : public multiset<Type> .

syntax error: missing ',' before '<'

I don't know how to resolve this error.

If I use just set<MultiSet<int>> everything works fine: No Errors no warnings (I do have to add using namespace std; before the template). However when I use Set<MultiSet<int>> it gives the error and using namespace std doesn't work.

Edit 1 :

Errors :

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   

Edit 2 :

Here is my 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;
}

Here is my 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;
}

Here is my 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);
}

You can't write set<Type> in your "cset.h" header. That needs to be std::set<Type> .

There may be more errors but this was the first one I saw.

Fix that one, then compile again, fix the next first error, and so on, and on.


By the way, it's a good idea to compile when you have written a few lines of code, and not wait with compiling until you've written tons of code.

In class Set : public set<Type> , it should be std::set instead of set .

The compiler gives a syntax error otherwise because it doesn't realize set is a class template.

There is a similar problem with multiset in the next part.

NB. Standard containers are not meant to be inherited from; consider using containment instead (ie have the container as a member variable).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM