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. Newbie Question

Newbie Question

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsquestion
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
    joshp1217
    wrote on last edited by
    #1

    I want to compile a project that I have created. The project has class objects separated in different files i.e. main.cpp, card.h, card.cpp, deck.h, deck.cpp. How am I able to do this so the deck class can create in instance of the card class i.e. vector x; I know this is really newbie but I got to know.

    D M 2 Replies Last reply
    0
    • J joshp1217

      I want to compile a project that I have created. The project has class objects separated in different files i.e. main.cpp, card.h, card.cpp, deck.h, deck.cpp. How am I able to do this so the deck class can create in instance of the card class i.e. vector x; I know this is really newbie but I got to know.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      joshp1217 wrote:

      How am I able to do this so the deck class can create in instance of the card class

      class deck
      {
      deck();
      ~deck();

      card c;
      

      }

      Or you can do it in one of the deck's members:

      void deck::foo( void )
      {
      card c;
      }


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      1 Reply Last reply
      0
      • J joshp1217

        I want to compile a project that I have created. The project has class objects separated in different files i.e. main.cpp, card.h, card.cpp, deck.h, deck.cpp. How am I able to do this so the deck class can create in instance of the card class i.e. vector x; I know this is really newbie but I got to know.

        M Offline
        M Offline
        Matthew Faithfull
        wrote on last edited by
        #3

        Remember to make sure that all the files that use a type ( e.g card class ) have a #include line that references the file that defines the type. If card is defined in card.h then deck.cpp might look something like this. //deck.cpp #include "card.h" deck::deck() { card the_cards[52];//An array of 52 cards for the deck } If the definition of deck itself, in deck.h, depends on the definition of card then it's deck.h that should #include "card.h" Think of it as, when the compiler sees #include "card.h" it goes and reads the card.h file and after that it knows about the card class otherwise it's as if it never heard of it. The trick is that every time it moves on to compiling a different .cpp file (compilation unit) it forgets everything it found out before and starts again knowning nothing except C++. Sounds dumb but it really works ;)

        Nothing is exactly what it seems but everything with seems can be unpicked.

        J 1 Reply Last reply
        0
        • M Matthew Faithfull

          Remember to make sure that all the files that use a type ( e.g card class ) have a #include line that references the file that defines the type. If card is defined in card.h then deck.cpp might look something like this. //deck.cpp #include "card.h" deck::deck() { card the_cards[52];//An array of 52 cards for the deck } If the definition of deck itself, in deck.h, depends on the definition of card then it's deck.h that should #include "card.h" Think of it as, when the compiler sees #include "card.h" it goes and reads the card.h file and after that it knows about the card class otherwise it's as if it never heard of it. The trick is that every time it moves on to compiling a different .cpp file (compilation unit) it forgets everything it found out before and starts again knowning nothing except C++. Sounds dumb but it really works ;)

          Nothing is exactly what it seems but everything with seems can be unpicked.

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

          I thought that was how it should work but when I sent it up that way, i was getting errors like the Deck class didnt recognize the Card class because when i said vector m_Cards;(this is in the deck.h with #include "card.h" at the top) it basically would say that a vector cant have any empty type. The IDE i am using is Dev-C++ I dont know if that has anything to do with it but any further help would greatly be appreciated because I could do this with ease in C# but I need to do it in C++.

          M 1 Reply Last reply
          0
          • J joshp1217

            I thought that was how it should work but when I sent it up that way, i was getting errors like the Deck class didnt recognize the Card class because when i said vector m_Cards;(this is in the deck.h with #include "card.h" at the top) it basically would say that a vector cant have any empty type. The IDE i am using is Dev-C++ I dont know if that has anything to do with it but any further help would greatly be appreciated because I could do this with ease in C# but I need to do it in C++.

            M Offline
            M Offline
            Matthew Faithfull
            wrote on last edited by
            #5

            Should that perhaps be vector<card> m_Cards; or did the html formatter just make your vector type parameter disappear?:omg: If not it might explain the empty type error, might even need #include<vector> and std::vector<card>m_Cards; There's very little of the using AClass::BClass; stuff in C++ although using namepsace std; works just fine and should let you use a vector as per the first example above. I'm no STL guru but then if you've done loads of C# you're no newbie either :-D

            Nothing is exactly what it seems but everything with seems can be unpicked.

            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