How to include a class in another class.

This is a discussion on How to include a class in another class. within the C++ Programming forums, part of the General Programming Boards category; I'm supposed to make a Queue Class to operate like a Queue does with enqueue, dequeue, print, etc. It is ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    36

    How to include a class in another class.

    I'm supposed to make a Queue Class to operate like a Queue does with enqueue, dequeue, print, etc. It is supposed to operate using a Doubly Linked List class, by calling on members of that class such as InsertLast() for enqueue which I have already written and gotten working but I'm having problems declaring my doublylinkedlist class in my queue header file under private.

    It is set that "stdafx.h" has all the includes in it, including DoublyLinkedList.h.

    Any help is greatly appreciated.

    Code:
    #pragma once
    
    #include "stdafx.h"
    
    
    
    	class Queue {
    
    //		=======
    		public:
    
    //		===========
    //		Constructor
    //		============
    		Queue(void);
    //		=============
    
    //		==========
    //		Destructor
    //		=============
    		~Queue(void);
    //		=============
    
    //		================
    //		Member Functions
    //		=======================
    		void InitializeQueue2();
    		bool IsEmpty2();
    		bool IsFull2();
    		void Front2();
    		void Back2();
    		void Enqueue2( int );
    		void Dequeue2(int&);
    		void PrintQueue2();
    		void EnqueueAndDequeue2();
    //		==========================
    
    		friend DoublyLinkedList; // This is here because its my failed attempt at trying to fix the error
    
    	private:
    
    		DoublyLinkedList list;
    
    	};
    Error 1 error C2079: 'Queue::list' uses undefined class 'DoublyLinkedList' c:\users\jesse\desktop\queue class 2.0\queue class 2.0\queue.h 44
    Last edited by Jesse20ghet; 06-15-2011 at 10:29 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Perhaps you did not include the appropriate header in the source file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    I did, its included in stafx.h.

    stdafx.h
    Code:
    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently
    //
    
    #pragma once
    
    #include "targetver.h"
    
    #include <stdio.h>
    #include <tchar.h>
    
    
    #include "Types.h"
    #include "Node.h"
    #include "Queue.h"
    #include "Menu.h"
    #include "DoublyLinkedList.h"
    
    
    
    
    // TODO: reference additional headers your program requires here

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    When the compiler reads Queue.h it doesn't know what DoublyLinkedList is because that header file hasn't been processed yet. Move the include for DoublyLinkedList.h above Queue.h

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Thank you Mike, that sure did fix the problem.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    I suggest that you don't use precompiled headers. Leave their use for when you are already fairly proficient in C++ and are working on large projects where they can make a noticeable impact on compilation time.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Using precompiled headers is fine so long as you understand what they are and how they work.
    Precompiled header - Wikipedia, the free encyclopedia
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header include problem - class not declared...
    By jontes in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2010, 03:18 PM
  2. Class Include Problem (I Think)
    By gpr1me in forum C++ Programming
    Replies: 8
    Last Post: 03-21-2006, 11:47 AM
  3. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  4. Template <class T1, class T2, class T3> error LNK2019
    By JonAntoine in forum C++ Programming
    Replies: 9
    Last Post: 10-11-2004, 12:25 PM
  5. Replies: 1
    Last Post: 12-11-2002, 09:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21