Can't separate file".h" and ".cpp" into the different file!!Help me please

This is a discussion on Can't separate file".h" and ".cpp" into the different file!!Help me please within the C++ Programming forums, part of the General Programming Boards category; I'm using MVS C++ 6.0 Compiler,I can't type ".h" files and ".cpp" files into the different files.Compiler informed errors that: ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    52

    Post Can't separate file".h" and ".cpp" into the different file!!Help me please

    I'm using MVS C++ 6.0 Compiler,I can't type ".h" files and ".cpp" files into the different files.Compiler informed errors that:

    "error LNK2001: unresolved external symbol "public: __thiscall Queue<int>::~Queue<int>(void)" (??1?$Queue@H@@QAE@XZ)

    error LNK2001: unresolved external symbol "public: enum ErrorCode __thiscall Queue<int>::Serve(void)" (?Serve@?$Queue@H@@QAE?AW4ErrorCode@@XZ)

    error LNK2001: unresolved external symbol "public: enum ErrorCode __thiscall Queue<int>::Append(int const &)" (?Append@?$Queue@H@@QAE?AW4ErrorCode@@ABH@Z)

    error LNK2001: unresolved external symbol "public: __thiscall Queue<int>::Queue<int>(void)" (??0?$Queue@H@@QAE@XZ)

    Debug/Queue.exe : fatal error LNK1120: 4 unresolved externals
    "
    So I have to type contents of ".h" files and ".cpp" files into a file and Compiler doesn't catch errors,but it's very long .I don't understand what these errors is......Everybody can help me...Thanks very much
    This my code:

    "Queue.h"

    Code:
    #ifndef QUEUE_H
    #define QUEUE_H
    #include "NodeQueue.h"
    enum ErrorCode{sucess, overflow,underflow};
    template <class Entry>
    class Queue{
    	public:
    		Queue();
    		~Queue();
    		Queue(const Queue<Entry> &Qqueue);
    		void operator=(const Queue<Entry> &Qqueue);
    		bool Empty() const;
    		ErrorCode Append(const Entry &item);
    		ErrorCode Serve();
    		ErrorCode Retrieve(Entry &item) const;
    		bool Full() const;
    		int Size() const;
    		void Clear();
    	private:
    		int count;
    		Node<Entry> *pfront,*ptail;
    };
    
    #endif
    "NodeQueue.h"

    Code:
    #ifndef NODEQUEUE_H
    #define NODEQUEUE_H
    
    template <class Entry>
    struct Node{
    	Entry entry;
    	Node<Entry> * next;
    	Node();
    	Node(Entry item,Node<Entry> *p=NULL);
    };
    #endif
    "NodeQueue.cpp"

    Code:
    #include "NodeQueue.h"
    
    template <class Entry>
    Node<Entry>::Node(){
    	next = NULL;
    }
    
    template <class Entry>
    Node<Entry>::Node (Entry item,Node<Entry> * p){
    	entry = item;
    	next = p;
    }
    "Queue.cpp"

    Code:
    include <iostream>
    #include "NodeQueue.h"
    #include "Hangdoi.h"
    
    using namespace std;
    
    template <class Entry>
    Queue<Entry>::Queue(){
    	pfront = ptail = NULL;
    	count = 0;
    }
    
    template <class Entry>
    Queue<Entry>::~Queue(){
    	while(!Empty()){
    		Serve();
    	delete pfront;
    	delete ptail;
    }
    
    template <class Entry>
    ErrorCode Queue<Entry>:: Append(const Entry &item){
    	Node<Entry> * ptam = new Node<Entry>(item);
    	
    	if (ptam==NULL)
    	{
    		cout<<"Hang doi da day!";
    		return overflow;
    	}
    	
    	if (ptail==NULL)
    		pfront = ptail = ptam;
    	else
    	{
    		ptail->next = ptam;
    		ptail = ptail->next;
    		count++;
    	}
    	return sucess;
    }
    
    template <class Entry>
    ErrorCode Queue<Entry>::Serve(){
    	if (pfront==NULL)
    	{
    		cout<<"Hang doi da rong!";
    		return underflow;
    	}
    	else
    	{
    		Node<Entry> * pgiu = pfront;
    		pfront = pfront -> next;
    		if (pfront==NULL)
    			ptail = pfront;
    		delete pgiu;
    		count--;
    	}
    return sucess;
    }
    
    template <class Entry>
    bool Queue<Entry>::Empty() const {
    	if (pfront== NULL)
    		return true;
    	else
    	return false;
    }
    
    template <class Entry>
    ErrorCode Queue<Entry>::Retrieve(Entry &item) const{
    	Node<Entry> * ptam = Node<Entry>(pfront->entry,pfront->next);
    	if (pfront==NULL)
    	{
    		cout<<"Hang doi da rong!";
    		return underflow;
    	}
    	else
    	cout<<ptam->entry;
    	return sucess
    }
    
    template <class Entry>
    void Queue<Entry>::Clear(){
    	while (pfront!= NULL)
    		Serve();
    	return sucess;
    }
    
    template <class Entry>
    bool Queue<Entry>::Full() const{
    	Entry thu;
    	Node <Entry> * pthu;
    	try
      {
        pthu = new Node<Entry>;
        delete pthu;
        return false;
      }
      catch(std::bad_alloc exception)
      {
        return true;
      }
    }
    
    template <class Entry>
    int Queue<Entry>::Size() const{
    	return count;
    }
    
    template <class Entry>
    void Queue<Entry>::operator =(const Queue<Entry> &Qqueue){
    	Node<Entry> * pfrontsao,*ptailsao,*pduyet,*pfronttam = Qqueue.pfront, *ptailtam = Qqueue.ptail;
    	if (pfronttam==NULL) pfrontsao=NULL;
    	else{
    		pfrontsao = pduyet = new Node<Entry>(pfronttam->entry);
    		ptailsao = new Node<Entry>(ptailtam->entry);
    		while(pfronttam->next != NULL)
    		{
    			pfronttam = pfronttam->next;
    			pduyet->next = new Node<Entry>(pfronttam->entry);
    			pduyet = pduyet->next;
    		}
    		
    		while (!Empty())
    			Serve();
    		
    		pfront = pfrontsao;
    		ptail = ptailsao;
    	}
    }
    
    template <class Entry>
    Queue<Entry>::Queue(const Queue<Entry> &Qqueue){
    	Node<Entry> * pduyet,*pfronttam = Qqueue.pfront,*ptailtam = Qqueue.ptail;
    	if (pfronttam==NULL) pfront = NULL;
    	else
    	{
    		pfront = pduyet = new Node<Entry>(pfronttam->entry);
    		ptail = new Node<Entry>(ptailtam->entry);
    		while (pfronttam->next != NULL){
    			pfronttam = pfronttam->next;
    			
    			pduyet->next = new Node<Entry>(pfronttam->entry);
    			pduyet = pduyet->next;
    		}
    	}
    	}
    }

    "UseQueue.cpp"

    Code:
    #include <iostream>
    #include <conio.h>
    #include "Queue.h"
    #include "NodeQueue.h"
    
    using namespace std;
    
    void main(){
    	Queue<int> Queue1;
    	Queue1.Append(5);
    	Queue1.Append(6);
    	Queue1.Append(1);
    	Queue1.Append(8);
    	Queue1.Serve();
    	Queue1.Serve();
    	getch();
    }
    Program couldn't run.It informed these above errors.But when I typed them(files) into a same file,it run...
    Last edited by zaracattle; 10-01-2006 at 05:42 AM.

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    I'm using MVS C++ 6.0 Compiler
    It is highly recommended not using that old compiler and IDE.

    I am not sure about this but I think you should define templates in headers because they should be linked at compile time not link time.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Templates need to be in header files, otherwise the compiler won't know which ones to instantiate.

    Also, main returns int.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    please type more clearly,how to declare template in header ....

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Copy and paste the templates from the .cpp file to the .h file
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    please type more clearly,how to declare template in header ....
    You should define them. It means their code body.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    Quote Originally Posted by Salem
    Copy and paste the templates from the .cpp file to the .h file
    I think we can use codes that separate in the different files,don't need copy codes from cpp.file -> h.files.I included ".h" file in ".cpp" file,so I think compiler can understand...And I read a lots of Book that allow this...Anybody have other ways??please help me.....

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    No, you're not listening... templated classes have to be in the header with the definition, otherwise as Salem said, the compiler doesn't know which one to instantiate. Copy and paste the implementation from the .cpp to the .h underneath the definition.
    Sent from my iPadŽ

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    You can't use this technique for templates:
    footempel.h (declarations of templates)
    footempel.cpp (definitions of them)
    main.cpp (main() with footempel.h included)

    Why? Because compiler needs the code body (definitions) to generate code for templates.
    When compiler compiles main.cpp it doesn't have access to footempel.cpp.

    And trust Salem, he is a moderator and very expert.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    Ok,thanks for interesting of everybody....
    Last edited by zaracattle; 10-01-2006 at 04:54 PM.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835

Popular pages Recent additions subscribe to a feed

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