By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Weird Errors

P: 12
Hi, I'm using visual studio 2008 and normally when I get an error it shows what line it is on and which file etc. The error I'm getting I don't know how to solve or even what the problem is. This is the whole output when I try to run the program.
"1>------ Build started: Project: corysid, Configuration: Debug Win32 ------
1>Compiling...
1>implementation.cpp
1>Linking...
1>actualmain.obj : error LNK2019: unresolved external symbol "public: __thiscall SortedList<float>::SortedList<float>(void)" (??0?$SortedList@M@@QAE@XZ) referenced in function _main
1>actualmain.obj : error LNK2019: unresolved external symbol "public: __thiscall SortedList<char>::SortedList<char>(void)" (??0?$SortedList@D@@QAE@XZ) referenced in function _main
1>C:\projectscpp\corysid\Debug\corysid.exe : fatal error LNK1120: 2 unresolved externals
1>Build log was saved at "file://c:\projectscpp\corysid\corysid\Debug\BuildLog.htm"
1>corysid - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========="

I'm using a class template, and it seems it cannot access parts of the class when I make an instance of the class?

Here's the code if that helps
.h file:
Expand|Select|Wrap|Line Numbers
  1. #define MAX_ITEMS  20
  2.  
  3. template<class ItemType>
  4. class SortedList
  5.        private:
  6.         int length;
  7.         ItemType values[MAX_ITEMS];
  8.         int currentPos;
  9.        public:
  10.          SortedList( );  // default constructor: lenght=0, currentPos=-1
  11.          void MakeEmpty();    // let length=0
  12.          void InsertItem(ItemType x);   // insert x into the list     
  13.          void DeleteItem(ItemType x);  // delete x from the list
  14.          bool IsFull( );        // test if the list is full
  15.          int LengthIs( );   // return length
  16.          void RetrieveItem(ItemType &x, bool &found);            // retrieve x from the list, the 
  17.                                                                 // boolean result is stored in found
  18.          void ResetList( );  // currentPos=-1
  19.          void GetNextItem(ItemType &x);                        // get the next element from the list with 
  20.                                                             // respect to the currentPos
  21.          int numCharElement();
  22.          int numFloatElement();
  23. };
