Thread: Conversion Struct to Class assistance.

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    9

    Conversion Struct to Class assistance.

    Hi, I am trying to convert part of code Struct to Class, and I understand that we just replace Struct with Class, however, I am stuck in this part where the lengthy comment is at:

    Code:
    class Book  //Previously was "struct"
    {
        public: 
            
        
    } *books[100];
    
    
    class Course  //changed from struct to class
    {
        public:
            
        
    } *course[100];
    
    
    class Class //same here, struct to class
    {
         //these were struct, but i changed them to class, but I think i am getting errors because of the pointers??  
        class Book* book;   //Help HERE! 
        class Course *course;
        
        public:
            int sectionNumber;
            bool required;
            
        
    } *classes[100];
    I commented where most of the errors coming from.
    I think my conflict resides with the pointers, since the "book" and "course" are used in other parts of the code.

  2. #2
    Registered User
    Join Date
    Jan 2017
    Posts
    9
    Nevermind I got it converted. But what if I want the "class Book" in a .h file, do I make .h file with the class Book in it, and include it in the main, will that be correct?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    It looks like you come from a background in C. With C++, you do not need to use the struct or class keyword ahead of the struct or class name when declaring a variable of that type. It is also not idiomatic C++ to declare arrays of structs or classes the way you have done it.

    Code:
    #include <iostream>
    #include <string>
    
    struct ExampleStruct
    {
      int intMember;
      std::string stringMember;
    };
    
    class ExampleClass
    {
      public:
        ExampleClass();
        ~ExampleClass();
    
        void DoSomething();
    };
    
    ExampleClass::ExampleClass()
    {
      // construction code goes here
    }
    
    ExampleClass::~ExampleClass()
    {
      // destruction code goes here
    }
    
    void ExampleClass::DoSomething()
    {
      std::cout << "ExampleClass::DoSomething()\n";
    }
    
    int main()
    {
      ExampleStruct es;
      ExampleClass ec; // ExampleClass::ExampleClass() gets called here.
    
      ExampleStruct *esArray[100]; // declares an array of 100 pointers to ExampleStruct
    
      ec.DoSomething();
    
      return 0;
      // ExampleClass::~ExampleClass() gets called here.
    }
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Jan 2017
    Posts
    9
    Ah I see, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. homework assistance wanted: using a class as a vector
    By DHart07 in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2010, 02:12 PM
  2. Looking for a little assistance with item class design...
    By Raigne in forum Game Programming
    Replies: 5
    Last Post: 10-22-2008, 08:55 AM
  3. Conversion constructor for template class
    By MarkZWEERS in forum C++ Programming
    Replies: 19
    Last Post: 05-08-2008, 01:22 PM
  4. Need Programming Assistance for Class Project
    By gbargsley in forum C++ Programming
    Replies: 2
    Last Post: 05-07-2008, 01:43 PM
  5. Making a (atom) class in a (struct) class makes big errors!
    By Yarin in forum Windows Programming
    Replies: 4
    Last Post: 09-11-2007, 07:18 PM

Tags for this Thread