Those crazy Templates

Discussion in 'C++' started by Pat, Nov 18, 2006.

  1. Pat

    Pat Guest

    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
    Pat, Nov 18, 2006
    #1
    1. Advertising

  2. 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::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
    doug turnbull, Nov 18, 2006
    #2
    1. Advertising

  3. Pat

    Pat Guest

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

    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
    Pat, Nov 18, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BinnuChowdary
    Replies:
    1
    Views:
    536
    Swanand Mokashi
    May 1, 2006
  2. BinnuChowdary
    Replies:
    0
    Views:
    411
    BinnuChowdary
    May 2, 2006
  3. BinnuChowdary
    Replies:
    1
    Views:
    546
    =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=
    May 2, 2006
  4. JKop
    Replies:
    3
    Views:
    467
  5. recover
    Replies:
    2
    Views:
    799
    recover
    Jul 25, 2006
Loading...

Share This Page