views:

243

answers:

2

I'm writing some code that utilizes the boost filesystem library. Here is an excerpt of my code:

artist = (this->find_diff(paths_iterator->parent_path(), this->m_input_path) == 1) ? (*(paths_iterator->parent_path().end() - 1)) : (*(paths_iterator->parent_path().end() - 2));
album = (this->find_diff(paths_iterator->parent_path(), this->m_input_path) == 1) ? "" : (*(paths_iterator->parent_path().end() - 1));

Types:

artist and album are of type std::string
this->find_diff returns an int
this->m_input_path is a std::string
paths_iterator is of type std::vector(open bracket)boost::filesystem::path>::iterator

I get a compile error:

error C2039: 'advance' : is not a member of 'boost::filesystem::basic_path<String,Traits>::iterator'    d:\development\libraries\boost\boost\iterator\iterator_facade.hpp on line 546

This code is part of a program that outputs a batch script that uses lame.exe to convert files into mp3s. The music library this is designed for has the format:

root/artist/song

OR

root/artist/album/song

this->m_input_path is the path to root.

I'm not sure if I'm approaching the problem properly. If I am, how do I fix the error that I am getting?

EDIT:

My code is now:

 boost::filesystem::path::iterator end_path_itr = paths_iterator->parent_path().end();
 if(this->find_diff(paths_iterator->parent_path(), this->m_input_path) == 1) /* For cases where: /root/artist/song */
 {
  album = "";
  end_path_itr--;
  artist = *end_path_itr;
 }
 else /* For cases where: /root/artist/album/song */
 {
  end_path_itr--;
  album = *end_path_itr;
  end_path_itr--; <-- Crash Here
  artist = *end_path_itr;
 }

The error that I now get is:

Assertion failed: itr.m_pos && "basic_path::iterator decrement pat begin()", file ... boost\filesystem\path.hpp, line 1444
+3  A: 

basic_path::iterator is a bidirectional iterator. So arithmetic with -1 and -2 is not allowed. Operators + and - between an iterator and an integer value is defined for a RandomAccessIterator.

Instead of using .end()-1, you could resort to using --.

Peter Jansson
+1  A: 

Your new error indicates that your end_path_iter doesn't have enough elements (should that be "decrement past begin"?), i.e. your path is shorter than you expect.

ZoogieZork
Ah, thats' the problem, I shouldn't have used the parent_path in paths_iterator->parent_path().end(); Thanks for your help.
LM