2
votes

I am working on Agent modeling project and decided to use repast for that. I have pre-installed bunch of libraries before and downloaded Repast source and trying include it in project. But suddenly getting error which I can not understand.

error: no match for ‘operator+’ in ‘std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator](((const char*)"_")) + boost::filesystem3::path::filename() const()’

CODE:

NCDataSet::NCDataSet(std::string file, const Schedule& schedule) :
file_(file), schedule_(&schedule), start(0), open(true) 
{
    rank = RepastProcess::instance()->rank();
    if (rank == 0) {
        fs::path filepath(file);
        if (!fs::exists(filepath.parent_path())) {
            fs::create_directories(filepath.parent_path());
        } else if (fs::exists(filepath)) {
            string ts;
            repast::timestamp2(ts);
            fs::path to(filepath.parent_path() / (ts + "_" + filepath.filename()));
        fs::rename(filepath, to);
    }
}
}
ERROR LINE: fs::path to(filepath.parent_path() / (ts + "_" + filepath.filename()));

Thanks!!!

1
Is that the only error? If not, could you post the full compiler output?hmjd

1 Answers

1
votes

The error indicates that it can't match the operator+, ie you're trying append two invalid types.

It looks like path::filename doesn't return a std::string.

class path {
  // ...
  path  filename() const;
  // ...
};

It's reasonable to think about infix operators as keeping the type of the left hand side of the operation. In this case, std::string doesn't know anything about boost or filesystem::path.

So you likely need to change the offending line to something like this:

fs::path to(filepath.parent_path() / (ts + "_" + filepath.filename().string() ));

I find when it isn't immediately obvious how a bunch of inline operations are causing an error, it's a good practice to separate everything on to it's own line. In this case, it would even make the code a bit clearer as to your intention.

std::string old_filename(filepath.filename().string());
std::string new_filename = ts +"_"+ old_filename;
fs::path to( filepath.parent_path() / new_filename);