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: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 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: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