I have an index table where every index contains a list of type 'entry', where entry is
class entry{
public:
string word;
vector<int> line_numbers;
}
List member functions such as 'push_back' and 'empty' are unrecognized by the compiler since the list is of a custom type. How should I format to make these work, short of expanding the 'entry' class to include modified list member functions? Is that even possible? For reference,
vector<list<entry> > table
is the index table, and
table[c]
is the index. I'm trying to use something akin to
table[c].empty()
to test if the list is empty.
You're mistaken. std::list<entry>::empty() works regardless of the definition of empty. push_back requires that you can copy (or move) an empty object, how else are you going to get it into the container?
Related
The field has to be immutable so I can't use the vector. Is there a way to do it like in the title?
I want to do something like this:
typedef list<pair<int,string>> list_pair;
class tree{
private:
list_pair arr[]{
public:
tree(int size){
arr[size];
}
}
Is there a way to do it like in the title?
No.
A non-static member array must have a known size, there is no way around that in C++.
The field has to be immutable so I can't use the vector.
Your example array of non-const isn't immutable either.
Furthermore, I don't see a reason why that should matter. It's a private member, so it's fairly easy to choose to not mutate it. That way the class remains effectively immutable from the outside. Conclusion: Use std::vector.
I'm trying to add a new constructor an existing class and wondering if I can somehow do an emplace initialization of an optional and then use that value in the initializer for another member value.
For example:
class sample {
my_type sc;
optional<opt_type> run;
sample() :
sc( gen_default() ) {
}
enum ctor_alt { ctor_alt };
sample( ctor_alt ) :
emplace( run, ctor_arg ), /* Possible somehow? */
sc( run.field ) {
}
My primary motivation is that I don't want to alter the type of my_type. There are many users of this variable and putting it in a wrapper would require changing a lot of code.
emplace is a member function, and you cannot execute a member function until you've constructed the object (unless you're the object's constructor). But if you want to construct an engaged optional<T> in-place, you can simply use the std::in_place constructor:
run( std::in_place, ctor_arg )
However, member initializers are always executed in the order in which the members are declared, not the order in the member initializer list (your compiler ought to warn you if you initialize them out of order). So if you want to use an earlier initializer to initialize a later variable, you have to order them correctly in your declarations:
optional<opt_type> run;
my_type sc;
Sure thing, just call the constructor of std::optional with std::in_place_t:
run{std::in_place, ctor_arg}
std::in_place_t is a tag type to disambiguate when you want the type to construct the value in place instead of copying it.
If you want to use the value of the optional for another member variable, you need to make sure that that member variable comes after the optional, because member objects are initialized in the order that they appear.
I'm new to Qt Creator and I'm trying to define a Database the takes a vector as its parameter. I already have the code for the Database and here is what I am doing to instantiate the object.
public:
vector<CEmployee*> records;
CDatabase all_emps(records);
I keep getting the error that "records is not a type" though and I don't quite understand why since I've define records as a vector right above it. I've tried changing records to simply vector but that creates other errors elsewhere in my code. If anyone could point me in a direction for how to fix this it would be much appreciated. Thanks in advance!
Well, records is not a type, it's the name of a member variable. You probably meant this:
public:
// member variable declaration
std::vector<CEmployee*> records;
// method declaration, taking a vector of CEmployee* as an argument
CDatabase all_emps(std::vector<CEmployee*> records);
I have no idea, though, why records is a publicly visible member, as it seems to be an implementation detail, and why all_emps returns a CDatabase.
Maybe this would make more sense:
private:
/// A database used to manage the data.
CDatabase m_db;
public:
/// Returns all employee records from the database.
std::vector<CEmployee*> all_employees();
Make sure you have #include <vector> and using std::vector somewhere, or simply use std:: prefix.
You cannot have the syntax CDatabase all_emps(records); in a class definition. It gets treated as a member function declaration, where you specify the parameter type as records (parameter name is not required, the return type is CDatabase). records is not a type.
You have two choices:
Use list initialization:
Database all_emps{records};
Use constructor and member initializer list:
MyClass
{
vector<CEmployee*> records;
CDatabase all_emps;
public:
MyClass(/* possibly vector<CEmployee*> const& records*/) :
records(/* possibly records*/),
all_emps(records)
{
}
...
};
I need to establish a hash table using a hasher different from the default one, so I write something like:
class foo {
public:
...
private:
struct myhasher {
size_t operator() (myclass bar) { return hash_calculation bar; }
}
static size_t hash_calculation (myclass bar) {
// do some calculation
}
hash_map<myclass, myhasher> myhashmap;
}
It works. Now for some reason I have to write a non-static member function to replace hash_calculation, say, it needs a non-static member of the class as an argument. Then the whole thing failed because I cannot use a non-static method in a nested struct.
This is somehow similar to another widely discussed problem: how to use a non-static function to do comparison or sorting. See for example:
Using a non-static class member inside a comparison function
and
C++ std list sort with custom comparator that depends on an member variable for the object instance . They both established a functor instead of a function as the comparator. However in my case this trick does not work because I need a class name inside the hash_map definition, not a specific struct object. What should I do? Thanks in advance for your help!
You can't. How is the hash_map supposed to know which instance of myhasher should be used when calling myhaser::hash_calculation?
hash_map isn't part of the standard C++ library, not even in C++11, so it's a custom class, and you have included no information about how it works. If there is a way for it to take some sort of constructor argument for which myhasher it should use, you're in luck. But it doesn't sound like it.
Also, you're using pass by value when you probably mean to pass in a const reference. Passing by value is likely going to be really slow and inefficient.
The standard "hash-map", i.e., std::unordered_map<K, V, H, E, A> takes a hash object of type H as constructor argument. A copy of this object is used to determine the hash for the object by way of the function call operator. This way can provide some context. Obviously, you were already using a non-static function call operator but you choose to delegate to a static member.
I'm working on performing some operations on a polymorphic doubly-linked list, and I seem to be having some issues with that.
I'm trying to pass a instance of the class to this function:
void performoperator(List<string> list, string operator, int &OpCount){
//...
}
and I'm trying to call it as such:
List<string> list;
//...
performoperator(list, temp, OpCount);
The compiler doesn't appear to accept the way in which I'm calling the function, and I'm fairly certain the issue is with the templated class somehow. What am I doing improperly?
Edit: Resolved, won't let me post as a solution as I'm a new user.
The issue was the I needed to pass the list by reference.
void performoperator(List<string> list, string operator, int &OpCount)
^^^^^^^^
operator is a reserved keyword in C++; you cannot use it as a variable name.
Just a guess: You put template code in cpp file not in your header file