Error with std::string::find() function - c++

I get a runtime error stating that the list iterator is not incrementable.
void extractData::compareInventoryRecipe(string recipeLine)
{
list<double>::iterator invenIterAmount = inventoryAmount.begin();
list<double>::iterator recipeIterAmount = recipeAmount.begin();
for (list<string>::iterator invenIter != inventoryItem.begin(); invenIter !=inventoryItem.end(); ++invenIter)
{
++invenIterAmount;
size_t found;
found = recipeLine.find(*invenIter);
}
}
The error occurs on the line with found = recipeLine.find(*invenIter).
The members inventoryAmount, recipeAmount, and inventoryItem are within the protected scope of my class. This function is also a public function of my class. Can someone please help me with this?
EDIT: The exact error says Debug Assertion failed. It lists the line and it says Expression list iterator not incrementable. I am sure which line the error occurs in because VS makes a breakpoint there.
EDIT2: the for loop was mistyped when I typed it in the forum. It still gives the same error.

Related

exception: std::out_of_range at memory location when using erase

I know what the error means and I know why it's throwing the error. However I do not understand as to how to resolve it. I have already read the other answers here on StackOverflow, which mostly tell what the error means.
I basically loop through elements, let the elements do something and then at the very end (outside of the loop), at the bottom of my method I use:
void someMethod()
{
for (Wagon& wagon: wagons)
{
//some code
}
wagons.erase(std::remove_if(wagons.begin(), wagons.end(), [](const Wagon& wagon) { return !wagon.active; }), wagons.end());
}
The second iteration it throws exception: std::out_of_range at memory location.
Basically what I am trying to do is remove inactive wagons, so they do not get iterated through the second time it calls the method which loops through the wagons.

How can I retrieve a specific member from a class with a getter inside of a function that uses an iterator inside of a loop?

I'm trying to make a function that returns a class (CTaxGroup) by taking as parameter a TaxGroupId.
The function will loop through a vector with an iterator.
int m_TaxGroupId;
CTaxGroup CTaxGroupVector::FetchTaxGroup(int TaxGroupId)
{
CTaxGroup l_TaxGroup;
std::vector<CTaxGroup>::iterator l_iterator =m_TaxGroupVector.begin();
for(l_iterator; l_iterator != m_TaxGroupVector.end(); l_iterator++)
{
int l_TaxGroupId = *l_iterator->GetTaxGroupId();
if(l_TaxGroupId == TaxGroupId)
{
l_TaxGroup = *l_iterator;
}
}
return l_TaxGroup;
}
std::vector<CTaxGroup> CTaxGroupVector::GetTaxGroupVector()
{
return m_TaxGroupVector;
}
At line 7, I get an error from my IDE (RAD Studio XE4) which states :
"E2062 Invalid indirection".
I know that it was something to do with the pointer, but I'm not sure what is my error.
I'm expecting that line 7 would return me an integer.
Without testing, since you don't provide a compilable program, it's likely that you should put parentheses around the initial dereferencing, (*l_iterator)->GetTaxGroupId(), so that the compiler knows you're trying to access a member of a CTaxGroup, not a member of an iterator.

c++ error handling return values error returned

I have a class that I have been given some error input handling for. It's a class that has been given it's own "extraction" operator and I've been asked to implement the code that I've been given. The problem I'm having is that the code I should use looks similar to this.
try {
while (some condition)
{....implemented code....}
} catch (runtime_error& e) {
cerr << e.what() << endl;
return 1;
}
The problem I am having compiling this is it doesn't seem to like the "return 1" value it gives me an error of:
invalid initialization of non-const reference of type ‘std::istream& {aka std::basic_istream<char>&}’ from an rvalue of type ‘int’
If I remove the return value it compiles straight but then the program fails to execute once it hits the area where it is trying to do the try statement. As mentioned the code I have there is the above is the example code we are supposed to implement so I assumed straight out of the box it would work. My condition for the while loop was
while (!std::cin.fail())
as I assumed I'd want to keep getting input until it fails for some reason. Why would the return values in this case be causing a problem?
Psychic debugging indicates:
your enclosing function has a signature of the form
std::istream& func_name(/*parameter list goes here*/)
Hence the compilation error

I am receiving the error 'List::showList' : function-style initializer appears to be a function definition' on a function in my cpp file

GithubLinkhttps://github.com/leomdmfiel/Assignment/tree/master/BankManager/BankManager
First here's my header:
class List
{
protected:
Node* head;
public:
List(object data);
void addNode(Node* headRef, object data);
void removeNode();
void showList(Node* headRef);
};
And here is the function showList
void List::showList(Node* headRef)
{ Node* current = headRef;
while (current != NULL)
{
cout << current->retrieveData();
current = current->retrieveNext();
}
}
For some reason at compile it keeps giving me the error error C2448: 'List::showList' : function-style initializer appears to be a function definition even though I've copied the declaration letter by letter. So I'm at a loss and looking for help.
Now that I've seen the code you have an extra semi-colon
#include"List.h";
should be
#include"List.h"
I would have expected a better error message though, I must admit.
Nothing seems to be wrong with the function you gave, check the code above the function in case you forgot a semi-colon.
edit: Make sure you also check where the function is located, make sure it is in the right file, it could be something as small as the wrong file.
Try rebuilding the solution.
Sometimes in Visual Studio there are problems linking files. The potential cause of this is beyond my level of understanding, but sometimes rebuilding the solution cures a problem like this.
So it could be that the cpp file is out-of-date so to speak.
Personally, from what I can see, there doesn't appear to be an issue.

Python exception text on syntax errors (boost library)

I've got this code snnipet (the whole program compiles and links correctly):
...
try
{
boost::python::exec_file(
"myscript.py", // this file contains a syntax error
my_main_namespace,
my_local_namespace
);
return true;
}
catch(const boost::python::error_already_set &)
{
PyObject *ptype, *pvalue, *ptraceback;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
// the next line crashes on syntax error
std::string error = boost::python::extract<std::string>(pvalue);
...
}
The file that the program tries to execute has a syntax error, so an exception is thrown. When the program tries to get the error message crashes...
The code works well with run-time errors but somehow crashes with syntax errors.
How can i get the error string with this kind of errors?
Thanks in advance
From the documentation of PyErr_Fetch: "The value and traceback object may be NULL even when the type object is not". You should check whether pvalue is NULL or not before trying to extract the value.
std::string error;
if(pvalue != NULL) {
error = boost::python::extract<std::string>(pvalue);
}
If you want to check whether the exception is a SyntaxError you can compare ptype against the exception types listed here.
To answer anymore specifically I would need a backtrace from the point where it crashed.
Edit
pvalue is an exception object, not a str instance so you should use PyObject_Str to get the string representation of the exception.
You may need to call PyErr_NormalizeException first to turn pvalue into the correct exception type.