14 Replies - 2378 Views - Last Post: 15 May 2010 - 05:49 PM Rate Topic: -----

#1 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Some help on a dynamic queue?

Posted 15 May 2010 - 03:06 PM

I had this same problem with my previous assignment, but I just moved all the class code into my driver program and it fixed it. But I don't wanna do it this time, and would like to figure out why i get this issue.

errors I am receiving::
1>  dynamicQueue.cpp
1>18-3.obj : error LNK2019: unresolved external symbol "public: __thiscall dynTempQueue<int>::~dynTempQueue<int>(void)" (??1?$dynTempQueue@H@@QAE@XZ) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: __thiscall dynTempQueue<double>::~dynTempQueue<double>(void)" (??1?$dynTempQueue@N@@QAE@XZ) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: __thiscall dynTempQueue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::~dynTempQueue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" (??1?$dynTempQueue@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: void __thiscall dynTempQueue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::dequeue(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?dequeue@?$dynTempQueue@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEXAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: bool __thiscall dynTempQueue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::isEmpty(void)" (?isEmpty@?$dynTempQueue@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE_NXZ) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: void __thiscall dynTempQueue<double>::dequeue(double &)" (?dequeue@?$dynTempQueue@N@@QAEXAAN@Z) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: bool __thiscall dynTempQueue<double>::isEmpty(void)" (?isEmpty@?$dynTempQueue@N@@QAE_NXZ) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: void __thiscall dynTempQueue<int>::dequeue(int &)" (?dequeue@?$dynTempQueue@H@@QAEXAAH@Z) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: bool __thiscall dynTempQueue<int>::isEmpty(void)" (?isEmpty@?$dynTempQueue@H@@QAE_NXZ) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: void __thiscall dynTempQueue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::enqueue(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?enqueue@?$dynTempQueue@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: void __thiscall dynTempQueue<double>::enqueue(double)" (?enqueue@?$dynTempQueue@N@@QAEXN@Z) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: void __thiscall dynTempQueue<int>::enqueue(int)" (?enqueue@?$dynTempQueue@H@@QAEXH@Z) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: __thiscall dynTempQueue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::dynTempQueue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" (??0?$dynTempQueue@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: __thiscall dynTempQueue<double>::dynTempQueue<double>(void)" (??0?$dynTempQueue@N@@QAE@XZ) referenced in function _main
1>18-3.obj : error LNK2019: unresolved external symbol "public: __thiscall dynTempQueue<int>::dynTempQueue<int>(void)" (??0?$dynTempQueue@H@@QAE@XZ) referenced in function _main
1>C:\Users\Mysterious\Desktop\CSIS 297\Assignment 12\18-3\Debug\18-3.exe : fatal error LNK1120: 15 unresolved externals





header.h
#ifndef HEADER_H
#define HEADER_H

template <class T>
class dynTempQueue	// Dynamic queue based on a templte
{
	private:
		class qNode
		{
			friend class dynTempQueue;
			T userInput;		// userInput data type will be whatever is passed to it from driver
			qNode *next;
			qNode(T userInput1, qNode *next1 = NULL)
			{
				userInput = userInput1;
				next = next1;
			}
		};
		// To track the rear and front of the queue
		qNode *front;
		qNode *rear;
	public:
		dynTempQueue();
		~dynTempQueue();

		void enqueue(T );
		void dequeue(T &);
		bool isEmpty();
		void clear();
};
#endif



dynamicQueue.cpp
#include <iostream>
#include <string>
#include "header.h"
using namespace std;

// CONSTRUCTOR
template <class T>
dynTempQueue<T>::dynTempQueue()
{
	front = NULL;
	rear = NULL;
}

// DESTRUCTOR
template <class T>
dynTempQueue<T>::~dynTempQueue()
{
	clear();
}

// ENQUEUE WILL INSERT VALUES TO REAR OF THE QUEUE
template <class T>
void dynTempQueue<T>::enqueue(T uInput)
{
	if(isEmpty())
	{
		front = new qNode(uInput);
		rear = front;
	}
	else
	{
		rear->next = new qNode(uInput);
		rear = rear->next;
	}
}

// DEQUEUE REMOVES THE VALUE AT THE FRONT, AND COPIES INTO VALUE
template <class T>
void dynTempQueue<T>::dequeue(T &uInput)
{
	qNode *temp;
	if(isEmpty())
	{
		cout << "Queue is empty" << endl;
		return;
	}
	else
	{
		num = font->value;
		temp = front;
		front = front->next;
		delete temp;
	}
}

// isEmpty() RETURNS TRUE IF DEQUEUE IS EMPTY, FALSE IF IT ISN'T
template <class T>
bool dynTempQueue<T>::isEmpty()
{
	if(front == NULL)
		return true;
	else
		return false;
}

// DEQUEUE'S ALL ELEMENTS IN THE QUEUE
template <class T>
void dynTempQueue<T>::clear()
{
	T dummy;	// dummy value for deqeue

	while(!isempty())
		deqeue(dummy);
}



I googled the error and it came up with links unrelated to what I am doing.

Is This A Good Question/Topic? 0
  • +

Replies To: Some help on a dynamic queue?

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 03:11 PM

Question for you: what's the difference between a template function and a function?

EDIT: That question above is really unfair. Let me give you an example.

//a.cpp
template<class T>
T addTwo(T a, T B)/>
{
    return a + b;
}

int main()
{
}



//b.cpp
int addTwo(int a, int B)/>
{
    return a + b;
}

int main()
{
}



Assume the compiler doesn't optimize out functions that aren't used. Will the generated code be the same? If not, how will they differ?

This post has been edited by Oler1s: 15 May 2010 - 03:16 PM

Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 03:16 PM

A template function does not have to define the data type, as it is defined by the class instantiation... right? :bigsmile:

In case my terminology is wrong(which I don't doubt) here is an example of what I mean

//template function, EDIT:: or better yet
template <class T>
T class1<T>::findSomething(T value)
{ //stuff, return something }

// Compared to
int class1::findSomething(int value)
{ //stuff, return something }


This post has been edited by IngeniousHax: 15 May 2010 - 03:20 PM

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 03:22 PM

you need to use Explicit Instantiation, its kind of a pain but read this:
http://www.parashift....html#faq-35.13

keep scrolling down to read the solutions to avoid these linker errors. The new c++ update will have a export operator which is supposed to simplify this i guess
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 03:23 PM

Quote

A template function does not have to define the data type, as it is defined by the class instantiation... right?
You're conceptually looking at it wrong, then. Templates are instructions to the compiler. A turing complete language to be precise, so C++ can be thought of consisting of two languages.

It's better than a macro, in that rather than blind copy pastes and preprocessor manipulation, you have a compiler actively evaluating syntax. Still, none of it is handled before runtime. When you write a template function, you provide just that. A template. A piece of data. If you don't give any instructions to the compiler to make use of a template, nothing happens. It's a bit like compiling a define, or something. How is it possible to compile sheer compiler instructions?

When you instantiate a template, you actually instruct the compiler to generate code, based on the template. It's not a magic function that takes any type. It's just templates. And you generate code with these templates. Then this generated code is compiled.

You cannot compile templates for this reason. There is nothing to compile. All templates go into a header file for similar reasons. Every time you instantiate a template definition, that entire template must be visible.
Was This Post Helpful? 0
  • +
  • -

#6 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 03:32 PM

Alright, I read that page... Now if I wanted to implement it into my program, since I am using double, int and string... Would I place all three of those in a separate .cpp file?

Like:
#include "dynamicQueue.cpp"

template void dynTempQueue<int>();
template void dynTempQueue<double>();
template void dynTempQueue<string>();



Or how would that work?

EDIT:: Thanks for the explanation Oler1s :D

This post has been edited by IngeniousHax: 15 May 2010 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 03:32 PM

yeah somethin like that would work

test.h
#ifndef TEST_H
#define TEST_H

template <typename T>
T add(T a, T B)/>;

#endif 



test.cpp
#include "test.h"

template int add<int>(int, int);

template <typename T>
T add(T a, T B)/>
{
	return a+b;
}




main.cpp
#include <iostream>
#include "test.h"

int main() 
{
	std::cout<<add<int>(1,2)<<std::endl;
	return 0;
}



or you could just have all templated code within your .h file

This post has been edited by ImaSexy: 15 May 2010 - 03:43 PM

Was This Post Helpful? 1
  • +
  • -

#8 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 03:41 PM

Thanks ImaSexy, that worked fantastically. I just hate having all kinds of code in one file, it just looks... terrible.

That is really weird that you'd have to do that, you'd imagine they would have had this as part of the compiler to kind of watch out for. Guess it isn't all that bad of a fix though.
Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 03:49 PM

some compilers support a new operator called
export


which solves this i believe, Comeau C++ and Sun Studio are ones that do.

Its weird because Visual Studio reconizes the keyword but they have not given it any functionality

This post has been edited by ImaSexy: 15 May 2010 - 03:50 PM

Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 04:10 PM

export isn't exactly popular because the practicalities of it aren't desirable. You can Google for discussions, but it's something that catches experienced C++ programmers by surprise too.
Was This Post Helpful? 0
  • +
  • -

#11 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 04:32 PM

Well that's weird they would add the keyword, but not add functionality to it. Seems like a bunch of useless code to have to put into something that isn't useable.
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 04:35 PM

Quote

Well that's weird they would add the keyword, but not add functionality to it. Seems like a bunch of useless code to have to put into something that isn't useable.
Before you mock the compiler writers, who are very smart and very knowledgeable, make sure you understand the motivation for their decisions, by reading discussions on export, for example.
Was This Post Helpful? 0
  • +
  • -

#13 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 05:26 PM

Just a side note, I was flaming the compiler writers in any way. I understand that this is not a small feat and should be highly respected, which I do. But having read up on some papers on the keyword 'export' I understand why it is not supported.

It is a lot of hassle for very little pay off as templates can not be distributed in binary, even if export is used. A compiler writer would have to somehow solve the cross compiler dependencies on export, or ship out the source code of the templates, which is obviously not always an option.

So in short, flaming, I was not doing, I just don't understand why they would go through the hassle of implementing the keyword, without giving it some functionality.

This post has been edited by IngeniousHax: 15 May 2010 - 05:26 PM

Was This Post Helpful? 0
  • +
  • -

#14 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 05:33 PM

No idea.

MSDN. Actually, export isn't there in C++0x anymore. You could ask the programmers themselves. Post a question on their blog or something. I'm betting the logic is something like: export is standard C++, so to notify programmers it's not implemented, allow it to be used, but do nothing and notify under a compiler warning setting. Just speculation though.
Was This Post Helpful? 0
  • +
  • -

#15 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Some help on a dynamic queue?

Posted 15 May 2010 - 05:49 PM

Some information I found: "export" keyword of the day
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1