Implementation:
Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <iostream>
  3. #include "part2.h"
  4. using namespace std;
  5.  
  6.  
  7. template<class ItemType>
  8. SortedList<ItemType>::SortedList()
  9. {
  10.     length = 0;
  11.     currentPos= -1;
  12. }
  13.  
  14. template<class ItemType>
  15. void SortedList<ItemType>::MakeEmpty()
  16. {
  17.     length=0;
  18. }
  19.  
  20. template<class ItemType>
  21. void SortedList<ItemType>::InsertItem(ItemType x)
  22. {
  23.  
  24.     int midpoint;
  25.     int first=0;
  26.     int last= length-1;
  27.     midpoint = (first+last)/2;
  28.  
  29.     bool moreToSearch = (first <= last);
  30.     found = false;
  31.     while(moreToSearch)
  32.     {
  33.         switch(x.comparedTo(values[location]))
  34.         {
  35.         case LESS:    //search in 1st half
  36.             moreToSearch = (first <= last);
  37.             last= midpoint-1;
  38.             break;
  39.         case GREATER:
  40.                 location ++;
  41.                 moreToSearch=(location<length);
  42.                 break;
  43.         }
  44.     }
  45. for (int index=length; length>location; index--)
  46. {    
  47.     values[index]= values[index-1];
  48. }
  49.     values[location] =x;
  50.     length++;
  51.  
  52. }
  53.  
  54.  
  55. template<class ItemType>
  56. void SortedList<ItemType>::DeleteItem(ItemType x)
  57. {
  58.     int midpoint;
  59.     int first=0;
  60.     int last= length-1;
  61.     midpoint= (first+last)/2
  62.  
  63.     bool moreToSearch = (first <= last);
  64.     found = false;
  65.     while(moreToSearch)
  66.     {
  67.         switch(x.comparedTo(values[location]))
  68.         {
  69.         case LESS:    //search in 1st half
  70.             moreToSearch = (first <= last);
  71.             last= midpoint-1;
  72.             break;
  73.         case GREATER:
  74.                 location ++;
  75.                 moreToSearch=(location<length);
  76.                 break;
  77.         }
  78.     }
  79. for (int index=length; length>location; index--)
  80. {    
  81.     values[index]= values[index-1];
  82. }
  83.     values[location] =x;
  84.     length++;
  85.  
  86. }
  87.  
  88. template<class ItemType>
  89. void SortedList<ItemType>::RetrieveItem(ItemType &x, bool &found)
  90. {
  91.     int midpoint;
  92.     int first=0, last=length-1;
  93.     bool moreToSearch= (first <= last);
  94.     found= false;
  95.     while(moreToSearch && !found)
  96.     {
  97.         midpoint=(first+last)/2
  98.         switch(x.comparedTo(values[index]))
  99.         {
  100.         case LESS:        //search in 1st half
  101.             moreToSearch= (first <= last);
  102.             last= midpoint-1;
  103.             break;
  104.         case GREATER;    //Search in 2nd half
  105.             first= midpoint+1;
  106.             moreToSearch = (first <=last);
  107.             break;
  108.         case EQUAL;        //x has been found
  109.             found= true;
  110.             break;
  111.         }
  112.     }
  113. }
  114.  
  115. template<class ItemType>
  116. int SortedList<ItemType>::LengthIs()
  117. {
  118.     return length;
  119. }
  120.  
  121. template<class ItemType>
  122. void SortedList<ItemType>::ResetList()
  123. {
  124.     currentPos= -1;
  125. }
  126.  
  127. template<class ItemType>
  128. bool SortedList<ItemType>::IsFull()
  129. {
  130.     if (length < 19)
  131.         return false;
  132.     else
  133.         return true;
  134. }
  135.  
  136. template<class ItemType>
  137. void SortedList<ItemType>::GetNextItem(ItemType &x)
  138. {
  139.     currentPos++; 
  140.     item = values [currentPos]; 
  141. }
  142.  
  143. template<class ItemType>
  144. int SortedList<ItemType>::numCharElement()
  145. {
  146.     indata.open("char.dat");
  147.  
  148.     int i=0; 
  149.     int size=0;
  150.  
  151.     char values[20];
  152.     while (indata.eof())        // write or read data from indatac into values
  153.     {    
  154.     indata >> values[i];            
  155.     i++;
  156.     size++;                    // this will count how many values there are in the array
  157.     }    
  158.  
  159.     for(i=0; i<size; i++)//just a test to see if it put the values in the array
  160.     {
  161.     cout<<values[i]<<endl;
  162.     }
  163.  
  164.  
  165.     for (int i=0; i< size; i++)    
  166.     {            
  167.  
  168.     a.InsertItem (values[i]);        //and insterts each element into the list
  169.     }
  170.  
  171.  
  172.     cout << "There are "<< size << " items in the char array" << endl;        
  173.  
  174.  
  175.     indata.close();
  176.     return size;
  177.  
  178.  
  179. }
  180.  
  181.  
  182.  
  183. template<class ItemType>
  184. int SortedList<ItemType>::numFloatElement()
  185. {
  186.     int i=0; 
  187.     int size=0;
  188.     indata.open("float.dat");
  189.  
  190.     float values[20];
  191.     while (!indata.eof())        // write or read data from indatac into values
  192.     {    
  193.     indata >> values[i];            
  194.     i++;
  195.     size++;                    // this will count how many values there are in the array
  196.     }    
  197.  
  198.     for(i=0; i<size; i++)//just a test to see if it put the values in the array
  199.     {
  200.     cout<<values[i]<<endl;
  201.     }
  202.  
  203.  
  204.     for (int i=0; i< size; i++)    
  205.     {                                    
  206.     b.InsertItem (values[i]);        //and insterts each element into the list
  207.     }
  208.  
  209.  
  210.     cout << "There are "<< size << " items in the char array" << endl;        
  211.  
  212.  
  213.     indata.close();
  214.     return size;
  215.  
  216.  
  217. }
Main function:
Expand|Select|Wrap|Line Numbers
  1. #include "part2.h"
  2. #include <fstream>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9. ifstream indata;
  10.  
  11. SortedList<char> a;
  12. //a.numCharElement();
  13.  
  14. SortedList<float> b;
  15. //b.numFloatElement();
  16. return 0;
  17. }
The code is supposed to call those 2 template functions and populate an array in sorted order. But I need to get past those weird errors to even try and debug the logic errors etc.

Thanks a lot.
Mar 14 '09 #1
Share this Question
Share on Google+
2 Replies


Savage
Expert 100+
P: 1,764
When you are coding templates both definition and implementation must be in the same file.There are ways around it, but they are not worth it.
Mar 15 '09 #2

P: 12
Thank you sir. I should be able to get it working now
Mar 15 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.