Those crazy Templates

P

Pat

Hello everyone,

I have a question that even my instructor is perplexed. I have created a
template class within a header file. Then in a cpp file I have the
methods that go with the class. I have tried about everything to call
the appropriate file to get the program to work. Here is the two files:

/* This is the class file or Grocery.h */
#ifndef GROCERY_H
#define GROCERY_H

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

/**
* Used for the Lists.
*/
template< class Item, class Many, class Cost, class Character >
struct groceryList {
Item info; ////
Many howMany; // Instances.
Character added;//
Cost howMuch; ////

//A pointer to the struct template.
groceryList< Item, Many, Cost, Character > *nextLink;
groceryList< Item, Many, Cost, Character > *backLink;
};
/********************************************************************************/

/**
* Class Grocery
*/
template< class Item, class Many, class Cost, class Character >
class Grocery {
private:
//Pointers pointing to first and last portion of the
//struct groceryList class.
groceryList< Item, Many, Cost, Character > *allFirst;
double total; //Stores the total amount for all item.

public:
Grocery() {
//Setting pointers to NULL.
allFirst = NULL;
//allLast = NULL;
//subtotal = 0.00;
total = 0.00;
}
~Grocery() {
//Destroying the pointers.
groceryList< Item, Many, Cost, Character > *temp;
while( allFirst != NULL ) {
temp = allFirst;
allFirst = allFirst-> nextLink;
delete temp;
}
};

//Getting data from an order.
void getData( Item&, Many&, Cost&, Character& ); //insertFirst
void getDatas( Item&, Many&, Cost&, Character& );
void accending( Item&, Many&, Cost&, Character& ); //Sorts Accending

void decending( Item&, Many&, Cost&, Character& );
void destroylist();
void display( std::eek:fstream& ); //Displaying data from an order.

};
//Still don't understand the logistics of this one.
//If there is a function template has already been defined error,
//then comment this out and re-compile.
//After the recompile a unresolved external symbol "public:void_thiscall
(class)
//error will appear, then uncomment the #include class.
//#include "Grocery.cpp"
#endif
/********************************************************************************/

/** This is the some of the Grocery.cpp **/
#include "Grocery.h"

using namespace std;

/**
* Builds the list backwards.
*
* @param class, class, class, class
* @return none
*/
template< class Item, class Many, class Cost, class Character >
void Grocery<Item, Many, Cost, Character>::getDatas( Item& newItem,
Many& howMuc, Cost& price, Character& chars ) {

//pointer to create a new node
groceryList< Item, Many, Cost, Character > *newNode;

//create the new node
newNode = new groceryList< Item, Many, Cost, Character >;

//Setting the items into arrays in the groceryList.
newNode->info = newItem;
newNode->howMuch = price;
newNode->howMany = howMuc;
newNode->added = chars;

//insert newNode before the first
newNode->nextLink = allFirst;
allFirst = newNode;
if( allFirst == NULL ) {
allFirst = newNode;
}
}
/********************************************************************************/

The main question is how do I put my functions or methods in a separate
file such as a cpp and still have the header file in a header file
without receiving a linking error or an all ready defined error? I have
tried to include the x.cpp file and it does some weird thing but will
work after several compiles by commenting the include "Grocery.cpp" on
and off. Any ideas would be great.

Thank you,

Patrick
 
D

doug turnbull

Pat said:
Hello everyone,

I have a question that even my instructor is perplexed. I have created a
template class within a header file. Then in a cpp file I have the
methods that go with the class. I have tried about everything to call
the appropriate file to get the program to work. Here is the two files:

/* This is the class file or Grocery.h */
#ifndef GROCERY_H
#define GROCERY_H

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

/**
* Used for the Lists.
*/
template< class Item, class Many, class Cost, class Character >
struct groceryList {
Item info; ////
Many howMany; // Instances.
Character added;//
Cost howMuch; ////

//A pointer to the struct template.
groceryList< Item, Many, Cost, Character > *nextLink;
groceryList< Item, Many, Cost, Character > *backLink;
};
/********************************************************************************/

/**
* Class Grocery
*/
template< class Item, class Many, class Cost, class Character >
class Grocery {
private:
//Pointers pointing to first and last portion of the
//struct groceryList class.
groceryList< Item, Many, Cost, Character > *allFirst;
double total; //Stores the total amount for all item.

public:
Grocery() {
//Setting pointers to NULL.
allFirst = NULL;
//allLast = NULL;
//subtotal = 0.00;
total = 0.00;
}
~Grocery() {
//Destroying the pointers.
groceryList< Item, Many, Cost, Character > *temp;
while( allFirst != NULL ) {
temp = allFirst;
allFirst = allFirst-> nextLink;
delete temp;
}
};

//Getting data from an order.
void getData( Item&, Many&, Cost&, Character& ); //insertFirst
void getDatas( Item&, Many&, Cost&, Character& );
void accending( Item&, Many&, Cost&, Character& ); //Sorts Accending

void decending( Item&, Many&, Cost&, Character& );
void destroylist();
void display( std::eek:fstream& ); //Displaying data from an order.

};
//Still don't understand the logistics of this one.
//If there is a function template has already been defined error,
//then comment this out and re-compile.
//After the recompile a unresolved external symbol "public:void_thiscall
(class)
//error will appear, then uncomment the #include class.
//#include "Grocery.cpp"
#endif
/********************************************************************************/

/** This is the some of the Grocery.cpp **/
#include "Grocery.h"

using namespace std;

/**
* Builds the list backwards.
*
* @param class, class, class, class
* @return none
*/
template< class Item, class Many, class Cost, class Character >
void Grocery<Item, Many, Cost, Character>::getDatas( Item& newItem,
Many& howMuc, Cost& price, Character& chars ) {

//pointer to create a new node
groceryList< Item, Many, Cost, Character > *newNode;

//create the new node
newNode = new groceryList< Item, Many, Cost, Character >;

//Setting the items into arrays in the groceryList.
newNode->info = newItem;
newNode->howMuch = price;
newNode->howMany = howMuc;
newNode->added = chars;

//insert newNode before the first
newNode->nextLink = allFirst;
allFirst = newNode;
if( allFirst == NULL ) {
allFirst = newNode;
}
}
/********************************************************************************/

The main question is how do I put my functions or methods in a separate
file such as a cpp and still have the header file in a header file
without receiving a linking error or an all ready defined error? I have
tried to include the x.cpp file and it does some weird thing but will
work after several compiles by commenting the include "Grocery.cpp" on
and off. Any ideas would be great.

Thank you,

Patrick

I hope your instructor isn't really perplexed, because anyone who
*teaches* templates should know this. Hopefully you get a better
instructor... Anyway, this probably answers what you are looking for:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12
 
P

Pat

doug said:
I hope your instructor isn't really perplexed, because anyone who
*teaches* templates should know this. Hopefully you get a better
instructor... Anyway, this probably answers what you are looking for:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12
Outstanding, thank you. This actually has answered some question of why.
Now I just have to try some of those possible solutions out. I greatly
appreciate this. As far as my instructor is concerned, she is great and
knowledgeable. In all reality she is more than likely pushing me to find
the solution out for myself since one day I will be on my own in the
real world. In other words give a man a fish and he will eat for a day.
Teach a man to fish and he will eat for a lifetime.

Thank you again,

Patrick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top