1

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);
}
Abbas
  • 3,529
  • 5
  • 36
  • 64
  • 5
    **bad** idea inheriting from `std::container`-s, btw. – edmz Sep 24 '16 at 11:39
  • Yes I know about inheriting from `std::container` and clearing memory. I just want to do it anyway – Abbas Sep 24 '16 at 11:41
  • 4
    Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. And also include the complete error output, in full and unedited (even if it's long as it tends to be when templates are involved). – Some programmer dude Sep 24 '16 at 11:43
  • [It works for me](https://ideone.com/3XPevC). Please create a [mcve]. – Rakete1111 Sep 24 '16 at 11:44
  • @JoachimPileborg please check my updated question. – Abbas Sep 24 '16 at 12:03
  • @Rakete1111 please check my updated question. – Abbas Sep 24 '16 at 12:03
  • @Abbas Nope. We need a [mcve], not a huge git repo. Please create a program which reproduces the error, it should be as small as possible. – Rakete1111 Sep 24 '16 at 12:06
  • @Rakete1111 it is small, just two `header` files, two `.cpp` source files and another `.cpp` `main()` file. – Abbas Sep 24 '16 at 12:09
  • 1
    @Abbas If you ever delete the repository, this question is obsolete, so you would have to paste the code here. 5 files is *huge* for a single SO question. – Rakete1111 Sep 24 '16 at 12:11
  • I know It is huge thats why I posted it on github, but I will also post code in the question. Thanks! – Abbas Sep 24 '16 at 12:13
  • 1
    @Abbas But not everything! Create a [mcve]. – Rakete1111 Sep 24 '16 at 12:16
  • `Type` is a terrible class name. – stark Sep 24 '16 at 12:24
  • Abbas, please make sure you read through the links Rakete1111 has provided on creating an example for Stack Overflow. Specifically, pay attention to the *Minimal* section on reducing your code to the smallest amount possible necessary to reproduce the problem. Typically this results in 15-20 lines of code. If your end example is noticeably longer than 20 lines, I might suggest it's not minimal enough. – Cornstalks Sep 24 '16 at 12:24
  • @Rakete1111 check my updated Question. Is this better? I tried to remove all the unnecessary code. – Abbas Sep 24 '16 at 12:39
  • 2
    `public set` should be `public std::set` . same prob with multiset – M.M Sep 24 '16 at 12:40
  • @M.M yes that did the trick in the minimal example but I tried that in the complete code earlier and that did not work. – Abbas Sep 24 '16 at 12:42
  • 1
    @Abbas you will need to make a minimal example that actually represents your problem. nobody here can guess your code – M.M Sep 24 '16 at 12:42
  • You need to read ["Why can templates only be implemented in the header file?"](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – Some programmer dude Sep 24 '16 at 13:43
  • @JoachimPileborg this is funny, I was just reading that. I am quite sure too the problem is with templates because if/when I define my class's methods in header file I get a running solution but if I define them in .cpp file can't find add() method Link error. Anyways I can't understand from the accepted answer what is it I am doing wrong. Any pointers? – Abbas Sep 24 '16 at 13:59
  • @Abbas: You should post new question about new problem. Anyway, in order to instantiate template stuff the compiler needs to see the template definitions. One way around that is to explicitly instantiate a template for the relevant template arguments (this avoids the need for instantiation elsewhere), and another way around it is to have the full template definition in header file. – Cheers and hth. - Alf Sep 24 '16 at 18:28
  • @M.M post your answer I'll accept it. – Abbas Sep 25 '16 at 10:22

2 Answers2

2

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
2

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 (i.e. have the container as a member variable).

M.M
  • 138,810
  • 21
  • 208
  • 365