Jump to content

Error while using compare

- - - - -

  • Please log in to reply
7 replies to this topic

#1
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
here is the method of class TrainingProgram. Program_list is the list of strings. This method checks if the given string is already in program_list or not. I get such an error: . C:\Users\admin\Desktop\Documents\C\training_functions.cpp|559|error: 'struct std::_List_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >' has no member named 'compare'|
Any help? Thanks in advence


bool TrainingProgram::trainingProgram_isExercise(string exercise)
{

for (it3 = program_list.begin(); it3 != program_list.end(); ++it3)
{

if (it3.compare(exercise) == 0) //error in that line
return true;

}
return false;

}

#2
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
the compare method is part of std::string? it3 is the iterator, so you want to use *it3 to get to what the iterator is actually pointing to.

#3
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
Yes, the compare is method of std::string. *it3 didn't help. The same error occurs

#4
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
try (*it3).compare, or it3->compare()

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Post code for it3 - what kind of iterator is it? Also, is program_list a std::list?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
I would just use std::find from algorithm.

bool TrainingProgram::trainingProgram_isExercise( const std::string& exercise )
{
    return std::find( program_list.begin(), program_list.end(), exercise )
        != program_list.end();
}
Does the same thing with less hassle.
std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;

#7
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
I just did that: string str = *it3 and then str.compare(exercise) worked well. thanks for help

#8
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
although that's probably ok for a string. For other data types you might want to use a reference or pointer rather then the normal object.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users