Hello,
I am working on a "line-oriented text editor" for my CS course and I must use the List container.
I created a member function that prints all elements from my List in a specific range. I am sure there must be a better way to code this, but my current code is below:
Quick example:
List {1, 2, 3, 4, 5, 6};
print(List, 1, 2);
The output should be 1 and 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
std::list<std::string> print(std::list<std::string> list, int first_line, int last_line)
{
std::list<std::string>::iterator it = list.begin();
std::advance(it, first_line);
for (auto i = 10; i > last_line + 1; --i)
{
list.pop_back();
}
for (; it != list.end(); ++it)
{
std::cout << *it << std::endl;
}
return list;
}
|
This function runs perfectly when I compile it using an online c++ compiler and I get the output I want. Of course, I tested it alone.
However, when I include the above function in my project (inside a Class) and compile it, when I call the function I get the following error message:
a.out(2586,0x7fffbbb313c0) malloc: *** error for object 0x7fff501c76e8: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
I am using Xcode 8 to write my code, but I am compiling it from my Terminal using clang++ for C++11.
Unfortunately, I have no idea why I am getting this error. Any suggestion/help?
Thanks!
obs: first post here, if I did something wrong, just let me know! :)