Click to See Complete Forum and Search --> : setget function in a template class


YourSurrogateGod
July 12th, 2004, 11:00 AM
#include <iostream>
#include <iomanip>
using namespace std;

template <class T, int element>
class Array
{
public:
void putElement(T x, int index);
T getElement(int index);
int &setgetInteger();

private:
T item[element];
int integer;
};

template <class T, int element>
void Array<T, element>::putElement(T x, int index)
{
item[index] = x;
}

template <class T, int element>
T Array<T, element>::getElement(int index)
{
return item[index];
}

template <class T, int element>
int Array<T, element>::setgetInteger()
{
return integer;
}

int main()
{
char *name[] = {"Meier", "Schulze", "Lehmann", "Aust"};
double contents[]= {2200.5, 1300.5, 1700.30, 4888.80};

Array<char *, 4> nameArray;
Array<double, 4> contentsArray;

nameArray.setgetInteger() = 56;

for(int i = 0; i < 4; i++)
{
nameArray.putElement(name[i], i);
contentsArray.putElement(contents[i], i);
}

for(i = 0; i < 4; i++)
{
cout << setiosflags(ios::left) << setw(10) << nameArray.getElement(i)
<< resetiosflags(ios::left) << setw(8) << contentsArray.getElement(i) << endl;
}

cout << "Integer output: " << nameArray.setgetInteger() << endl;

return 0;
}This doesn't compile because of the setget function that I put in. My question is why can't I make it work with a specific value being returned? Is this a limitation of template classes or something? Sorry for the silly question, I'm just not familiar with this.

YourSurrogateGod
July 12th, 2004, 11:02 AM
I've tried something like this ...template <class T, int element, int integer>... and making all of the appropriate changes along the way, but that still didn't work :( .

matthias_k
July 12th, 2004, 11:06 AM
Maybe you tell us what the compiler error is first :)
By the way:
nameArray.setgetInteger() = 56;
This doesn't do anything but initializing an anonymous temporary variable with 56.

I think what you meant is that (note the reference type):

template <class T, int element>
int & Array<T, element>::setgetInteger()
{
return integer;
}

Just as a side note.

matthias_k
July 12th, 2004, 11:08 AM
I just recognized your declaration and definition of setgetInteger() don't fit. In the definition you return an int, in the declaration an int ref.

YourSurrogateGod
July 12th, 2004, 11:09 AM
Originally posted by matthias_k
Maybe you tell us what the compiler error is first :)
By the way:

This doesn't do anything but initializing an anonymous temporary variable with 56.

I think what you meant is that (note the reference type):

template <class T, int element>
int & Array<T, element>::setgetInteger()
{
return integer;
}

Just as a side note. You son of a biscuit ;) . That did the trick :sigh: .

YourSurrogateGod
July 12th, 2004, 11:10 AM
Originally posted by matthias_k
I just recognized your declaration and definition of setgetInteger() don't fit. In the definition you return an int, in the declaration an int ref. Yeh, fixed it. I need to get home asap, this is killing me to be so long abroad :( .

matthias_k
July 12th, 2004, 11:11 AM
That has happened to me too many times as well... don't be ashamed :)

YourSurrogateGod
July 12th, 2004, 02:22 PM
Originally posted by matthias_k
That has happened to me too many times as well... don't be ashamed :) Ja, aber hast du irgendwann diese Fehler in CodeGuru geposted?