1

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?

Bob Shannon
  • 638
  • 2
  • 10
  • 19
  • 2
    include header with the definition of class `User` in header with the definition with class `UserBase`. – 101010 Apr 30 '14 at 00:04
  • Ah, that seems to have done the trick! – Bob Shannon Apr 30 '14 at 00:12
  • See link in my answer for why you shouldn't put `using namespace` in header files. – 101010 Apr 30 '14 at 00:16
  • @40two Interesting, I didn't know about that. I plan on separating my source into .h and .cpp files once everything is done, do you think that's an advisable approach? – Bob Shannon Apr 30 '14 at 00:20
  • 1
    Take a look at http://stackoverflow.com/questions/4955159/is-is-a-good-practice-to-put-the-declaration-of-c-classes-into-the-header-file – 101010 Apr 30 '14 at 00:24

1 Answers1

2

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:


};
Community
  • 1
  • 1
101010
  • 41,839
  • 11
  • 94
  • 168