Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Creating a new Template Item

Creating a new Template Item

Scheduled Pinned Locked Moved C / C++ / MFC
c++wpfdata-structuresjson
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Ken Mazaika
    wrote on last edited by
    #1

    Hi, I made a Stack implementation, and now I'm looking to make the implementation be more generic. I know that the way to do this is probably to use template. I have a cons list of items which is template class ConsList : public LList { .... ConsList(T first, LList* rest); //constructor } as of now. When I try to make an instance of the ConsList with the new keyword, I get a lot of errors g++ -o Main LList.o Stack.o Exception.o main.o Stack.o: In function `Stack::push(int)': Stack.cpp:(.text+0xaa): undefined reference to `ConsList::ConsList(int, LList*)' Stack.o: In function `Stack::Stack()': Stack.cpp:(.text+0xe5): undefined reference to `EmptyList::EmptyList()' Stack.o: In function `Stack::Stack()': Stack.cpp:(.text+0x123): undefined reference to `EmptyList::EmptyList()' collect2: ld returned 1 exit status I've googled templates, and it looks like nobody makes a new item that is a template. If there a reason why people don't use this, or how would I go about creating an instance of ConsList(.., ..). Thanks, -Ken

    Steve EcholsS R 2 Replies Last reply
    0
    • K Ken Mazaika

      Hi, I made a Stack implementation, and now I'm looking to make the implementation be more generic. I know that the way to do this is probably to use template. I have a cons list of items which is template class ConsList : public LList { .... ConsList(T first, LList* rest); //constructor } as of now. When I try to make an instance of the ConsList with the new keyword, I get a lot of errors g++ -o Main LList.o Stack.o Exception.o main.o Stack.o: In function `Stack::push(int)': Stack.cpp:(.text+0xaa): undefined reference to `ConsList::ConsList(int, LList*)' Stack.o: In function `Stack::Stack()': Stack.cpp:(.text+0xe5): undefined reference to `EmptyList::EmptyList()' Stack.o: In function `Stack::Stack()': Stack.cpp:(.text+0x123): undefined reference to `EmptyList::EmptyList()' collect2: ld returned 1 exit status I've googled templates, and it looks like nobody makes a new item that is a template. If there a reason why people don't use this, or how would I go about creating an instance of ConsList(.., ..). Thanks, -Ken

      Steve EcholsS Offline
      Steve EcholsS Offline
      Steve Echols
      wrote on last edited by
      #2

      Maybe select the "Ignore HTML tags in this message" check box and re-post your code. There are a lot of sad faces in it.


      - S 50 cups of coffee and you know it's on!

      • S
        50 cups of coffee and you know it's on!
        Code, follow, or get out of the way.
      1 Reply Last reply
      0
      • K Ken Mazaika

        Hi, I made a Stack implementation, and now I'm looking to make the implementation be more generic. I know that the way to do this is probably to use template. I have a cons list of items which is template class ConsList : public LList { .... ConsList(T first, LList* rest); //constructor } as of now. When I try to make an instance of the ConsList with the new keyword, I get a lot of errors g++ -o Main LList.o Stack.o Exception.o main.o Stack.o: In function `Stack::push(int)': Stack.cpp:(.text+0xaa): undefined reference to `ConsList::ConsList(int, LList*)' Stack.o: In function `Stack::Stack()': Stack.cpp:(.text+0xe5): undefined reference to `EmptyList::EmptyList()' Stack.o: In function `Stack::Stack()': Stack.cpp:(.text+0x123): undefined reference to `EmptyList::EmptyList()' collect2: ld returned 1 exit status I've googled templates, and it looks like nobody makes a new item that is a template. If there a reason why people don't use this, or how would I go about creating an instance of ConsList(.., ..). Thanks, -Ken

        R Offline
        R Offline
        Rajkumar R
        wrote on last edited by
        #3

        Ken Mazaika wrote:

        Stack.cpp.text+0xaa): undefined reference to `ConsList::ConsList(int, LList*)'

        It seems that you have n't defined the functions of the template classes. At the point of template instantiation the definition of the function should be visible where it is triggered by refering in the code (of stack). May be you declared the template class in a header file which doesnot have definitions (possibly you defined in LList.cpp) and included the header in stack.cpp. But template class requires the definitions also , so add the definitions in LList.cpp to the header itself.

        K 1 Reply Last reply
        0
        • R Rajkumar R

          Ken Mazaika wrote:

          Stack.cpp.text+0xaa): undefined reference to `ConsList::ConsList(int, LList*)'

          It seems that you have n't defined the functions of the template classes. At the point of template instantiation the definition of the function should be visible where it is triggered by refering in the code (of stack). May be you declared the template class in a header file which doesnot have definitions (possibly you defined in LList.cpp) and included the header in stack.cpp. But template class requires the definitions also , so add the definitions in LList.cpp to the header itself.

          K Offline
          K Offline
          Ken Mazaika
          wrote on last edited by
          #4

          Thanks for your response. That solved the problem completely. I created a LList.cpp file, which I didn't treat as a header, I added the template definitions to the header and it worked perfectly. Thanks again for your response! -Ken

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups