File reading error in Reader class implementation - c++

I'm trying to implement a Reader class for my own hobby programming language which I'm working on. The job of the reader would be very simple, to read the source file, and remove comments.
Here's the definition of the Reader class:
// reader.hh
// Contains Reader Class specifications
#ifndef PHI_SRC_FRONTEND_READER_HH
#define PHI_SRC_FRONTEND_READER_HH
#include "errhandler.hh"
class Reader
{
public:
Reader() = default;
auto val() const -> String const & { return val_; }
void read(String const &filename);
explicit operator bool() const { return success; }
auto operator!() const -> bool { return !success; }
friend auto operator==(Reader const &lhs, Reader const &rhs) -> bool
{
return lhs.val_ == rhs.val_;
}
friend auto operator!=(Reader const &lhs, Reader const &rhs) -> bool
{
return lhs.val_ != rhs.val_;
}
friend auto operator<<(std::ostream &stream, Reader const &read) -> std::ostream &
{
return stream << read.val_;
}
private:
String val_;
bool success;
};
#endif
Previously, I used a very simple placeholder for the Reader's read function. It basically copied everything inside the file using an istreambuf_iterator
void Reader::read(String const &filename)
{
val_.clear();
success = true; // success flag, true by default
auto file = std::ifstream{filename};
if(!file)
{
log_error(Error{Error::Type::ReadError, "Could not open file"});
success = false;
}
val_.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
// Read entire file into val_
}
This worked fine, it passed the unit tests, and I manually checked the output as well, which was also fine.
But this was just a placeholder, the actual Reader needed to remove comments. Which made me implement this:
// reader.cc
// Contains Reader Class Implementation
// Work In Progress, placeholders being used for now
#include <fstream>
#include <sstream>
#include "reader.hh"
void Reader::read(String const &filename)
{
val_.clear();
success = true; // success flag, true by default
auto inStringLiteral = false;
// Variable to determine if the reader is currently reading a string literal
// (enclosed in double quotes)
// In order to not mistake '//' inside literals as comments"
auto file = std::ifstream{filename};
if(!file)
{
log_error(Error{Error::Type::ReadError, "Cannot open file: " + filename});
success = false;
return;
}
for (unsigned char c; file >> c; )
{
// ASCII characters only use 7 bits, which is up to 127
// So value of an ascii char must be lesser than 128
if (c < 128)
{
if(c == '"')
{
inStringLiteral = !inStringLiteral; // flip the value of the boolean
}
if(!inStringLiteral && c == '/')
{
// If we're not inside a string literal enclosed in quotes, and find a backslash
// Peek at the next character to check if it is a backslash
// In case two consecutive backslashes are found, treat it as a comment and
// ignore everything until the end of line
if(file >> c)
{
if(c == '/')
{
// keep reading until a newline is found
while(file >> c && c != '\n')
{
}
}
else
{
c = '/';
file.unget();
}
}
else
{
c = '/';
}
}
val_ += c;
}
else
{
log_error(Error{Error::Type::ReadError, "Unrecognized character(s) found in file: " + filename});
success = false;
return;
}
}
}
However, this weirdly causes the Unit Tests to fail.. I've compared the outputs of both versions of the read function, both have (apparently) the exact same output. Note I haven't actually checked for equality of the strings, but they do look the same. I've tried to find the reason for the error a lot but have failed....
Here's the Unit Test I'm using for the Reader (using GoogleTest):
#include <gtest/gtest.h>
#include "frontend/reader.hh"
TEST(ReaderTest, BaseTestCase)
{
auto TestReader = Reader{};
auto const ExpectedOutput = String{
R"delim(Int test = 0;
String test2 = "abcdefgh";
Float test3 = 0.9876;
)delim"};
TestReader.read("TestFiles/ReaderTest_BaseTestCase.phi");
ASSERT_FALSE(!TestReader);
ASSERT_EQ(TestReader.val(), ExpectedOutput);
// If Reader Base Test Case fails, no need to continue next tests
}
TEST(ReaderTest, Should_Fail_When_FileDoesNotExist)
{
auto TestReader = Reader{};
TestReader.read("Non_existent_test_file.txt");
EXPECT_TRUE(!TestReader);
}
As I mentioned before, it worked quite fine for the first placeholder version, but the actual read function doesn't seem to pass the tests.... The weird thing is, the sample file doesn't even have any comments, here's the sample file the Reader reads:
Int test = 0;
String test2 = "abcdefgh";
Float test3 = 0.9876;
(Yes, that's literally it. Also as I mentioned before, the language the Reader is reading is not C++, but rather a homemade language I'm working on reading, but that's probably irrelevant for this question).
Oh and in case you need to compile this, you'd need to implementation and definition of errhandler (errhandler.hh and errhandler.cc), I'll put them here as well:
Declaration (errhandler.hh):
// errhandler.hh
// Contains Phi Error Handling specifications
// Mostly complete, minor changes still might be made though
#ifndef PHI_SRC_FRONTEND_ERRHANDLER_HH
#define PHI_SRC_FRONTEND_ERRHANDLER_HH
#include <iostream>
#include "utils.hh"
class Error
{
public:
enum class Type : unsigned char
{
ReadError, LexError, ParseError, SemanticError, InterpretError
};
Error() = delete;
Error(Type type__, String const &val__) : type_(type__), val_(val__) {}
auto type() const -> Type { return type_; }
auto val() const -> String { return val_; }
friend auto operator<<(std::ostream &stream, Error const &error) -> std::ostream&
{
return stream << error.val();
}
private:
Type type_;
String val_;
};
class ErrorLog
{
public:
using iterator = Vector<Error>::iterator;
using const_iterator = Vector<Error>::const_iterator;
using reverse_iterator = Vector<Error>::reverse_iterator;
using const_reverse_iterator = Vector<Error>::const_reverse_iterator;
void push(Error const &error) { errors.push_back(error); }
void pop() { errors.pop_back(); }
auto size() const -> Size { return errors.size(); }
auto operator[](Size index) -> Error& { return errors[index]; }
auto operator[](Size index) const -> Error const& { return errors[index]; }
auto begin() -> iterator { return errors.begin(); }
auto end() -> iterator { return errors.end(); }
auto cbegin() const -> const_iterator { return errors.cbegin(); }
auto cend() const -> const_iterator { return errors.cend(); }
auto rbegin() -> reverse_iterator { return errors.rbegin(); }
auto rend() -> reverse_iterator { return errors.rend(); }
auto crbegin() -> const_reverse_iterator { return errors.crbegin(); }
auto crend() -> const_reverse_iterator { return errors.crend(); }
friend auto operator<<(std::ostream &stream, ErrorLog const &error_log) -> std::ostream&
{
for (Size i = 0; i < error_log.size(); i++)
stream << error_log[i];
return stream;
}
private:
Vector<Error> errors;
};
void log_error(Error const &error);
void show_errors(std::ostream& stream);
extern ErrorLog errlog;
// The global error log to be used by every part of the Phi system
// To be declared in main()
#endif
Definition (errhandler.cc):
// errhandler.cc
// Contains Phi Error Handling implementation
// Work In Progress, placeholders are temporarily being used
#include "errhandler.hh"
void log_error(Error const& error)
{
errlog.push(error);
}
void show_errors(std::ostream& stream)
{
stream << errlog;
}
And you also, finally, need the utils header, containing the utilities
// utils.hh
// Contains globally used utility functions, templates, using declarations, etc.
#ifndef PHI_SRC_UTILS_HH
#define PHI_SRC_UTILS_HH
#include <string>
#include <vector>
#include <limits>
#define UNUSED(x) (void)x
template <typename T>
using Vector = std::vector<T>;
using String = std::string;
using Size = std::size_t;
class bad_narrow_cast : public std::bad_cast
{
public:
bad_narrow_cast(const char* message) : what_(message)
{
}
char const *what() { return what_; }
private:
char const *what_;
};
template <typename Target, typename Base> static inline
typename std::enable_if<std::numeric_limits<Target>::is_specialized,
Target>::type narrow_cast(Base const &base)
{
if(base > static_cast<Base>(std::numeric_limits<Target>::max()) ||
base < static_cast<Base>(std::numeric_limits<Target>::min()))
{
throw(bad_narrow_cast((String() + "Invalid narrowing conversation from type " +
typeid(Target).name() + " to type " + typeid(Base).name()).c_str()));
}
return static_cast<Target>(base);
}
template <typename Target, typename Base> static inline
typename std::enable_if<!std::numeric_limits<Target>::is_specialized,
Target>::type narrow_cast(Base const &base)
{
Target target = static_cast<Target>(base);
Base narrowed_base = static_cast<Base>(target);
if (base == narrowed_base)
return target;
throw(bad_narrow_cast((String() + "Invalid narrowing conversation from type " +
typeid(Target).name() + " to type " + typeid(Base).name()).c_str()));
}
#endif
This question really halted my progress on the project. Helping me solve it would be really helpful

So, people in the comments really helped me, and suggested me to use >> for reading characters from the stream instead of eof() and get(), as eof() is unreliable.. But even that didn't solve the problem. Until I, through some googling, figured it out myself, I had to use std::noskipws, in order to make the >> operator not skip whitespaces, and then it worked. Thanks for all the help, I really appreciate it

Related

TransformerClangTidyCheck drops required parentheses around bound `expr()`

I'd like to rename a function on one of the classes:
class Container {
public:
int count() const; // old
int size() const; // new
};
So I've written a TransformerClangTidyCheck along the lines of the tutorial (https://clang.llvm.org/docs/ClangTransformerTutorial.html#example-rewriting-method-calls):
ReproParenIssueCheck::ReproParenIssueCheck(llvm::StringRef Name, ClangTidyContext *Context)
: utils::TransformerClangTidyCheck(Name, Context)
{
const std::string o = "object";
auto hasTypeIgnoringPointer = [](auto type) { return anyOf(hasType(type), hasType(pointsTo(type))); };
auto derivedFromClass = [&](StringRef Class) {
auto exprOfDeclaredType = [&](auto decl) {
return hasTypeIgnoringPointer(hasUnqualifiedDesugaredType(recordType(hasDeclaration(decl))));
};
return exprOfDeclaredType(cxxRecordDecl(isSameOrDerivedFrom(hasName(Class))));
};
auto renameMethod = [&] (StringRef Class, StringRef from, StringRef to) {
return makeRule(cxxMemberCallExpr(on(expr(derivedFromClass(Class)).bind(o)),
callee(cxxMethodDecl(hasName(from), parameterCountIs(0)))),
changeTo(cat(access(o, cat(to)), "()")),
cat("use '", to, "' instead of '", from, "'"));
};
setRule(renameMethod("Container", "count", "size"));
}
This works:
int test_ok(const Container *p) {
- return p->count();
+ return p->size();
}
except that changeTo() is losing the required ParenExpr in the expr() bound to o:
int test_fail(const void *p) {
- return ((const Container*)p)->count();
+ return ((const Container*)p)->size(); // expected
+ return (const Container*)p->size(); // actual
}
Bug in changeTo() or am I missing something? It looks like any ParenExpr is dropped:
int test_fail2(const Container &c) {
- return (c).count();
+ return (c).size(); // expected
+ return c.size(); // actual
}
It turns out that the on() matcher drops the parentheses:
AST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>,
InnerMatcher) {
const Expr *ExprNode = Node.getImplicitObjectArgument()
->IgnoreParenImpCasts(); // <-- HERE
return (ExprNode != nullptr &&
InnerMatcher.matches(*ExprNode, Finder, Builder));
}
If I create a new matcher that only ignores implicit casts:
AST_MATCHER_P(CXXMemberCallExpr, onIgnoringImpCasts, Matcher<Expr>,
InnerMatcher) {
const Expr *ExprNode = Node.getImplicitObjectArgument()
->IgnoreImpCasts();
return (ExprNode != nullptr &&
InnerMatcher.matches(*ExprNode, Finder, Builder));
and use that instead of on(), both test cases pass.

Is there anything like C++ default object method

I have the following templated merge sort program:
#include <iostream>
#include <vector>
#include <string>
// trying to create a default method call
class CInstance {
private:
std::string str_;
public:
CInstance(const std::string& str) : str_(str) {}
bool const operator>(const CInstance& that){ return (this->str_.size() > that.str_.size());}
};
template<class T>
class CObj {
private:
T val;
public:
CObj(const T n) : val(n) {}
T Get() { return val; }
};
template<class T>
using vcobj = std::vector<CObj<T>>;
template<class T>
void display(vcobj<T>& v) {
for (auto &i : v) {
std::cout << i.Get() << " ";
}
std::cout << "\n";
}
template<class T>
vcobj<T> Merge(vcobj<T>& lv, vcobj<T>& rv) {
vcobj<T> ret;
auto lsize = lv.size();
auto rsize = rv.size();
unsigned int lpin = 0,
rpin = 0;
while(lpin < lsize && rpin < rsize) {
if(lv.at(lpin).Get() > rv.at(rpin).Get()) {
ret.emplace_back(rv.at(rpin).Get());
rpin++;
}
else {
ret.emplace_back(lv.at(lpin).Get());
lpin++;
}
}
for (auto i=lpin; i<lsize; i++) {
ret.emplace_back(lv.at(i).Get());
}
for (auto i=rpin; i<rsize; i++) {
ret.emplace_back(rv.at(i).Get());
}
return ret;
}
template<class T>
vcobj<T> Sort(const vcobj<T>& v) {
vcobj<T> ret;
auto size = v.size();
if(size == 0) {
return ret;
}
if(size > 1) {
auto mid = size / 2;
vcobj<T> l(v.begin(), v.begin()+mid);
auto lv = Sort(l);
vcobj<T> r(v.begin()+mid, v.end());
auto rv = Sort(r);
ret = Merge(lv, rv);
}
else {
ret = v;
}
return ret;
}
int main() {
{
vcobj<int> v = {4, 5, 2, 1, 9, 6, 10, 8, 15, 3, 7};
display(v);
auto sorted = Sort(v);
display(sorted);
}
{
vcobj<float> v = {0.01, 0.001, 0.002, 0.009, 0.010, 0.0003, 0.00001};
display(v);
auto sorted = Sort(v);
display(sorted);
}
{
vcobj<std::string> v = {{"pineapple"}, {"jackfruit"}, {"mango"}, {"apple"}, {"banana"}};
display(v);
auto sorted = Sort(v);
display(sorted);
}
// causing problem
{
vcobj<CInstance> v = {{"pineapple"}, {"jackfruit"}, {"mango"}, {"apple"}, {"banana"}};
display(v);
auto sorted = Sort(v);
display(sorted);
}
return 0;
}
In all of the above types, I can simply call the object and it extracts the data which looks like calling a default get() method. Is there a way to make objects of class CInstance trigger a methos, when used just alone.
example:
I could do something like
CInstance obj;
std::cout << obj;
And that will call a default method in CInstance what every it may be.
As already mentioned in the other answer you can create your own operator<< function:
std::ostream & operator<<(std::ostream &stream, const CInstance &obj) {
// stream << whatever you want to output
return stream;
}
You could also define a conversion operator. But you should think twice before you use them. They can lead to problems that are not easy to debug, especially when explicit is omitted. You generally should not use those for logging/debugging purposes. If your type represents a string and you use it to allow an easy conversion to an std::string then it might be fine.
#include <iostream>
#include <string>
class CInstance {
std::string str_ = "test";
public:
explicit operator const std::string () const { return str_; }
};
int main() {
CInstance obj;
std::cout << (std::string)obj << std::endl;
return 0;
}
If you can guarantee that the lifetime of the returned const char * is still valid after the call you could also do something like (but I would avoid that solution):
#include <iostream>
#include <string>
class CInstance {
std::string str_ = "test";
public:
operator const char *() const { return str_.c_str(); }
};
int main() {
CInstance t;
std::cout << t << std::endl;
return 0;
}
Personally, I would go with the first solution. But that really depends if you actually have a string representation of CInstance or if you want to display something for debugging purposes in a different format. I however would avoid the last non-explicit version with the const char * conversion operator.
In this exact case, you define an operator<< method like so:
std::ostream & operator<<(std::ostream &stream, const CInstance &obj) {
... output obj however you want to the stream. For instance:
stream << obj.getAge();
return stream;
}

Understanding operator overloading & iterator why does it print out "wrhrwwr"?

In the following code the output is "wrhrwwr", I try to understand what the iterator is doing, also how the "++" is overloaded. It seems like it somehow skips the 'e'. However, the code is very unclear to me, maybe I can be helped.
Thank you
#include <iostream>
using namespace std;
class datas {
private:
const char *string,marker;
const int len;
public:
class hophop {
private:
const char *pos, next;
public:
char operator *() { return *pos; }
hophop operator ++() {++pos;while (*(pos+1)!=next) ++pos; return *this; }
bool operator !=(hophop &o) { return pos < o.pos; }
hophop(const char *p, char m) : pos(p),next(m) {}
};
typedef hophop iterator;
iterator begin() { return hophop (string, marker); }
itrator end () { return hophop(string +len ,marker); }
datas(const char *d,int l, char m) : string (d), len(l),marker(m){}
};
int main( void ) {
datas chars ("we are where we were", 20, 'e');
for (char c: chars)
cout << c;
return 0;
}
It will be easier to see by pulling hophop out of the datas class. Now you can see the hophop constructor and what it is up to. I would have removed the return value of the ++operator, set it to void, to point out it does nothing here.
#include <iostream>
class hophop {
private:
const char* pos, next;
public:
hophop(const char* p, char m) : pos(p), next(m) {}
char operator *() { return *pos; }
hophop operator ++() {
++pos;
while (*(pos + 1) != next)
++pos;
return *this;
}
bool operator !=(const hophop& o) { return pos < o.pos; }
};
class datas {
private:
using iterator = hophop;
const char* string, marker;
const int len;
public:
datas(const char* d, int l, char m) : string(d), len(l), marker(m) {}
iterator begin() { return hophop(string, marker); }
iterator end() { return hophop(string + len, marker); }
};
int main(void) {
datas chars("we are where we were", 20, 'e');
// w r h r w w r
for (char c : chars)
std::cout << c;
std::cout << "\nusing a non range based for loop:" << std::endl;
for (hophop it = chars.begin(); it != chars.end(); ++it)
std::cout << *it;
std::cout << "\nor where the return value could be used:" << std::endl;
auto it = chars.begin();
std::cout << *it;
for (; it != chars.end();)
std::cout << *++it;
}
So now it may be easier to see how the hophop ++ operator is working. operator *() gets called at the beginning of the loop so no matter what the first character is, it gets retrieved. Then the ++operator is called and it moves the iterator at least once forward and until it matches next. Then it returns the character before the match. Look at the comment in main. The first and every character before the e is returned.
If you have not used a debugger much, you should. by putting a break point in operator++ you can see what is happening.
UPDATE
I had previously set the return value of the ++operator to void. As #JaMiT points out, it is appropriate for the operator to return *this. I've also added two more loops, they should be clearer than using a range based loop. The third example actually uses the return value of the ++operator, the first two loops don't.
And, get in the habit of not using namespace std; It will save you from troubles down the road.

Is it possible to define a variable that can be set only once?

I know of const, that can't be changed after creation. But I was wondering if there is a way to declare a variable that you set only once and after that, can't overwrite.
In my code, I would like to avoid the bool variable by having an nFirst that, once set to nIdx, can't be set to the new value of nIdx.
My code:
int nFirst = 0;
int nIdx = 0;
bool bFound = false;
BOOST_FOREACH(Foo* pFoo, aArray)
{
if (pFoo!= NULL)
{
pFoo->DoSmth();
if (!bFound)
{
nFirst= nIdx;
bFound = true;
}
}
nIdx++;
}
Pretty easy to roll your own.
template<typename T>
class SetOnce
{
public:
SetOnce(T init) : m_Val(init)
{}
SetOnce<T>& operator=(const T& other)
{
std::call_once(m_OnceFlag, [&]()
{
m_Val = other;
});
return *this;
}
const T& get() { return m_Val; }
private:
T m_Val;
std::once_flag m_OnceFlag;
};
Then just use the wrapper class for your variable.
SetOnce<int> nFirst(0);
nFirst= 1;
nFirst= 2;
nFirst= 3;
std::cout << nFirst.get() << std::endl;
Outputs:
1
I would like to avoid the bool variable
You can check nFirst itself, based on the fact that it won't be set a negative number. Such as:
int nFirst = -1;
int nIdx = 0;
BOOST_FOREACH(Foo* pFoo, aArray)
{
if (pFoo != NULL)
{
pFoo->DoSmth();
if (nFirst == -1)
{
nFirst = nIdx;
}
}
nIdx++;
}
Similar to cocarin's, but throws exception instead of silently ignoring assignment:
template <typename T, typename Counter = unsigned char>
class SetOnce {
public:
SetOnce(const T& initval = T(), const Counter& initcount = 1):
val(initval), counter(initcount) {}
SetOnce(const SetOnce&) = default;
SetOnce<T, Counter>& operator=(const T& newval) {
if (counter) {
--counter;
val = newval;
return *this;
}
else throw "Some error";
}
operator const T&() const { return val; } // "getter"
protected:
T val;
Counter counter;
};
Usage:
SetOnce<int> x = 42;
std::cout << x << '\n'; // => 42
x = 4;
// x = 5; // fails
std::cout << x << '\n'; // => 4
Online demo
Your question is about avoiding the bool but also implies the need for const-ness.
To avoid the bool, I'd use a boost::optional like this:
boost::optional<int> nFirst;
// ..
if (!nFirst) nFirst = nIdx;
// and now you can use *nFirst to get its value
Then, you can enforce logical (rather than literal) const-ness like this:
const boost::optional<int> nFirst;
// ..
if (!nFirst) const_cast<boost::optional<int>&>(nFirst) = nIdx;
// you can use *nFirst to get the value, any attempt to change it would cause a compile-time error
Using const_cast is not the safest practice, but in your particular case and as long as you only do it once it'd be OK. It simplifies both your code and your intentions: you do want a const, it's just that you want to defer it's initialisation for a bit.
Now, as songyuanyao suggested, you could directly use an int instead of a boost::optional, but the latter makes your intention explicit so I think it's better this way. In the end of day this is C++ while songyuanyao's solution is really a C-style one.
This is set once template. You can use this class as assurance that the value will be set and saved only once. Every next try will be canceled.
#include <iostream>
using namespace std;
template <class T>
class SetOnce;
template<class T>
std::ostream& operator<<( ostream& os, const SetOnce<T>& Obj );
template <class T>
class SetOnce
{
public:
SetOnce() {set = false; }
~SetOnce() {}
void SetValue(T newValue) { value = !set ? newValue : value; set = true; }
private:
T value;
bool set;
friend std::ostream& operator<< <>( ostream& os, const SetOnce& Obj );
public:
SetOnce<T>& operator=( const T& newValue )
{
this->SetValue(newValue);
return *this;
}
};
template<class T>
std::ostream& operator<<( ostream& os, const SetOnce<T>& Obj )
{
os << Obj.value;
return os;
}
Use case:
int main()
{
SetOnce<bool> bvar;
SetOnce<int> ivar;
SetOnce<std::string> strvar;
std::cout<<"initial values: \n"<<bvar<<" "
<<ivar<<" "<<strvar<<" \n\n";
bvar = false; //bvar.SetValue(false);
ivar = 45; //ivar.SetValue(45);
strvar = "Darth Vader"; //strvar.SetValue("Darth Vader");
std::cout<<"set values: \n"<<bvar<<" "
<<ivar<<" "<<strvar<<" \n\n";
bvar = true; //bvar.SetValue(true);
ivar = 0; //ivar.SetValue(0);
strvar = "Anakin"; //strvar.SetValue("Anakin");
std::cout<<"set again values: \n"<<bvar<<" "
<<ivar<<" "<<strvar<<" \n\n";
return 0;
}

Boost 1.59 compile error

After updating my files to boost_1.59.0 i get an ambyguous error. I can't understand what's wrong because in boost 1.43 all work's fine.
This is my boost declaration and my function.
boost::unordered_map<VID, size_t>::iterator iterTargetMap = rSkillUseInfo.TargetVIDMap.find(TargetVID);
if (rSkillUseInfo.TargetVIDMap.end() != iterTargetMap)
{
size_t MaxAttackCountPerTarget = 1;
switch (SkillID)
{
case SKILL_SAMYEON:
case SKILL_CHARYUN:
MaxAttackCountPerTarget = 3;
break;
}
if (iterTargetMap->second >= MaxAttackCountPerTarget)
{
sys_log(0, "SkillHack: Too Many Hit count from SkillID(%u) count(%u)", SkillID, iterTargetMap->second);
return false;
}
iterTargetMap->second++;
}
else
{
rSkillUseInfo.TargetVIDMap.insert( std::make_pair(TargetVID, 1) );
}
I also tried with auto in c++11
auto iterator iterTargetMap = rSkillUseInfo.TargetVIDMap.find(TargetVID);
Here is my error log gcc49 http://pastebin.com/p1KLqs9H
I can't write here the error is too big.
I'am stucked on this error for 4 days. :(
Here is vid.h
class VID
{
public:
VID() : m_id(0), m_crc(0)
{
}
VID(DWORD id, DWORD crc)
{
m_id = id;
m_crc = crc;
}
VID(const VID &rvid)
{
*this = rvid;
}
const VID & operator = (const VID & rhs)
{
m_id = rhs.m_id;
m_crc = rhs.m_crc;
return *this;
}
bool operator == (const VID & rhs) const
{
return (m_id == rhs.m_id) && (m_crc == rhs.m_crc);
}
bool operator != (const VID & rhs) const
{
return !(*this == rhs);
}
operator DWORD() const
{
return m_id;
}
void Reset()
{
m_id = 0, m_crc = 0;
}
private:
DWORD m_id;
DWORD m_crc;
};
By looking at the error, it looks like you have to define a hash function for the type VID to be able to use it as a key in a map.
Standard hash functions are already defined in the STL for basic types, but you have to define for yourself a specific one for your domain types.
Usually, it's enough to do something like this:
namespace std {
template<> struct hash<VID> {
using argument_type = VID;
using result_type = std::size_t;
result_type operator()(argument_type const& vid) const {
// what to put here depends on the type of VID
// and how you want to create the hash
}
};
}
The difficulties are usually in understanding how to create the hash. In my experience, for user defined classes, I've ever used the standard specialization with some data members, the most significant ones.
In your case, as an example, you could cast the DWORDs to a couple of unsigned ints and use them to get the hash by using std::hash<unsigned int> (I'm assuming that that's the DWORD from the Windows API, that is a 32 bit unsigned integer as far as I remember).
As already said in the comments, see here for further details.