C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 18th, 2009, 09:36 PM
Taka Taka is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 33 Taka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 20 sec
Reputation Power: 10
"instantiated from here" error

Hey,

Can anyone help me with the below please?

sort.cpp: In function 'void sortContainer(iterator, iterator, int) [with iterator = __gnu_cxx::__normal_iterator<dataObject*, std::vector<dataObject, std::allocator<dataObject> > >]':
sort.cpp:137: instantiated from here

Code:

/**************************************************  ******\
   Test program for demonstrating sorting algorithms
\*************************************************  *******/
#include <stdlib.h>

#include <iostream>
#include <algorithm>
#include <vector>
#include <stdlib.h>

#include "dataobject.h"

using namespace std;

int randNum();

template <class iterator> 
   void sortContainer(iterator start, iterator finish, int gap);
   

int randNum()
{
   // rand() produces pseudo-random number in range 0 to RAND_MAX
   // for most compilers RAND_MAX is 2^15-1
   // randNum() produces pseudo-random number in range 0 to 2^31-1
  
   return ((rand() << 16) + rand());
} 

   /**************************************************  ***********************\
      sortPercentage
      Checks every consecutive pair of items in the container, 
      eg, 1st and 2nd, 2nd and 3rd, 3rd and 4th ... to ... 2nd last and last
      and finds the percentage that are in order vs the total number of pairs.
      This percentage value is returned.
   \*************************************************  ************************/
 






 
template <class iterator> 
   void sortContainer(iterator start, iterator finish, int gap)
{
   /**************************************************  **************\
       Sort the container using shell sort
       gap will be called with the original size of the container
       The underlying sort for the container is bubble sort which
       will be run twice for each gap decrement
   \*************************************************  ***************/

   if (gap == 1) return;
   
   iterator left, right;
   int i, numswap, totswap=0, numpass, sortPercent;
   
   gap = gap * 3 / 4;
   do {
      numswap = 0;
      
      // Reduce gap. Gap will start out at half the size of the container
      // Eventually will stabilize at 1 in size
      gap = (gap * 2 + 1) / 3; 
      
      // make two passes through container with bubble sort
      for (numpass=0; numpass<2; numpass++) {
         right = left = start;
         // Make the distance between left and right gap in size
         for (i=0; i<gap; i++) right++;
         
         // Make a pass through container using Bubble sort
         while (right != finish) {
            if (*left < *right) {
               swap(left, right);
               numswap++;
            }
            left++;
            right++;
         }
      
         // update some statistics and print them
         totswap += numswap;
         cout << "gap = " << gap << " num swaps = " << numswap;
         cout << " sort percentage = " << sortPercent(start, finish) << "\n";
         
         if (numswap == 0) break;
      }
   } while (numswap > 0 || gap > 1);
   
   cout << "Total swaps = " << totswap << "\n";
}

int main(int argc, char *argv[])
{
   const int MAXDATA = 10000;
   vector<dataObject> array;
   dataObject *doPtr;
   int i, rand;
   
   try {
      /**************************************************  ************\
         Initialise things to demonstrate the sort
      \*************************************************  *************/
      
      // fill vector with unique values
      for (i=0; i<MAXDATA; i++) {
         doPtr = new dataObject(i);
         array.push_back(*doPtr);
         delete doPtr;
      }
      
      // set up random number generator
      if (argc != 2) {
         cout << "Syntax : program seed\n";
         cout << "seed is any integer value\n";
         return 0;
      }
      int seed = atoi(argv[1]);
      srand(seed);
      
      // scramble vector
      for (i=0; i<MAXDATA; i++) {
         rand = randNum() % MAXDATA;
         swap(array[i], array[rand]);
      }
      cout << MAXDATA << " Items in vector\n";
      
      /**************************************************  ***********\
         sort the vector using iterators 
      \*************************************************  ************/
      
      cout << "Sorting vector using shell sort\n";
      sortContainer(array.begin(), array.end(), array.size());
      
   } catch(...) {
      cout << "\nERROR - undefined Exception thrown\n";
      exit(1);
   }
   
   return 0;
}

Reply With Quote
  #2  
Old May 19th, 2009, 01:34 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
I'm not sure you have posted all of the error messages. You should also post the code for the dataObject class.

Did you implement operator < for the dataObject class?
__________________
There is no such thing as C/C++, you either program C or C++

Reply With Quote
  #3  
Old May 19th, 2009, 01:54 AM
Taka Taka is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 33 Taka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 20 sec
Reputation Power: 10
sorry here is dataobject.h

Code:
#ifndef DATAOBJECT_H_
#define DATAOBJECT_H_

#include <algorithm>

/**************************************************  *********\
   class for holding data to be stored in container objects
\*************************************************  **********/

struct dataObject
{
   int keyval;
   
   /**************************************************  ******\
      place any other data declartions the data object 
      may need here
   \*************************************************  *******/
   
   /**************************************************  ******\
      constructors
   \*************************************************  *******/
   
   // constructor
   dataObject() : keyval(-1) {}
   dataObject(int val) : keyval(val) {}
   
