I've encountered really strange error while working with iterators, and I am not really sure what to think about it:error: could not convert ‘0’ from ‘int’ to ‘CCarList’
CCarList CRegister::ListCars(const string& name, const string& surname) const {
auto ownerIt = findOwner(name, surname);
if (ownerIt == m_owners.end()) return 0; //Error is on this line
//...
}
findOwner:
vector<TOwner*>::const_iterator CRegister::findOwner(const string& name,
const string& surname) const
{
return lower_bound(m_owners.begin(), m_owners.end(), TOwner(name, surname),
[](const TOwner* a, const TOwner& b){return (*a) < b;});
}
m_owners is vector<TOwner*> (TOwner is struct.)
CCarList:
class CCarList {
vector<TCar*>::iterator m_it;
const vector<TCar*>::iterator m_end;
public:
CCarList(vector<TCar*>::iterator it, const vector<TCar*>::iterator end):
m_it(it), m_end(end){}
CCarList(){}
string RZ() const;
bool AtEnd() const;
void Next();
};
ListCars returns a CCarList. You try to return 0, an int. There is no conversion from int to CCarList, so the attempt fails.
Related
I can compile normal,when I use vector:
TEST(function_obj,bindMemeber1){
std::vector<Person> v {234,234,1241,1241,213,124,152,421};
std::for_each(v.begin(),v.end(), std::bind(&Person::print,std::placeholders::_1) );
}
but when I use set,something wrong:
TEST(function_obj,bindMemeber1){
std::set<Person,PersonCriterion> v{234,234,1241,1241,213,124,152,421};
std::for_each(v.begin(),v.end(), std::bind(&Person::print,std::placeholders::_1) );
}
clion's tips
The IDE tell me that something wrong.when I force IDE to compile, it also can't compile successfully .
Below is the code of Person;
class Person{
private:
size_t no;
std::string name;
public:
Person():no(0){};
Person(size_t n): no(n){};
Person(const Person& p):no(p.no),name(p.name){};
friend class PersonCriterion;
size_t getNo() const;
void print(){
std::cout<<no<<' ';
}
const std::string &getName() const;
};
class PersonCriterion{
public:
bool operator()(const Person& p1,const Person& p2){
return p1.no<=p2.no;
}
};
size_t Person::getNo() const {
return no;
}
const std::string &Person::getName() const {
return name;
}
Elements got from std::set are const-qualified; they're supposed to be non-modifiable. You should mark Person::print as const then it could be called on a const object.
class Person {
...
void print() const {
// ^^^^^
std::cout<<no<<' ';
}
...
};
BTW: Better to mark operator() in PersonCriterion as const too.
class PersonCriterion {
public:
bool operator()(const Person& p1, const Person& p2) const {
return p1.no<=p2.no;
}
};
I'm learning C++ and I've been writing a wrapper for std::map and std::string, and I've stumbled upon a problem. Whenever I add something to the map using a string as key, once I try to access that item using the exact same key it says the key is out of bounds of the map. Here's my code (irrelevant parts left out):
ADictionary.h
#ifndef ADICTIONARY_H
#define ADICTIONARY_H
#include <map>
...
template<typename KEY, typename VALUE>
class ADictionary {
public:
...
VALUE operator [](KEY key) const {
return value.at(key);
}
void add(KEY key, VALUE value) {
this->value.insert(std::make_pair(key, value));
}
...
private:
std::map<KEY, VALUE> value;
};
#endif
AString.cpp
#include "AString.h"
AString::AString() {
value = "";
}
AString::AString(const char character) {
value = character;
}
AString::AString(const char * characters) {
value = characters;
}
AString::AString(std::string text) {
value = text;
}
...
AString::operator const char *() const {
return value.c_str();
}
AString::operator const std::string() const {
return value;
}
...
ABoolean AString::operator<(AString & text) const {
return getLength() < text.getLength();
}
ABoolean AString::operator>(AString & text) const {
return text < *this;
}
ABoolean AString::operator==(AString & text) const {
return value == text.value;
}
ABoolean AString::operator!=(AString & text) const {
return !(text == *this);
}
AString & AString::operator=(AString & text) {
value = text.value;
return *this;
}
...
The code which uses the above
ADictionary<AString, AString> test;
AString a = "a";
AString b = "b";
test.add(a, b);
std::cout << test[a]; // Error occurs here, according to the program "a" is not a key in the map
I hope someone can explain to me what's going wrong. I've tried creating a dictionary with the default std::string as types and it worked correctly:
ADictionary<std::string, std::string> test;
std::string a = "a";
std::string b = "b";
test.add(a, b);
std::cout << test[a]; // No error this time
As I've said, I'm pretty new to C++ so there may be other errors. If so, feel free to point them out.
Thanks!
EDIT:
AString.h
#ifndef ASTRING_H
#define ASTRING_H
#include <string>
#include "ABoolean.h"
#include "AInteger.h"
#include "AList.h"
class ABoolean;
class AInteger;
template<typename VALUE>
class AList;
class AString {
public:
AString();
AString(const char);
AString(const char *);
AString(std::string);
~AString();
operator const char *() const;
operator const std::string() const;
operator const AInteger() const;
ABoolean operator<(AString &) const;
ABoolean operator>(AString &) const;
ABoolean operator==(AString &) const;
ABoolean operator!=(AString &) const;
AString & operator=(AString &);
AString & operator+(AString &);
AString & operator+=(AString &);
void clear();
ABoolean contains(AString) const;
AInteger getIndex(AString) const;
AInteger getLength() const;
AList<AString> getSplit(AString) const;
AString getSubstring(AInteger, AInteger) const;
void removeRange(AInteger, AInteger);
void removeSubstring(AString);
void toLowercase();
void toUppercase();
private:
std::string value;
};
AString & operator+(const char, AString &);
AString & operator+(const char *, AString &);
#endif
Your string operators appear to be incorrect.
std::map uses the less than operator by default. While you provide one for AString, the only thing it does is check the length of the string. What if the two strings are of equal length?
The correct thing to do is to lexicographically compare the characters in the string. While there is a standard library function to do this, you can use operator < of the std::string values in your class:
friend bool operator<(AString const& a, AString const& b)
{
return a.value < b.value;
}
EDIT: You may also wish to remove your conversion operators, or at least make them explicit, which prevents surprising and unwanted implicit conversions. Constructors taking one parameter (other than copy or move constructors) should also be declared explicit.
I'm trying to implement Best First Search using C++ on VS2013. Below is the code.
//node for tree
struct Node
{
Node(std::string const& s, std::string const& p)
: state(s), path(p)
{}
const std::string state;
const std::string path;
};
//heuristic functor
struct ManhattanDistance
{
std::size_t operator()(std::string const& state, std::string const& goal)
{
std::size_t ret = 0;
for (int index = 0; index != goal.size(); ++index)
{
if ('0' == state[index])
continue;
auto digit = state[index] - '0';
ret += abs(index / 3 - digit / 3) + abs(index % 3 - digit % 3);// distance(row) plus distance(col)
}
return ret;
}
};
//functor to compare nodes using the heuristic function.
template<typename HeuristicFunc>
struct GreaterThan
{
explicit GreaterThan(HeuristicFunc h, std::string const& g = "012345678")
: goal(g), heuristic(h)
{}
bool operator()(Node const& lhs, Node const& rhs) const
{
return heuristic(lhs.state, goal) > heuristic(rhs.state, goal);
return true;
}
const std::string goal;
const HeuristicFunc heuristic;
};
When testing this code in Unit Test, compiler complained that :
Error 1 error C3848: expression having type 'const ai::search::ManhattanDistance' would lose some const-volatile qualifiers in order to call 'size_t ManhattanDistance::operator ()(const std::string &,const std::string &)'
How to understand this error? How to fix it?
Your method std::size_t ManhattanDistance::operator()(std::string const& state, std::string const& goal) is not declared const, yet you try to call it on a const ManhattanDistance object. The compiler is correctly rejecting this ill-formed program.
Change the defining line to declare the method const:
std::size_t operator()(std::string const& state, std::string const& goal) const
// ^^^^^
Say I have a Storage class:
class Storage
{
public:
const string& get() const { return m_data; }
const char& get(int ind) const { return m_data[ind]; }
const string& get(int s_ind, int e_ind) const { /* TBD */ }
private:
string m_data; ///< Data is so big that part of it is stored on disk
}
Say I have a Writer class that gets const Storage& and needs to access its data.
My question, is there a way to implement:
const string& get(int s_ind, int e_ind) const;
i.e, get const access to only a part of a string.
Notes:
get() is called countless of times and it is the bottleneck of my application. I'd like to avoid allocating new objects when accessing data.
is there a way to implement:
const string& get(int s_ind, int e_ind) const;
i.e, get const access to only a part of a string.
Definitely not.
What is often done - and may resolve your bottleneck - is to create a class that stores a const char* and size_t (or equally begin and end const char*s, or iterators but there's no reason to limit this to use for data in std::strings).
You could then create an object that "references" text inside a string, and use it until any of the events that would invalidate an iterator or reference to those characters happens - see the Standard or e.g. cppreference. It's possible to support stream output, comparisons, indexing etc. driven off the std::string hosted data.
Clearly you won't be able to pass such a class to functions that hardcode std::string type, but you could write it to have a similar interface, which should lessen pain.
Just as a taster (hasn't seen a compiler / flesh out as needed)...
class Text_Ref
{
public:
Text_Ref(const char* p, size_t n) : p_(p), n_(n) { }
// intuitive values for &text_ref[x] BUT text_ref[n] may not be nul
const char& operator[](size_t o) const { return p_[n]; }
*** OR ***
// text_ref[n] is nul BUT can't use &text_ref[x]
char operator[](size_t o) const { return o == n ? '\0' : p_[n]; }
// same design trade off as the operator[] alternatives above
char at(size_t o) const
{
if (o > n) throw std::out_of_range();
return o == n ? '\0' : p_[n];
}
bool empty() const { return n == 0; }
size_t size() const { return n; }
size_t length() const { return n; }
int compare(const char* p) const
{
do
{
if (*p != *p_)
return (int)*p_ - *p;
} while (*p);
return 0;
}
bool operator< (const char* p) const { return compare(p) < 0; }
bool operator<=(const char* p) const { return compare(p) <= 0; }
bool operator==(const char* p) const { return compare(p) == 0; }
bool operator!=(const char* p) const { return compare(p) != 0; }
bool operator>=(const char* p) const { return compare(p) >= 0; }
bool operator> (const char* p) const { return compare(p) > 0; }
private:
const char* p_;
size_t n;
};
inline std::ostream& operator<<(std::ostream& os, const Text_Ref& t)
{
return os.write(t.data(), t.size());
}
I am trying to initialize a priority_queue , Here is the code
class stdnt{
public:
int indx;
int c;
int lvl;
bool operator<(const stdnt &x)
{
return this->c > x.c;
}
};
priority_queue<stdnt> pq;
But its giving me error that passing const & discards qualifiers. How else am I supposed to do this?
You need to make the operator const so that it can be called on const instances or via const references or pointers to const:
bool operator<(const stdnt &x) const
^^^^^
Alternatively, make it a non-member:
bool operator<(const stdnt &lhs, const stdnt& rhs)
{
return lhs.c > rhs.c;
}