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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Declaring a global variable of a template type

Declaring a global variable of a template type

Scheduled Pinned Locked Moved C / C++ / MFC
questionwpf
3 Posts 2 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.
  • T Offline
    T Offline
    tguzella
    wrote on last edited by
    #1

    Hi there I am writing a program to use linked lists using templates, and i am trying to make it as fast as possible, so i am thinking of storing the address of the last inserted item. Each node of the list if declared as follows: template struct NodeList { ItemType *item; NodeList *next; } what i am trying to do is to declare a global variable like template NodeList *last_inserted=NULL; , but the compiler won't allow me to do that. If instead i declare it as NodeList<> *last_inserted=NULL, it is ok, but it would work for integers only. How can I get this to work??? Thanks

    J 1 Reply Last reply
    0
    • T tguzella

      Hi there I am writing a program to use linked lists using templates, and i am trying to make it as fast as possible, so i am thinking of storing the address of the last inserted item. Each node of the list if declared as follows: template struct NodeList { ItemType *item; NodeList *next; } what i am trying to do is to declare a global variable like template NodeList *last_inserted=NULL; , but the compiler won't allow me to do that. If instead i declare it as NodeList<> *last_inserted=NULL, it is ok, but it would work for integers only. How can I get this to work??? Thanks

      J Offline
      J Offline
      Johnny
      wrote on last edited by
      #2

      Have you thought about using the STL to do this? It does exactly what you need, is about as fast as you can get, and most importantly, it works without you needing to re-invent something. An example can be found here[^]. If you really must create your own linked list, then there is no need to use a global (which is a bad thing to do anyway) for the next position. Instead, store it in the class that provides the list operations.

      T 1 Reply Last reply
      0
      • J Johnny

        Have you thought about using the STL to do this? It does exactly what you need, is about as fast as you can get, and most importantly, it works without you needing to re-invent something. An example can be found here[^]. If you really must create your own linked list, then there is no need to use a global (which is a bad thing to do anyway) for the next position. Instead, store it in the class that provides the list operations.

        T Offline
        T Offline
        tguzella
        wrote on last edited by
        #3

        I wish I could do that, but this is my assignment for my computer science project... Thanks for the reply. Thiago Guzella

        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