Welcome! Log In Create A New Profile

Advanced

need little advice on class templates

Posted by dve83 
Announcements Last Post
Announcement myUnisa availability 21 to 24 March 2019 03/17/2019 02:24PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
need little advice on class templates
July 16, 2006 05:48PM
Ive got this

something.h //HEADER
using namespace std;
template<class T>
class Set
{
public:
void Add(T avalue);
private:
vector<T> thev;
};
--------------------------------------
something.cpp //CODE IMPLEMENTATION
#include <iostream>
#include <string>
#include <vector>
#include "something.h"
using namespace std;

template<class T>
void Set<T>::Add(T avalue)
{
thev.push_back(avalue);
}
---------------------------------------------
main.cpp //MAIN PROGRAM
#include <iostream>
#include "set.h"
using namespace std;

int main() {
Set<int> aset;
aset1.Add(1);
return 0;
}
---------------------------------------------
so why do I keep getting the following compilation error : ????

[Linker error] undefined reference to `Set<int>::Add(int)'

I am obviously missing something here. Any advise would be greatly appreciated. Thanks in advance.

Re: need little advice on class templates
July 17, 2006 01:01AM
hi ya

have you tried sticking everything in one file? read second paragraph of page 22 of tut 501. the compiler of devC++ isnt the greatest. so for example try this...

#include <iostream>
#include <string>
#include <vector>
using namespace std;

using namespace std;
template<class T>
class Set
{
public:
void Add(T avalue){thev.push_back(avalue);}; // in chapter 14 they did this...
private:
vector<T> thev;
};

template<class T>
Set<T>:: Set(){}
//it looks like you have to declare your default
// constructor like this.
//didnt work when i did it in the class declaration..



this is the structure of my program and its still working fine, only my .remove is giving problems. it wont remove. this is what i did:

void Remove(T TElement)
{
v.erase(TElement, TElement); // v being my vector...
};

any suggestions?
Re: need little advice on class templates
July 17, 2006 07:45AM
I mihgt be wrong, but the erase function of the Vector class onyl takes 1 parameter, wheras you are providing two values in the parameter list. Also, the erase functins's parameter specifies the position to be removed, not the actual value. In order to remove the element by refeering to it, I'd think you have to loop through the vector until you find it and then use erase(currentposition) to remove it.
might be wrong in my interpretation, but this is what i think.

all try to put everything in one unit. any other ideas welcome.
smile
Re: need little advice on class templates
July 17, 2006 01:09PM
My Remove is different i cant say if it is right but it works fine

Try this:

template<class T>
bool Set<T>::EraseValue(const T & Val) {

remove(S1.begin(),S1.end(),Val);
S1.pop_back();
}

Re: need little advice on class templates
July 17, 2006 01:10PM
My Remove is different i cant say if it is right but it works fine

Try this:

template<class T>
bool Set<T>::EraseValue(const T & Val) {

remove(S1.begin(),S1.end(),Val);
S1.pop_back();
}

If you have any ideas in how the intersection and differences should be implemented please let me know. Hurry time is ticking

Thanks



Re: need little advice on class templates
July 17, 2006 01:46PM
this is what i did for the intercection:

Set<T> Intersection(Set<T> TSet)
{
Set NewSet;

for (int i = 0; i < v.size(); i++)
{
for (int j = 0; j < TSet.Size(); j++)
if (v == TSet[j])
NewSet.Add(v);
}
return NewSet;

};

i implemented in the same file as the declarations so Set<T> Intersection(Set<T> TSet) is my declaration follow by the implementation
Re: need little advice on class templates
July 17, 2006 01:49PM
i dont quite understand your remove method. can you elaborate it a bit for me please?
Re: need little advice on class templates
July 17, 2006 02:28PM
Try this for erase:

void Remove(int val)
{
v.erase(v.begin() + val, v.end() - val);

};
Re: need little advice on class templates
July 18, 2006 10:11AM
okaayyy. i put it all in 1 unit and guess what?! it works!! thanks for that!
now i need to find out why it doesn't work the other way around... mmmm.
undefined reference and LINKER error. somewhere Im not including something i should be including. and whats up with this SHOE question? you might think its easy until you find you dont know that many shoes smile
Re: need little advice on class templates
July 18, 2006 11:52PM
thats why i got my girlfriend to help with the shoes, heheheh.
avatar Re: need little advice on class templates
July 21, 2006 07:42PM
dve83

The reason you're getting compiler errors when using a template is that you've put the implementation in a separate source file and only left the class declaration in the header file.

Templates are just that, they are not real classes/functions but rather they are rubber stamps that the compiler uses to make the real classes/functions at compile-time. For this to work properly, the entire template must be visible to the compiler for each file that uses that template. For this reason, the entire definition(interface AND implementation) should be in the header file.

The whole point of templates is to make coding similar classes/functions quicker since there is usually the algorithm is the same but only the data types change. Without templates you would have to create multiple versions of common tasks.

Oh, another thing that I've mentioned before. Don't put "using namespace" in a header file

This simplest way to erase would be to utilise the iterators themselves
template<class Type>
void Set<Type>:: Remove (Type const &val)
  {
  std:: vector<Type>::iterator vecIter = m_dataList.begin(); // m_dataList being the vector
  for (vecIter = m_dataList.begin(); vecIter != m_dataList.end(); ++vecIter)
    {
    if ((*vecIter) == val)
      {
      m_dataList.erase(vecIter);
      break;  // found the element, break out of loop and finish      
      }
    }
  }
Sorry, only registered users may post in this forum.

Click here to login