This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

string comparison C++


I'm a novice C++ programmer and perhaps I'm making a mistake, but I 
can't seem to find it.

I am trying to compare STL type strings, say

string s1("hello")
string s2("goodbye")

with the compare function, ie

s1.compare(1,2,s2,1,2)

I am using code verbatim from both my text, Deitel & Deitel "How to 
Program C++ 3rd ed", and Josuttis' "The C++ Standard Library", yet g++ 
refuses to compile, saying "no matching function for call to..."

Has the syntax changed since my books were printed?  Should I install 
gcc 3.04?  Maybe I'm missing some obvious error?

I'm using Red Hat Linux 7.2 (an upgrade from 7.1) and gcc (g++) 2.96, 
which was on the RH 7.2 CD.

A small example of this problem is below:

#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

int main()
{
    string s1("string1"), s2("string2");

    cout << "s1.compare(s2): " << s1.compare(s2) << endl;     // ok

    cout << "s1.compare(1,3,s2,1,3): " << s1.compare(1,3,s2,1,3)  << 
endl;    // not ok

    return 0;
}

Thank you for any help or direction you can offer.

Scott Pollock

-- 
A day without math is like a day without sunshine.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]