473,503 Members | 2,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New to c++, need help with a template class

i cant seem to figure out what is wrong with this class. it has to do
with the input/output stream override. please can somebody help me.

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
template <typename Item>
class Sample {
private:
vector<Item> results;

public:
Sample(vector<Item> samps);

const vector<Item> get_data();

void set_data(const vector<Item> &v);

long double minimum();

long double maximum();

long double range();

long double midrange();

long double mean();

long double variance();

long double std_deviation();

//get the index of the current minimum double in the vector
int minimum2(vector<Item> v);

//get the number which i currently the smallest in the
//given vector
long double minimum3(vector<Item> v);

long double median();

friend ostream& operator<<(ostream& os, const Sample& samp);

friend istream& operator>>(istream& is, Sample& samp);

};

template <typename item>
Sample<item>::Sample(vector<item> samps) : results(samps) {}

template <typename item>
const vector<item> Sample<item>::get_data(){
return results;
}

template <typename item>
void Sample<item>::set_data(const vector<item> &v) {
results = v;
}

template <typename item>
long double Sample<item>::minimum(){
long double m;
for(int i = 0; i < results.size(); i++){
if(i == 0){
m = results[i];
}else if(results[i] < m){
m = results[i];
}
}

return m;
}

template <typename item>
long double Sample<item>::maximum(){
long double m;
for(int i = 0; i < results.size(); i++){
if(i == 0){
m = results[i];
}else if(results[i] > m){
m = results[i];
}
}

return m;
}

template <typename item>
long double Sample<item>::range(){
long double max = maximum();
long double min = minimum();
long double range = max - min;
return range;
}

template <typename item>
long double Sample<item>::midrange(){
long double max = maximum();
long double min = minimum();
long double mid_range = (max + min)/2.0;
return mid_range;
}

template <typename item>
long double Sample<item>::mean(){
long double sum = 0;
long double m;
for(int i = 0; i < results.size(); i++){
sum += results[i];
}
m = sum/(results.size() * 1.0);

return m;
}

template <typename item>
long double Sample<item>::variance(){
long double sum = 0;
long double m = mean();

for(int i = 0; i < results.size(); i++){
sum = sum + (pow(results[i]-m,2));
}
return sum/(results.size() * 1.0);
}

template <typename item>
long double Sample<item>::std_deviation(){
sqrt (variance());
}

//get the index of the current minimum double in the vector
template <typename item>
int Sample<item>::minimum2(vector<item> v){
double m;
int k = 0;
for(int i = 0; i < v.size(); i++){
if(i == 0){
m = v[i];
k = i;
}else if(v[i] < m){
m = v[i];
k = i;
}
}

return k;
}

//get the number which i currently the smallest in the
//given vector
template <typename item>
long double Sample<item>::minimum3(vector<item> v){
long double m;
for(int i = 0; i < v.size(); i++){
if(i == 0){
m = v[i];
}else if(v[i] < m){
m = v[i];
}
}

return m;
}

template <typename item>
long double Sample<item>::median(){

vector <item> ordered_results;
vector <item> current_results = results;

int index;
int s = current_results.size();

for(int i = 0; i < s; i++){
index = minimum2(current_results);

//push the current minimum into our new array;
ordered_results.push_back(current_results[index]);

//remove that value from the array
for(vector<item>::iterator k = current_results.begin(); k !=
current_results.end(); ++k){
if (*k == minimum3(current_results)){
current_results.erase(k);
break;
}
}
}

if (ordered_results.size()%2 == 1){
int g = (ordered_results.size()/2);
return ordered_results[g];

}else{
return (ordered_results[(ordered_results.size()/2)-1] +
ordered_results[(ordered_results.size()/2)])/2.0;

}

}

template <typename item>
ostream&
Sample<item>::operator<<(ostream& os, const Sample<item>& samp){
cout << "<" << samp.results.size() << ":";

for(int i = 0; i < samp.results.size(); i++){
cout << " " << samp.results[i];
}

cout << " >";
return os;
}

template <typename item>
istream&
Sample<item>::operator>>(istream& is, Sample<item>& samp){
string s;
int count = 0;
int size;
while (is >> s){
if(count == 0 && s[0] == '<' && s[s.size()-1] == ':'){
//size = parseInt(s.substr(0,s[s.size()-2]));
size = atoi((s.substr(1,s.size()-2)).c_str());

cout << "begin size = " << size << endl;
}else if(s[s.size()-1] == '>'){
//size = parseInt(s.substr(0,s[s.size()-1]));
cout << "end " << endl;
count = 0;
break;
}else {
//size = parseInt(s.substr(0,s[s.size()-1]));
cout << count << " " << atof(s.c_str()) << endl;
samp.results.push_back(atof(s.c_str()));
}
count++;
}
return is;
}

