Home Template class works in one function for all datatypes but in the other it only works with strings
Reply: 0

Template class works in one function for all datatypes but in the other it only works with strings

user44413
1#
user44413 Published in September 19, 2018, 9:59 am

Hello I have a find function that takes any value and searches for that value in a array. This is part of my implementation of a set. The insert function uses the find function and it takes integer arguments and string arguments.

However I implemented a erase function which utilizes the find function as well but the find function in the erase function only works with string arguments and not integer arguments.

Below you should find my find function, insert function, and erase function. The problem is my erase function cannot call my find function with integer arguments.

/* Find function takes a const of any type by reference. */

template <class T>
int Set<T> :: find(const T & value)
{


      cerr << "called find" << endl;
      int start = 0;
      int end = numItems -1;
      int middle = 0;

      int beforeMiddle = 0;


      // middle is the bigger number that needs to move down.

      // loop through until nothing left.
      while (start <= end)
      {

            // middle is in between.
            middle = (start + end) / 2;

            beforeMiddle = middle -1;

            // found it.
            if (data[beforeMiddle] < value && data[middle] >= value)
             return middle;


            // if the value is smaller search first half.
            if (value < data[middle])
                  end = middle - 1;
            else
                  // search last half.
                  start = middle + 1;
      }




}

/* Insert function takes */

template <class T>
void Set <T> :: insert(const T & t) throw (const char *)
{


      int insertHere = 0;
      bool foundIt = false;

      // put it in the beginning.
      if (empty())
      {
            resize();
            data[0] = t;
            numItems++;

            cerr << "the index is now: " << insertHere << endl;
            cerr << "The cap is now: " << cap << endl;
            cerr << "numItems is now: " << numItems << endl;


      }
      // look to see if it is in there
      else
      {
            // why did they find it.
            // determine if its there.
            // if they found it do nothing.


            insertHere = find(t);

            // found it already there
            if (data[insertHere] == t)
            {
                  cerr << "already there";

            }
            // insert at that spot.
            else
            {



               // make some space.
               if (full())
               {
                  resize();
               }

                   cerr << "thought it best to be here: " << insertHere << endl;
               if (insertHere < numItems)
                   shift(insertHere);

                cerr << "the index is now: " << insertHere << endl;
                cerr << "The cap is now: " << cap << endl;
                cerr << "numItems is now: " << numItems << endl;
               // insert stuff.
               data[insertHere] = t;

               numItems++;
                              cerr << "we got this far: " << endl;


            }





      }
}

/* Erase function */

template <class T>
void Set <T> :: erase(const int & t)
{
int number = 0;
string stuff = "hello";

/ * this line gives me the error */

number = find(t);

}

Error

set.h:311:14: error: no matching function for call to ‘Set >::find(const int&)’ number = find(t);

if i replace the t with a string like stuff it will accept it. If i put any number into the find function it will throw this error.

However in my insert function I call the function with string arguments and integer arguments and it works perfectly.

Any tips would be great. I'm kinda confused why it wouldn't accept certain arguments when called from one function compared to another.

This is a assignment for my class.

So I got it to work by adding this code

template <class T>
void Set <T> :: erase( int & t)
{
int number = 0;

T insertMe;

insertMe = t;

number = find(insertMe);


//number = find(t);

}

Hello everyone I just made a new template variable to take the integer and for some reason that worked. I don't know why I couldn't just put in my integer.

share|improve this question

active oldest votes

Your Answer

StackExchange.ifUsing("editor", function () { StackExchange.using("externalEditor", function () { StackExchange.using("snippets", function () { StackExchange.snippets.init(); }); }); }, "code-snippets"); StackExchange.ready(function() { var channelOptions = { tags: "".split(" "), id: "1" }; initTagRenderer("".split(" "), "".split(" "), channelOptions); StackExchange.using("externalEditor", function() { // Have to fire editor after snippets, if snippets enabled if (StackExchange.settings.snippets.snippetsEnabled) { StackExchange.using("snippets", function() { createEditor(); }); } else { createEditor(); } }); function createEditor() { StackExchange.prepareEditor({ heartbeatType: 'answer', convertImagesToLinks: true, noModals: false, showLowRepImageUploadWarning: true, reputationToPostImages: 10, bindNavPrevention: true, postfix: "", onDemand: true, discardSelector: ".discard-answer" ,immediatelyShowMarkdownHelp:true }); } });
 
StackExchange.ready( function () { StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48724860%2ftemplate-class-works-in-one-function-for-all-datatypes-but-in-the-other-it-only%23new-answer', 'question_page'); } );

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged templates or ask your own question.

StackExchange.ready(function(){$.get('/posts/48724860/ivc/c5b0');});
StackExchange.ready(function () { StackExchange.responsiveness.addSwitcher(); }) (function(i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function() { (i[r].q = i[r].q || []).push(arguments) }, i[r].l = 1 * new Date(); a = s.createElement(o), m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); StackExchange.ready(function () { StackExchange.ga.init({ sendTitles: true, tracker: window.ga, trackingCodes: [ 'UA-108242619-1' ] }); StackExchange.ga.setDimension('dimension2', '|templates|'); StackExchange.ga.setDimension('dimension3', 'Questions/Show'); StackExchange.ga.trackPageView(); }); /**/ var _qevents = _qevents || [], _comscore = _comscore || []; (function() { var ssl = 'https:' == document.location.protocol, s = document.getElementsByTagName('script')[0], qc = document.createElement('script'); qc.async = true; qc.src = (ssl ? 'https://secure' : 'http://edge') + '.quantserve.com/quant.js'; s.parentNode.insertBefore(qc, s); _qevents.push({ qacct: "p-c1rF4kxgLUzNc" }); /**/ var sc = document.createElement('script'); sc.async = true; sc.src = (ssl ? 'https://sb' : 'http://b') + '.scorecardresearch.com/beacon.js'; s.parentNode.insertBefore(sc, s); _comscore.push({ c1: "2", c2: "17440561" }); })();
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298612 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO