473,662 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_th iscall
(class)
//error will appear, then uncomment the #include class.
//#include "Grocery.cp p"
#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>::get Datas( 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.cp p" on
and off. Any ideas would be great.

Thank you,

Patrick
Nov 18 '06 #1
2 3542
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_th iscall
(class)
//error will appear, then uncomment the #include class.
//#include "Grocery.cp p"
#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>::get Datas( 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.cp p" 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_th iscall
(class)
//error will appear, then uncomment the #include class.
//#include "Grocery.cp p"
#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>::get Datas( 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.cp p" 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
1665
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 Menu-Layer... submenus and all - MySQL-support - configurable templates - custom input templates (not a must) That's all I need. Do you have any suggestions on what to use/try out?
1
2306
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 is I cannot figure out how to pass value between smarty and PHP. Going from php to smarty I've tried this: {php} $smarty = new Smarty; $smarty->assign("x", 100);
11
3342
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 Explicit and Response.Expires=-1,
25
3317
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 same subject in clc++m to get the more of the context. Ted
28
2627
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 executable size. This should not cause any performance issue. So, is it safe to say that if I can offered to have bigger image size I can go ahead and use Templates with out worrying about any other issues?
3
1765
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 outputs the value that other scripts i have running set it to. Why should crazy.php care what other scripts are running that use that variable name?? <?php crazy();
0
3946
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 Inbox Reply from Craig Somerford <uscos@2barter.net> hide details 10:25 pm (3 minutes ago)
104
4561
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 feeling the answer I'm going to get back will be "no, because templates have taken on a life of their own since their original conception and now also affect compiler implementation" (read: not good, IMO. John
28
2546
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 in advance. --dhina --------------------------------------------------------------------------------------------------------------------------------------------- class Foo { public:
0
8435
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7368
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5655
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4181
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
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 we have to send another system

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.