   // copy constructor
   dataObject(const dataObject &other) : keyval(other.keyval) {
      // copy any data in other to this
   }
   
   /**************************************************  ******\
      misc functions
   \*************************************************  *******/
   
   void swapObjects(dataObject &other) {
      std::swap(keyval, other.keyval);
      // swap any other data in dataObject
   }
   
   bool isKeyval(int val) const {
      return (val == keyval);
   }
   
   int getKeyval() const {
      return keyval;
   }
   
   int getHashval(int range) const {
      return (keyval % range);
   }

   bool empty() const {
      return (keyval == -1);
   }
   
   /**************************************************  ******\
      overloaded operators
   \*************************************************  *******/
   
   // overloaded assignment operator
   dataObject& operator = (const dataObject &other) {
      dataObject temp(other);
      swapObjects(temp);
      return *this;
   }
   
   // overloaded relational operators
   bool operator == (const dataObject &other) const {
      return (keyval == other.keyval);
   }
   
   bool operator != (const dataObject &other) const {
      return (keyval != other.keyval);
   }
   
   bool operator > (const dataObject &other) const {
      return (keyval > other.keyval);
   }
   
   bool operator < (const dataObject &other) const {
      return (keyval < other.keyval);
   }
   
   bool operator >= (const dataObject &other) const {
      return (keyval >= other.keyval);
   }
   
   bool operator <= (const dataObject &other) const {
      return (keyval <= other.keyval);
   }
};

#endif

Reply With Quote
  #4  
Old May 19th, 2009, 02:28 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
With dataObject included I only get a compiler error in the line where you call sortPercent, because that function is not in your code. If I comment that line then I can compile and link the code.

Could you post all the messages the compiler is emitting? Because I think there is a message in front of the one you already posted which is actually detailing the error.

Reply With Quote
  #5  
Old May 19th, 2009, 05:20 AM
Taka Taka is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 33 Taka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 20 sec
Reputation Power: 10
g++ sort.cpp
sort.cpp: In function 'void sortContainer(iterator, iterator, int) [with iterator = __gnu_cxx::__normal_iterator<dataObject*, std::vector<dataObject, std::allocator<dataObject> > >]':
sort.cpp:137: instantiated from here
sort.cpp:88: error: 'sortPercent' cannot be used as a function

These are all the error messages I am getting.

Reply With Quote
  #6  
Old May 19th, 2009, 05:26 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
That is what I got as well. The last message is the important one. You are calling a function called sortPercent (or is it a class?) but you do not have that function defined anywhere. At least not in the code you posted.

The verbose error message merely states that the instantiation of the sortContainer failed because of that sortPercent thing.

Reply With Quote
  #7  
Old May 19th, 2009, 05:28 AM
Taka Taka is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 33 Taka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 20 sec
Reputation Power: 10
Ahhh ok so once I write sortPercent I should be ok? Thanks.

Reply With Quote
  #8  
Old May 19th, 2009, 05:39 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
Yeah I think so. Let us know if it all works out ok?

Reply With Quote
  #9  
Old May 22nd, 2009, 02:31 AM
Taka Taka is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 33 Taka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 20 sec
Reputation Power: 10
So I'm trying to work out how to run the sortPercentage code. Can you help me with it possibly. This is what I've tried so far. It compiles but when I run it it has a segmentation fault.

Code:
/**************************************************  ******\
   Test program for demonstrating sorting algorithms
\*************************************************  *******/
#include <stdlib.h>

#include <iostream>
#include <algorithm>
#include <vector>
#include <stdlib.h>

#include "dataobject.h"

using namespace std;

int randNum();

template <class iterator> 
   void sortContainer(iterator start, iterator finish, int gap);
   

int randNum()
{
   // rand() produces pseudo-random number in range 0 to RAND_MAX
   // for most compilers RAND_MAX is 2^15-1
   // randNum() produces pseudo-random number in range 0 to 2^31-1
  
   return ((rand() << 16) + rand());
} 

template <class iterator>
   int sortPercentage(iterator start, iterator finish)
{
   /**************************************************  ***********************\
      sortPercentage
      Checks every consecutive pair of items in the container, 
      eg, 1st and 2nd, 2nd and 3rd, 3rd and 4th ... to ... 2nd last and last
      and finds the percentage that are in order vs the total number of pairs.
      This percentage value is returned.
   \*************************************************  ************************/
 
   iterator left, right;
   int InnerCount;
   int comparisons;
   int swaps = 0;

   for (comparisons = 0; comparisons < finish; comparisons++)
   {
      for (InnerCount = 0; InnerCount < finish; InnerCount++)
      {
         right = left = start;

         while (right != finish)
         {
            if (*left < *right)
            {
               swaps+=1;
            }
         }
      }
      comparisons+=1;
   }
}

