Thread: How to include a class in another class.

  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
    28,412
    Perhaps you did not include the appropriate header in the source file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 04:18 PM
  2. Class Include Problem (I Think)
    By gpr1me in forum C++ Programming
    Replies: 8
    Last Post: 03-21-2006, 12:47 PM
  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, 10:31 PM