Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

template class - have a problem

Status
Not open for further replies.

GreatBloke

Programmer
Oct 2, 2000
2
GB
Hi,

I'm trying to create a very small and simple template class but am having problems.

here is my header file code...

//BookStock.h
#ifndef BookStock
#define BookStock

template< class T >
class BookStocks
{
private:
int numInStock;
T price; //price either int or float
char author[20], title[20];


public: //author//title //Number //price
BookStocks(char[], char[], int, T);
T ChangePrice( T );//recieves amount to change price by and returns new price
void Display();
};

here is my BookStock.cpp file...

//BookStocks.cpp - the base class or superclass

#include <iostream>
#include <iomanip>

#include &quot;bookstock.h&quot;

template< class T >
BookStocks< T >::BookStocks(char auth[], char tit[] , int num, T p)
{
author = auth;
title = tit;
numInStock = num;
price = p;
}

template< class T >
void BookStocks< T >::Display()
{
cout << &quot;Details of bookstock are as follows...\n\n&quot;;
cout << &quot;Author: &quot; << author << endl
<< &quot;Title: &quot; << title << endl
<< &quot;Number in Stock: &quot; << numInStock << endl
<< &quot;Price in Euros: &quot; << priceEuro << endl;
}

template< class T >
T BookStocks< T >::ChangePrice( T )
{
//p is the amount to be altered by either negative or positive

if (T > 0) //positive
price += T;
else //negative
price -= T;

return price;
}

and here is my main.cpp file...

#include <iomanip>
#include &quot;Bookstock.h&quot;

int main()
{

cout << &quot;Enter the author: &quot; << endl;
char a[20];
cin >> a;
cout << &quot;Enter the title: &quot; << endl;
char ti[20];
cin >> ti;
cout << &quot;Enter the number in stock: &quot; << endl;
int n;
cin >> n;
cout << &quot;Do you want to enter price in...&quot; << endl
<< &quot;1. Sterling&quot; << endl
<< &quot;2. Euro&quot; << endl;
int option;
cin >> option;

if (option == 1)
{
cout << &quot;Enter the price in sterling (float): &quot; << endl;
float fp;
cin >> fp;
// BookStocks< float > floatBook(a, ti, n, ps);

// floatBook bs1(a, ti, n, ps);
// bs1.Display;

BookStocks bs1(a, ti, n, T fp);

}
else
{
cout << &quot;Enter the price in euros (int): &quot; << endl;
int ip;
cin >> ip;
}
return 0;
}
#endif


As you can see, I'm having trouble creating a new instance of class bookstock with the T parameter.

Any help with this would be excellent..
Thanks
John
 
What does it mean?
BookStocks bs1(a, ti, n, T fp);
1. Type T is one a template class and not an argument for a ctor.
2. As you can see, when trying to execute ctor, you declare a fp of type T. It is a nonsense.
3. You can declare only as in thirst way or
typedef BookStocks< float > floatBook;
floatBook x(a,b,s,d);
... John Fill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top