template <class iterator> 
   void sortContainer(iterator start, iterator finish, int gap)
{
   /**************************************************  **************\
       Sort the container using shell sort
       gap will be called with the original size of the container
       The underlying sort for the container is bubble sort which
       will be run twice for each gap decrement
   \*************************************************  ***************/

   if (gap == 1) return;
   
   iterator left, right;
   int i, numswap, totswap=0, numpass, sortPercent;
   
   gap = gap * 3 / 4;
   do {
      numswap = 0;
      
      // Reduce gap. Gap will start out at half the size of the container
      // Eventually will stabilize at 1 in size
      gap = (gap * 2 + 1) / 3; 
      
      // make two passes through container with bubble sort
      for (numpass=0; numpass<2; numpass++) {
         right = left = start;
         // Make the distance between left and right gap in size
         for (i=0; i<gap; i++) right++;
         
         // Make a pass through container using Bubble sort
         while (right != finish) {
            if (*left < *right) {
               swap(left, right);
               numswap++;
            }
            left++;
            right++;
         }
      
         // update some statistics and print them
         totswap += numswap;
         cout << "gap = " << gap << " num swaps = " << numswap;
//         cout << " sort percentage = " << sortPercent(start, finish) << 
"\n";
         
         if (numswap == 0) break;
      }
   } while (numswap > 0 || gap > 1);
   
   cout << "Total swaps = " << totswap << "\n";
}

int main(int argc, char *argv[])
{
   const int MAXDATA = 10000;
   vector<dataObject> array;
   dataObject *doPtr;
   int i, rand;
   
   try {
      /**************************************************  ************\
         Initialise things to demonstrate the sort
      \*************************************************  *************/
      
      // fill vector with unique values
      for (i=0; i<MAXDATA; i++) {
         doPtr = new dataObject(i);
         array.push_back(*doPtr);
         delete doPtr;
      }
      
      // set up random number generator
      if (argc != 2) {
         cout << "Syntax : program seed\n";
         cout << "seed is any integer value\n";
         return 0;
      }
      int seed = atoi(argv[1]);
      srand(seed);
      
      // scramble vector
      for (i=0; i<MAXDATA; i++) {
         rand = randNum() % MAXDATA;
         swap(array[i], array[rand]);
      }
      cout << MAXDATA << " Items in vector\n";
      
      /**************************************************  ***********\
         sort the vector using iterators 
      \*************************************************  ************/
      
      cout << "Sorting vector using shell sort\n";
      sortContainer(array.begin(), array.end(), array.size());
      
   } catch(...) {
      cout << "\nERROR - undefined Exception thrown\n";
      exit(1);
   }
   
   return 0;
}

Last edited by Taka : May 22nd, 2009 at 09:32 PM.

Reply With Quote
  #10  
Old May 30th, 2011, 01:59 AM
TheOneAlone TheOneAlone is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2011
Posts: 3 TheOneAlone User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 39 sec
Reputation Power: 0
Regarding Sort Percent

Hey did you figure this problem out ? I have the same problem plus im getting these two errors also.

error: no match for 'operator<' in 'comparisons < finish'
error: no match for 'operator<' in 'InnerCount < finish'

I included dataobject.h also so im confused how this is happening.

Any help would be great thanx.

Reply With Quote
  #11  
Old June 3rd, 2011, 12:10 PM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Code:
template <class iterator>
   int sortPercentage(iterator start, iterator finish)
{
   /**************************************************    ***********************\
      sortPercentage
      Checks every consecutive pair of items in the container, 
      eg, 1st and 2nd, 2nd and 3rd, 3rd and 4th ... to ... 2nd last and last
      and finds the percentage that are in order vs the total number of pairs.
      This percentage value is returned.
   \*************************************************    ************************/
 
   iterator left, right;
   int InnerCount;
   int comparisons;
   int swaps = 0;

   for (comparisons = 0; comparisons < finish; comparisons++)
   {
      for (InnerCount = 0; InnerCount < finish; InnerCount++)


Quote:
error: no match for 'operator<' in 'comparisons < finish'
error: no match for 'operator<' in 'InnerCount < finish'
Quote:
compiles but [...] has a segmentation fault.


Well, you're doing a comparison between an integer and an iterator. They're apples and oranges. If it compiles, you've somehow told it how to, but failed to do it right. (And it may have been impossible to begin with.)

Secondly, the sortPercentage you've written doesn't match the spec. (It's biased towards the beginning being critical, and the end unimportant.)

Make sure you understand what the below code does.
Code:
   int ishigher = 0;
   int count = 0;
   for (;start+1<finish;start++) {
      if (*start < *(start+1))
         ishigher++;
      count++;
   }
   return ishigher*100/count;
__________________
Quote:
Programming by Coincidence
Fred types in some more code, tries it, and it still seems to work. [Then] the program suddenly stops working. [...] Fred doesnít know why the code is failing because he didnít know why it worked in the first place.
Undefined behavior results in: (worst to best)
-Erases your harddisk. Really.
-Appears to work - for now
-Delayed errors/crashes
-Crashes
-Compiler warning

Reply With Quote
  #12  
Old June 3rd, 2011, 10:21 PM
TheOneAlone TheOneAlone is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2011
Posts: 3 TheOneAlone User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 39 sec
Reputation Power: 0
thanx alot

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > "instantiated from here" error


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap