i think i found another solution for the problem.
by the way tanx, so whats your suggestion in this case?
Printable View
i came up wrtting this code , i used another map to store all of the statements ( lines) entered by user with its line number, now i can finally get rid of that stupid map element index!
anyways , when i try to compile this i get an error, i dont know why! maybe because i m trying to use a function im getting it !( proly im not doing it right)
the error im getting is :
would anyone help me correct this ?Quote:
error: no match for 'operator=' in 'iter = (+InputtedLines)->std::map<_Key, _Tp, _Compare, _Alloc>::begin [with _Key = int, _Tp = std::string, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, std::string> >]()'|
Code:#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
~~;
~~;
index = variable_location = Get_Variable_Location(StringCollector,tokenstr);
return 0;
}
Code://in the name of God
#include <map>
#include <string>
#include <iostream>
using namespace std;
int Get_Variable_Location(const map<int,string>& InputtedLines, string token)
{
int index = 0;
map<int,string>::iterator iter;
iter = InputtedLines.begin();
while ( iter != InputtedLines.end() )
{
if ( (*iter).second == token )
{
index = (*iter).first;
}
else
{
iter++;
}
}
return index;
}//end of function
What kind of error?
I don't see you including the header with the declaration of that function.
Error message:
( this indicates this line:Code:error: no match for 'operator=' in 'iter = (+InputtedLines)->std::map<_Key, _Tp, _Compare, _Alloc>::begin [with _Key = int, _Tp = std::string, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, std::string> >]()'|
)Quote:
iter = InputtedLines.begin();
and about declaration! its just pseudo main! i just forgot to add that!
the actual function is placed in a separate headerfile
so in main i have
there is a problem with function! what could be the cause?Code:#include <iostream>
#include "Get_variable_index.h"
int main()
{
~~;
~~;
index = variable_location = Get_Variable_Location(StringCollector,tokenstr);
return 0;
}
The map is passed as const reference, and so the iterator should be
Code:map<int,string>::const_iterator iter;
anyone can help me get this one? why do we have to declare a const iterator when we are dealing with a const map!? why is it obligatory ?
The difference between const_iterator and iterator is that the first allows only read access, which makes sense if the map is const and not meant to be modified.
When the map instance is const, begin() and end() methods return a const_iterator. If the container is non-const, these methods return iterator. iterator can be implicitly converted to const_iterator (you can always assign the result of begin() and end() to a const_iterator) but not the other way round.