Thread: Wont Compile In VS.NET

  1. #1
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784

    Wont Compile In VS.NET

    Code:
    //stdafx.h
    #pragma once
    #define WIN32_LEAN_AND_MEAN	// Exclude rarely-used stuff from Windows headers
    #include <stdio.h>
    #include <tchar.h>
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    /************************************/
    
    //Queue.h
    #ifndef GUARD_CQueue_H
    #define GUARD_CQueue_H
    
    template <class T>
    class CQueue
    {
    public:
    		//exception classes
    		class CqueueFull{};
    		class CqueueEmpty {};
    private:
    		typedef struct node_s 
    		{
    			T item;
    			node_s * next;
    			node_s (T x) { item = x; next = NULL;}
    		}LList;
    
    		LList *head, *tail;
    		int max_size;
    		int current_size;
    
    public:
    		CQueue(int iSize);
    		inline bool empty() const { return head == NULL; }
    		void In(T x);
    		T Out();
    };
    
    /******************************/
    //Queue.cpp
    #include "stdafx.h"
    #include "CQueue.h"
    
    template<class T>
    CQueue<T>::CQueue(int iSize) 
    { 
    	max_size = iSize;
    	current_size = 0;
    	head = NULL;
    }
    
    template<class T>
    T CQueue<T>::Out()
    {
    	if(!empty())
    	{
    		T v = head->item; 
    		LList *t = head->next;
    		delete head;
    		head = t;
    		return v;
    	}else
    	{
    		throw CqueueEmpty();
    	}
    }
    
    template <class T> 
    void CQueue<T>::In(T x) 
    {	
    		if( current_size < max_size)
    		{
    			current_size ++;
    			LList *t = tail;
    			tail = new node_s(x);
    			if(head == NULL) 
    			{
    				head = tail;
    			}else
    			{
    				t->next = tail;
    			}
    		}else
    		{
    			throw CqueueFull();
    		}
    }
    /*******************************/
    //Main.cpp
    #include "stdafx.h"
    #include "CQueue.h"
    
    int main()
    {
    	try
    	{
    
    		CQueue<double> Q(16);
    		Q.In(12.3);
    		Q.In(45.2);
    		Q.In(67.69);
    		Q.In(8.98);
    
    
    		cout << "Out: " << Q.Out() << endl;
    		cout << "OUt: " << Q.Out() << endl;
    		cout << "OUt: " << Q.Out() << endl;
    		cout << "OUt: " << Q.Out() << endl;
    	}
    	catch(CQueue<double>::CqueueFull)
    	{
    		cout << "Queue is full." << endl;
    	}
    	catch(CQueue<double>::CqueueEmpty)
    	{
    		cout << "Queue is Empty." << endl;
    	}
    
    	return 0;
    }
    What is the error? I get 3 'unresolved external' errors!

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I said the files were named Queue.h, Queue.cpp, but I meant CQueue. Yup I get those unresolved external errors. How do I fix it?

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    These are the actual error messages:
    Code:
    c:\Program Files\Microsoft Visual Studio.NET\Vc7\include\useoldio.h(29): warning C4995: '_OLD_IOSTREAMS_ARE_DEPRECATED': name was marked as #pragma deprecated
    BonusLab error LNK2019: unresolved external symbol "public: double __thiscall CQueue<double>::Out(void)" (?Out@?$CQueue@N@@QAENXZ) referenced in function _main
    BonusLab error LNK2019: unresolved external symbol "public: void __thiscall CQueue<double>::In(double)" (?In@?$CQueue@N@@QAEXN@Z) referenced in function _main
    BonusLab error LNK2019: unresolved external symbol "public: __thiscall CQueue<double>::CQueue<double>(int)" (??0?$CQueue@N@@QAE@H@Z) referenced in function _main
    BonusLab fatal error LNK1120: 3 unresolved externals
    BonusLab is just the project workspace that I used to write this code.

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    In main.cpp you'll need to change

    #include "CQueue.h"

    to

    #include "CQueue.cpp"
    zen

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Damn, that worked. And I already reported this to Microsoft!

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    why is that necessary zen? surely including the header file is the 'right' thing to do?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I just posed that question to zen too.

  8. #8
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Microsofts compliers don't seem to treat template classes as part of the project when they are in different files, so you have to make sure everythings included in the main file.
    zen

  9. #9
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144

    zen i have a question?

    i was taught that method definitions in a class template must be in the same file as the class definition regardless of complier. So i've always done it this way. Are there compilers that will compile a class template with the method definitions in a separate file?
    THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I reckon that code will compile on most non-microsoft compilers.....

    unfortuneately i only have msvc installed!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    i was taught that method definitions in a class template must be in the same file as the class definition regardless of complier. So i've always done it this way. Are there compilers that will compile a class template with the method definitions in a separate file?
    You could be partially right there, as I had linker errors trying it with Dev C++. However you don't have to have them in the same file, just ensure that all files are included in the file that creates an instance of the template.
    zen

  12. #12
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I said the files were named Queue.h, Queue.cpp, but I meant CQueue. Yup I get those unresolved external errors. How do I fix it?
    What I usually do is to have
    Queue.h #include Queue.cpp at the bottom the
    header file. Queue.cpp is never compiled and doesn't #include
    Queue.h, this basically just keeps the implementation out of the header file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 08:20 AM
  2. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  3. open scene graph compile issues
    By ichijoji in forum Game Programming
    Replies: 1
    Last Post: 08-04-2005, 12:31 PM
  4. compile program?
    By Goosie in forum C++ Programming
    Replies: 9
    Last Post: 06-22-2005, 02:26 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM