473,395 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Those crazy Templates

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::ofstream& ); //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
Nov 18 '06 #1
2 3528
Pat wrote:
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::ofstream& ); //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-lit...html#faq-35.12

Nov 18 '06 #2
Pat
doug turnbull wrote:
Pat wrote:
>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::ofstream& ); //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-lit...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
Nov 18 '06 #3

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

Similar topics

0
by: Tom Schmitt | last post by:
Hi everybody. For a long time now, I've been searching for a good, fast, reliable CMS. Most of the stuff out there (like phpnuke) is too fancy and overloaded for what I need: - more than one...
1
by: Richard | last post by:
I need help. I have a smarty based program that I am modifying. I have a smarty template file that consists of smarty and HTML. I need to integrate some PHP database calls into it. My problem...
11
by: doltharz | last post by:
Please Help me i'm doing something i though was to be REALLY EASY but it drives me crazy The complete code is at the end of the email (i mean newsgroup article), i always use Option...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
3
by: squash | last post by:
I have spent two hours trying to make sense of this script, called crazy.php. The output should be nothing because $cookie_password is nowhere defined in this script, correct? But it actually...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
28
by: cpluslearn | last post by:
Hi, I have a local class inside a function template. I am able to wrap any type in my local class. Is it legal C++? What type of class is Local? Is it a class template or regular class? Thanks...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.