Bob Shannon
Bob Shannon

Reputation: 648

Creating a C++ STL set containing custom objects

I've got a custom class that I've defined, called User.cpp:

#include <iostream>
#include <set>
#include <utility>

using namespace std;

/////////////////////////////////////////////////////////////
//  Custom comparator for set of movie ratings
//
//  Stores user ratings in a tree, where highest rating
//  is at root of the tree.
/////////////////////////////////////////////////////////////
struct Cmp {
    bool operator ()(const pair<size_t,size_t> &a, const pair<size_t,size_t> &b) {
        return a.second > b.second;
    }
};

/////////////////////////////////////////////////////////////
//  User container class
/////////////////////////////////////////////////////////////
class User { 
    private: 
        const size_t userIndex;
        set<pair<size_t,size_t>, Cmp> ratings;

    public:
        /*
            Constructor
        */
        explicit User(const size_t userIndex) 
            : userIndex(userIndex)
        { }

};

I hope you can see where I am going with this. I now want to store all of these User objects into a new object defined by another class UserBase.cpp -- a container for the User objects if you will.

#include <iostream>
#include <set>
#include <utility>


using namespace std;

/////////////////////////////////////////////////////////////
//  UserBase container class
/////////////////////////////////////////////////////////////
class UserBase { 
    private: 
        set<User> cont;

    public:


};

But this gives errors like this:

UserBase.cpp:32:7: error: ‘User’ was not declared in this scope
   set<User> cont;
       ^
UserBase.cpp:32:11: error: template argument 1 is invalid
   set<User> cont;
           ^
UserBase.cpp:32:11: error: template argument 2 is invalid
UserBase.cpp:32:11: error: template argument 3 is invalid

I was wondering what I need to do to make this work. Is it something with templates?

Upvotes: 1

Views: 2514

Answers (1)

Dimitrios Bouzas
Dimitrios Bouzas

Reputation: 42899

Include header file with the definition of class User in header file with the definition with class UserBase, see below:

Don't put using namespace in header files

User.hpp

#include <iostream>
#include <set>
#include <utility>

/////////////////////////////////////////////////////////////
//  Custom comparator for set of movie ratings
//
//  Stores user ratings in a tree, where highest rating
//  is at root of the tree.
/////////////////////////////////////////////////////////////
struct Cmp {
    bool operator ()(const std::pair<size_t,size_t> &a, const std::pair<size_t,size_t> &b) {
        return a.second > b.second;
    }
};

/////////////////////////////////////////////////////////////
//  User container class
/////////////////////////////////////////////////////////////
class User { 
    private: 
        const std::size_t userIndex;
        std::set<std::pair<std::size_t,std::size_t>, Cmp> ratings;

    public:
        /*
            Constructor
        */
        explicit User(const std::size_t userIndex) 
            : userIndex(userIndex)
        { }

};

UserBase.hpp

#include <iostream>
#include <set>
#include <utility>
#include "User.hpp"

/////////////////////////////////////////////////////////////
//  UserBase container class
/////////////////////////////////////////////////////////////
class UserBase { 
    private: 
        std::set<User> cont;

    public:


};

Upvotes: 2

Related Questions