gpt4 book ai didi

c++ - 创建包含自定义对象的 C++ STL 集

转载 作者:行者123 更新时间:2023-11-28 02:49:31 25 4
gpt4 key购买 nike

我已经定义了一个自定义类,称为 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)
{ }

};

我希望你能明白我的目标。我现在想将所有这些 User 对象存储到另一个类 UserBase.cpp 定义的新对象中——如果您愿意的话,这是 User 对象的容器。

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


using namespace std;

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

public:


};

但这会产生如下错误:

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

我想知道我需要做什么才能完成这项工作。是模板的东西吗?

最佳答案

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

用户.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)
{ }

};

用户库.hpp

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

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

public:


};

关于c++ - 创建包含自定义对象的 C++ STL 集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23377400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com