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. Template

Template

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresquestion
5 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.
  • J Offline
    J Offline
    james_dixon_2008
    wrote on last edited by
    #1

    Hello developers, Can i define a class in header file and declare it in .cpp file if using template or do i have to define and declare in the same header file. I have got two classes so do i have to define and declare both in the same header file or is it possible to have different header file for both classes. I am writing double linked list application. Now i have got two class LList and Node. Now when i simply use template and write the two classes under one file without defining it , then it works perfectly fine but when i try to define the two class seperately i am getting many errors. What should i do in this case ? Any way out ? ThankYou

    M N 2 Replies Last reply
    0
    • J james_dixon_2008

      Hello developers, Can i define a class in header file and declare it in .cpp file if using template or do i have to define and declare in the same header file. I have got two classes so do i have to define and declare both in the same header file or is it possible to have different header file for both classes. I am writing double linked list application. Now i have got two class LList and Node. Now when i simply use template and write the two classes under one file without defining it , then it works perfectly fine but when i try to define the two class seperately i am getting many errors. What should i do in this case ? Any way out ? ThankYou

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      When you split the two classes into two header files, did you remember to #include the dependent header file in the one that depends on it? If these are template classes, there shouldn't be any code in CPP files. Actual intances of the template class aren't created until an object of the class is defined, so at that point the compiler would need to see the code in the CPP file. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      J 1 Reply Last reply
      0
      • J james_dixon_2008

        Hello developers, Can i define a class in header file and declare it in .cpp file if using template or do i have to define and declare in the same header file. I have got two classes so do i have to define and declare both in the same header file or is it possible to have different header file for both classes. I am writing double linked list application. Now i have got two class LList and Node. Now when i simply use template and write the two classes under one file without defining it , then it works perfectly fine but when i try to define the two class seperately i am getting many errors. What should i do in this case ? Any way out ? ThankYou

        N Offline
        N Offline
        Nibu babu thomas
        wrote on last edited by
        #3

        james_dixon_2008 wrote:

        Can i define a class in header file and declare it in .cpp file if using template

        Read this -> http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12[^]


        Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http:\\nibuthomas.wordpress.com

        1 Reply Last reply
        0
        • M Mark Salsbery

          When you split the two classes into two header files, did you remember to #include the dependent header file in the one that depends on it? If these are template classes, there shouldn't be any code in CPP files. Actual intances of the template class aren't created until an object of the class is defined, so at that point the compiler would need to see the code in the CPP file. Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          J Offline
          J Offline
          james_dixon_2008
          wrote on last edited by
          #4

          Thanks, yes i included the dependent header file but i got many errors. But when include the node class declaration and defintion and llist class declartion in one header file then the program works. But yes if i try to declare the llist class first and then define it, it gives me many error. So i came to the conclusion that i have included all the code under one header file. Declaring template node class and then defining all the functions and then directly defining the template llist class as it doesn't work when i first declare it and then define it. I guess that each template class could be placed in different header file to use by main method. But in my case it's not working. So i have only main class and the one header file containing two template class.

          M 1 Reply Last reply
          0
          • J james_dixon_2008

            Thanks, yes i included the dependent header file but i got many errors. But when include the node class declaration and defintion and llist class declartion in one header file then the program works. But yes if i try to declare the llist class first and then define it, it gives me many error. So i came to the conclusion that i have included all the code under one header file. Declaring template node class and then defining all the functions and then directly defining the template llist class as it doesn't work when i first declare it and then define it. I guess that each template class could be placed in different header file to use by main method. But in my case it's not working. So i have only main class and the one header file containing two template class.

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            If the list class references the node class then the node class will always have to be defined before (in compile order) the list class, whether you use separate header files or not. The following is no different than using a single header file for both classes...

            /////////////////
            // node.h

            #pragma once

            ...define node class...

            /////////////////
            // list.h

            #pragma once

            #include "node.h"

            ...define list class...

            ////////////////
            // any file that uses the list class

            #pragma once

            #include "list.h"
            ...

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            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