|
-
July 12th, 2004, 11:00 AM
#1
setget function in a template class
Code:
#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.
-
July 12th, 2004, 11:02 AM
#2
I've tried something like this ...
Code:
template <class T, int element, int integer>
... and making all of the appropriate changes along the way, but that still didn't work .
-
July 12th, 2004, 11:06 AM
#3
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):
Code:
template <class T, int element>
int & Array<T, element>::setgetInteger()
{
return integer;
}
Just as a side note.
- Matthias
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." - Bjarne Stroustrup
-
July 12th, 2004, 11:08 AM
#4
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.
- Matthias
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." - Bjarne Stroustrup
-
July 12th, 2004, 11:09 AM
#5
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):
Code:
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: .
-
July 12th, 2004, 11:10 AM
#6
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 .
-
July 12th, 2004, 11:11 AM
#7
That has happened to me too many times as well... don't be ashamed
- Matthias
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." - Bjarne Stroustrup
-
July 12th, 2004, 02:22 PM
#8
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|