int main(){

vector<double> a;
double h1 = 1.7976931348623158e+308;
double h2 = 1.7976931348623158e+308;
a.push_back(h1);
a.push_back(h2);
a.push_back(h2);
//string s;
Sample<double> samp(a);
while (cin >> samp){

cout << samp.minimum() << endl << samp.maximum() << endl
<< samp.range() << endl << samp.midrange() << endl
<< samp.mean() << endl << samp.variance() << endl
<< samp.std_deviation() << endl << samp.median() << endl;

}

cout << samp;

return 0;
}

Jul 23 '05 #1
5 1548
fr*******@hotmail.com wrote:
i cant seem to figure out what is wrong with this class.
Then how do you know that something *is* wrong with it? :-)

Please be more specific when describing your problem next time.
it has to do
with the input/output stream override. please can somebody help me.

[...]

template <typename item>
ostream&
Sample<item>::operator<<(ostream& os, const Sample<item>& samp){
This operator cannot be a member. It has to be a stand-alone function.
IOW, drop the "Sample<item>::" prefix. Instead it should be

template<typename item>
ostream& operator<< <item> (ostream& os, const Sample<item>& samp){
cout << "<" << samp.results.size() << ":";

for(int i = 0; i < samp.results.size(); i++){
cout << " " << samp.results[i];
}

cout << " >";
return os;
}

template <typename item>
istream&
Sample<item>::operator>>(istream& is, Sample<item>& samp){
Same here, make it a stand-alone function.
[...]

Jul 23 '05 #2
It know looks like this and it still doesnt compile.

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
template <typename Item>
class Sample {
private:
vector<Item> results;

public:
Sample(vector<Item> samps);

const vector<Item> get_data();

void set_data(const vector<Item> &v);

long double minimum();

long double maximum();

long double range();

long double midrange();

long double mean();

long double variance();

long double std_deviation();

//get the index of the current minimum double in the vector
int minimum2(vector<Item> v);

//get the number which i currently the smallest in the
//given vector
long double minimum3(vector<Item> v);

long double median();

friend ostream& operator<<(ostream& os, const Sample& samp);

friend istream& operator>>(istream& is, Sample& samp);

};

template <typename item>
Sample<item>::Sample(vector<item> samps) : results(samps) {}

template <typename item>
const vector<item> Sample<item>::get_data(){
return results;
}

template <typename item>
void Sample<item>::set_data(const vector<item> &v) {
results = v;
}

template <typename item>
long double Sample<item>::minimum(){
long double m;
for(int i = 0; i < results.size(); i++){
if(i == 0){
m = results[i];
}else if(results[i] < m){
m = results[i];
}
}

return m;
}

template <typename item>
long double Sample<item>::maximum(){
long double m;
for(int i = 0; i < results.size(); i++){
if(i == 0){
m = results[i];
}else if(results[i] > m){
m = results[i];
}
}

return m;
}

template <typename item>
long double Sample<item>::range(){
long double max = maximum();
long double min = minimum();
long double range = max - min;
return range;
}

template <typename item>
long double Sample<item>::midrange(){
long double max = maximum();
long double min = minimum();
long double mid_range = (max + min)/2.0;
return mid_range;
}

template <typename item>
long double Sample<item>::mean(){
long double sum = 0;
long double m;
for(int i = 0; i < results.size(); i++){
sum += results[i];
}
m = sum/(results.size() * 1.0);

return m;
}

template <typename item>
long double Sample<item>::variance(){
long double sum = 0;
long double m = mean();

for(int i = 0; i < results.size(); i++){
sum = sum + (pow(results[i]-m,2));
}
return sum/(results.size() * 1.0);
}

template <typename item>
long double Sample<item>::std_deviation(){
sqrt (variance());
}

//get the index of the current minimum double in the vector
template <typename item>
int Sample<item>::minimum2(vector<item> v){
double m;
int k = 0;
for(int i = 0; i < v.size(); i++){
if(i == 0){
m = v[i];
k = i;
}else if(v[i] < m){
m = v[i];
k = i;
}
}

return k;
}

//get the number which i currently the smallest in the
//given vector
template <typename item>
long double Sample<item>::minimum3(vector<item> v){
long double m;
for(int i = 0; i < v.size(); i++){
if(i == 0){
m = v[i];
}else if(v[i] < m){
m = v[i];
}
}

return m;
}

template <typename item>
long double Sample<item>::median(){

vector <item> ordered_results;
vector <item> current_results = results;

int index;
int s = current_results.size();

for(int i = 0; i < s; i++){
index = minimum2(current_results);

//push the current minimum into our new array;
ordered_results.push_back(current_results[index]);

//remove that value from the array
for(vector<item>::iterator k = current_results.begin(); k !=
current_results.end(); ++k){
if (*k == minimum3(current_results)){
current_results.erase(k);
break;
}
}
}

if (ordered_results.size()%2 == 1){
int g = (ordered_results.size()/2);
return ordered_results[g];

}else{
return (ordered_results[(ordered_results.size()/2)-1] +
ordered_results[(ordered_results.size()/2)])/2.0;

}

}

template <typename item>
ostream&
operator<< <item>(ostream& os, const Sample<item>& samp){
cout << "<" << samp.results.size() << ":";

for(int i = 0; i < samp.results.size(); i++){
cout << " " << samp.results[i];
}

cout << " >";
return os;
}

template <typename item>
istream&
operator>> <item>(istream& is, Sample<item>& samp){
string s;
int count = 0;
int size;
while (is >> s){
if(count == 0 && s[0] == '<' && s[s.size()-1] == ':'){
//size = parseInt(s.substr(0,s[s.size()-2]));
size = atoi((s.substr(1,s.size()-2)).c_str());

cout << "begin size = " << size << endl;
}else if(s[s.size()-1] == '>'){
//size = parseInt(s.substr(0,s[s.size()-1]));
cout << "end " << endl;
count = 0;
break;
}else {
//size = parseInt(s.substr(0,s[s.size()-1]));
cout << count << " " << atof(s.c_str()) << endl;
samp.results.push_back(atof(s.c_str()));
}
count++;
}
return is;
}

int main(){

vector<double> a;

a.push_back(7);
a.push_back(11);
a.push_back(2);
a.push_back(13);
a.push_back(3);
a.push_back(5);

//string s;
Sample<double> samp(a);
while (cin >> samp){

cout << samp.minimum() << endl << samp.maximum() << endl
<< samp.range() << endl << samp.midrange() << endl
<< samp.mean() << endl << samp.variance() << endl
<< samp.std_deviation() << endl << samp.median() << endl;

}

cout << samp;

return 0;
}
thanks for you time

Jul 23 '05 #3
fr*******@hotmail.com wrote:
It know looks like this and it still doesnt compile.

[...]
template <typename item>
long double Sample<item>::std_deviation(){
sqrt (variance());
Should probably be

return sqrt(variance());
}

[...]
template <typename item>
ostream&
operator<< <item>(ostream& os, const Sample<item>& samp){
My mistake. Drop the first <item>. Make it

operator << (ostream& os, const Sample<item>& samp) {

(and the other one too).

[...]


It should compile now, but probably won't link (it didn't for me).
You're on your own to find the missing function implementations.

V
Jul 23 '05 #4
Thanks anyway for your help. made the change and it does compile but
doesnt link. this is a coursework and i just dont know what is wrong
with it. its driving me crazy.

Jul 23 '05 #5
<fr*******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thanks anyway for your help. made the change and it does compile but
doesnt link. this is a coursework and i just dont know what is wrong
with it. its driving me crazy.

It should drive you to implement the missing function(s)
about which the linker is undoubtedly complaining.
The code I've seen merely *declared* a template
function. That allows the compiler to process a
call to the function. Without actually *defining*
the function, there is nothing to actually *call*,
and that fact is finally evident only at link time.

Either your prof provided the definitions, you are
supposed to write the function, or the prof has made
a mistake of some kind. One mistake, unless you have
been asleep in or missing from class, was to never
explain the roles and purposes of the compiler and linker.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2283
by: Andrew | last post by:
Hi, I am thinking about ways for efficient techniques for isolation testing. Here is the problem as I see it: Complex OO systems are designed with massive number of dependencies between modules...
0
2375
by: tyousaf | last post by:
Hi i am new to mysql and mysql++, i have installed mysql server, it is running fine. i also installed "mysql++-1.7.9gcc3.2-2.i386.rpm" (i have gcc 3.3) , first of all as the readme file says to do...
2
2069
by: Anonymous | last post by:
I'm trying to port code from VC.net to VC.net 2003. Here's a typical piece of code that's often used: template<class T> class PseudoContainer { typedef T::iterator iterator; // this line will...
1
1294
by: David2511 | last post by:
Hello, I need a little help. I try to write the following architecture : an abstract template class A Two classes derived from class A : the classes B and C which are concrete. The class...
7
1797
by: Christian Christmann | last post by:
Hi, in the past I always appreciated your help and hope that you also can help me this time. I've spent many many hours but still can't solve the problem by myself and you are my last hope. ...
11
2558
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
1
3310
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one...
5
1820
by: aaragon | last post by:
Hello everybody, I appreciate your taking the time to take a look at this example. I need some help to start the design of an application. To that purpose I'm using policy-based design. The...
15
2152
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object...
6
1690
by: StephQ | last post by:
I need to implement an algorithm that takes as input a container and write some output in another container. The containers involved are usually vectors, but I would like not to rule out the...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.