www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/10/30/19:45:36

From: Stan Moore <smoore AT exis DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: compare()
Date: Mon, 30 Oct 2000 19:36:12 -0500
Message-ID: <mm4svsssf960uaqnaunc11eujqmmnk9a3e@4ax.com>
References: <1F7509A3BB20243A DOT 7FD3AC2DC483DD04 DOT 6BB6DE9721314E90 AT lp DOT airnews DOT net>
X-Newsreader: Forte Agent 1.8/32.548
MIME-Version: 1.0
NNTP-Posting-Host: digital-2-105.exis.net
X-Trace: 30 Oct 2000 19:34:06 -0500, digital-2-105.exis.net
Lines: 81
Organization: A Customer of Exis Net Inc
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Mon, 30 Oct 2000 01:02:05 -0500, Rodeo Red <rodeored AT netstep DOT net>
wrote:

>
>On my djgpp compiler I get the error messages below, and  I thought it
>might be a compiler specific glitch.  Does anyone know why this does not
>compile ?  It compares the last three letters of the word with a string. 
>
>

here is the original

>int main()
>{
>  std::string word = "babies";
>
>  std::string::size_type pos3 = word.size()-3;
>  std::string ies("ies");
>
>  std::cout << word << std::endl;
>  std::cout << ies << std::endl;
>
>  if (!word.compare(pos3, 3, ies))//line 16:no matching function
> {
>    std::cout<< "yes the word ends in 'ies'";
>  }  
>  
>
>  std::cout << word << std::endl;
>
>  return 0;
>}

here is the corrected copy 

#include <iostream>
#include <string>

int main()
{
  std::string word = "babies";

  std::string ies("ies");
  std::string::size_type pos3 = word.length() - ies.length() - 1;

  std::cout << "the length of word is " << pos3 << std::endl;
  std::cout << "word is " << word << std::endl;
  std::cout << "string to test is " << ies << std::endl;


  // this is my corrected attempt
  if (( pos3 > 0 ) && (word.compare(ies, pos3))) 
    {
      std::cout << "yes the word ends in 'ies'" << std::endl;
    }else{
      std::cout << "no the word doesn't end in 'ies'" << std::endl;
    }

  //this is the original code
  //if (!word.compare(pos3, 3, ies))//line 16:no matching function
  //{
  //  std::cout<< "yes the word ends in 'ies'";
  //}  
  

  std::cout << word << std::endl;

  return 0;
}


<error log snipped>

Your main problem was the incorrect call to compare. I did a quick
check of the info file and got the above version working. The other
changes I made like using length() were to ease the tests I ran.

Good luck! :)

Stan

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019