8 Replies - 4266 Views - Last Post: 07 December 2010 - 04:05 PM Rate Topic: -----

#1 gdernetz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 09

Template Class Selection Sort Help in C++

Posted 06 December 2010 - 09:17 AM

Hello Everybody,

HELP! I tried to write a selection sort program in C++ using the template class that would allow a user to input in whole numbers. I'm horrible at programming and my class is online with not much support to help me understand everything. Can anybody help me out by looking at my full program and the errors I received, and let me know what it is that I am doing wrong, please?


This is the unsortedArrayListType.h file
#pragma once

template <class elemType>
class unsortedArrayListType
{
public:
	void insert(const elemType& insertItem);
	void selectionsort() const;
	void print();
	
	unsortedArrayListType();
	~unsortedArrayListType();

private:
	int loc;
	int length;
	int list;
	int first, last;
	void swap(int, int);
	int minLocation(int, int) const;

};



#include "StdAfx.h"
#include "unsortedArrayListType.h"
#include <iostream>
using namespace std;

template <class elemType>
void unsortedArrayListType<elemType>::insert(const elemType& insertItem)
{
	int loc;

	if (length == 0)	// list is empty
	{
		list(length++) = insertItem;	//insert the item and increment the length
	}
	else
	{
		if (length == maxSize)
		{
			cerr << "Cannot insert in a full list." << endl;
		}
	}
	else
	{
		loc = seqSearch(insertItem);
		if (loc == -1);			// the item to be inserted does not exist in the list
		{
			list[length++] = insertItem;
		}
		else
		{
			cerr << "The item to be inserted has already been entered in the list.  No duplicates"
				 << " are allowed.  Please enter in a different whole number." << endl;
		}
	}
} // end insert

template <class elemType>
void unsortedArrayListType<elemType>::selectionSort()
{
    int minIndex;

    for (int loc = 0; loc < length; loc++)
    {
        minIndex = minLocation(loc, length - 1);
        swap(loc, minIndex);
    }
} // end selectionSort

template <class elemType>
void unsortedArrayListType<elemType>::print()             // to print out the sorted numbers
{
	for (int loc = 0; loc < 10; loc++)
	{
		cout << list[loc] << endl;
	}
}

template <class elemType>
unsortedArrayListType<elemType>::unsortedArrayListType()
{
	for (int loc = 0; loc < 10; loc++)
	{
		list[loc] = empty;
	}
} // end constructor

template <class elemType>
unsortedArrayListType::~unsortedArrayListType()
{
	delete list[];
} // end deconstructor

template <class elemType>
void unsortedArrayListType<elemType>::swap(int first, int second)
{
	elemType temp;

	temp = list[first];
	list[first] = list[second];
	list[second] = temp;
} // end swap

template <class elemType>
int unsortedArrayListType<elemType>::minLocation(int first, int last)
{
    int minIndex;

    minIndex = first;

    for (loc = first + 1; loc <= last; loc++)
        if (list[loc] < list[minIndex])
            minIndex = loc;

    return minIndex;
} // end minLocation



#include "stdafx.h"
#include "unsortedArrayListType.h"
#include <iostream>
using namespace std;

int main()
{
	unsortedArrayListType<int> list;
	int num;

	cout << "Enter in 15 whole numbers. Then type in -999 to end your list." << endl;

	cin >> num;

	while (num != -999)
	{
		list.insert(num);
		cin >> num;
	}
	
	cout << "The list of whole numbers before sorting : " << endl;
	list.print();
	cout << endl;

	cout << "The list of whole numbers after sorting: " << endl;
	list.print();
	cout << endl;
	    
    system("PAUSE");
    return 0;
}


ERRORS WHEN I TRY TO RUN THE PROGRAM
1>------ Build started: Project: Final_Project_2010_GailDernetz, Configuration: Debug Win32 ------
1>  unsortedArrayListType.cpp
1>c:\users\gail\documents\visual studio 2010\projects\final_project_2010_gaildernetz\final_project_2010_gaildernetz\unsortedarraylisttype.cpp(47): error C2039: 'selectionSort' : is not a member of 'unsortedArrayListType<elemType>'
1>c:\users\gail\documents\visual studio 2010\projects\final_project_2010_gaildernetz\final_project_2010_gaildernetz\unsortedarraylisttype.cpp(71): error C2244: 'unsortedArrayListType<elemType>::~unsortedArrayListType' : unable to match function definition to an existing declaration
1>          c:\users\gail\documents\visual studio 2010\projects\final_project_2010_gaildernetz\final_project_2010_gaildernetz\unsortedarraylisttype.h(12) : see declaration of 'unsortedArrayListType<elemType>::~unsortedArrayListType'
1>          definition
1>          'unsortedArrayListType::~unsortedArrayListType(void)'
1>          existing declarations
1>          'unsortedArrayListType<elemType>::~unsortedArrayListType(void)'
1>c:\users\gail\documents\visual studio 2010\projects\final_project_2010_gaildernetz\final_project_2010_gaildernetz\unsortedarraylisttype.cpp(95): error C2244: 'unsortedArrayListType<elemType>::minLocation' : unable to match function definition to an existing declaration
1>          c:\users\gail\documents\visual studio 2010\projects\final_project_2010_gaildernetz\final_project_2010_gaildernetz\unsortedarraylisttype.h(20) : see declaration of 'unsortedArrayListType<elemType>::minLocation'
1>          definition
1>          'int unsortedArrayListType<elemType>::minLocation(int,int)'
1>          existing declarations
1>          'int unsortedArrayListType<elemType>::minLocation(int,int) const'
1>  Final_Project_2010_GailDernetz.cpp
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 06 December 2010 - 11:24 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Template Class Selection Sort Help in C++

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 285
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Template Class Selection Sort Help in C++

Posted 06 December 2010 - 09:43 AM

Just a quick browse over the code made me realise that you have made some simple errors that first need to be fixed, be careful with your declarations within the class, for example you have said
void selectionsort() const;

// then in definition //
template <class elemType>
void unsortedArrayListType<elemType>::selectionSort() {
  // .. code
} 



notice that you need to either change the definition to void selectionSort() const and also add the const to the impementation.
template <class elemType>
void unsortedArrayListType<elemType>::selectionSort() const {

}


When you notice an compiler error stating something is not a member of something else, usually it is a typo of the function name or it can be errors concerning the parameter list.
Was This Post Helpful? 1
  • +
  • -

#3 gdernetz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 09

Re: Template Class Selection Sort Help in C++

Posted 06 December 2010 - 09:52 AM

Thanks so much! That was a stupid mistake. I also have an issue with my deconstructor. Can you guide me in the right direction?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Template Class Selection Sort Help in C++

Posted 06 December 2010 - 10:20 AM

"I have an issue with my deconstructor" is insufficient information. Please provide the errors or problems you are having exactly as they appear.

Also note, it's not a "deconstructor" it's a "destructor". Knowing that could be helpful in searching for help on Google.
Was This Post Helpful? 0
  • +
  • -

#5 gdernetz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 09

Re: Template Class Selection Sort Help in C++

Posted 06 December 2010 - 10:26 AM

Sorry about that...duh. It should be destructor.

I tried to redo my destructor:
template <class elemType>
unsortedArrayListType::~unsortedArrayListType()
{
	delete [] list;
} // end destructor



and received these errors:
c:\users\..\documents\visual studio 2010\projects\..\..\unsortedarraylisttype.cpp(68): error C2955: 'unsortedArrayListType' : use of class template requires template argument list
1>          c:\users\..\documents\visual studio 2010\projects\..\..\unsortedarraylisttype.h(5) : see declaration of 'unsortedArrayListType'
1>c:\users\..\documents\visual studio 2010\projects\..\..\unsortedarraylisttype.cpp(71): error C2244: 'unsortedArrayListType<elemType>::~unsortedArrayListType' : unable to match function definition to an existing declaration
1>          c:\users\..\documents\visual studio 2010\projects\..\..\unsortedarraylisttype.h(12) : see declaration of 'unsortedArrayListType<elemType>::~unsortedArrayListType'
1>          definition
1>          'unsortedArrayListType::~unsortedArrayListType(void)'
1>          existing declarations
1>          'unsortedArrayListType<elemType>::~unsortedArrayListType(void)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This post has been edited by JackOfAllTrades: 06 December 2010 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

Re: Template Class Selection Sort Help in C++

Posted 06 December 2010 - 10:57 AM

template <class elemType>
class unsortedArrayListType {
public:
	void insert(const elemType& insertItem); // ok
	void selectionsort() const; // clearly, a sort can't be const
	void print(); // ok, a print can be const, btw
	
	unsortedArrayListType(); // ok
	~unsortedArrayListType(); // ok

private:
	// why are any of these object variables???
	int loc;
	int length;
	int list;
	int first, last;
	// where exactly are your storing data??

	void swap(int, int);
	int minLocation(int, int) const;

};

template <class elemType>
unsortedArrayListType<elemType>::unsortedArrayListType() {
	// 10?  why 10
	for (int loc = 0; loc < 10; loc++) {
		// list is defined as int
		// how can this array thing possibly work?
		list[loc] = empty; // empty is defined where?
	}
}



Thats really just the beginning, I'm afraid. I suspect this has been cobbled together from various sources with no real understanding how any of them work.
Was This Post Helpful? 0
  • +
  • -

#7 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 285
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Template Class Selection Sort Help in C++

Posted 06 December 2010 - 11:06 PM

it probably has, for the destructor i believe the error is caused by not including <elemType> in the implementation...

eg you have unsortedArrayListType::~unsortedArrayListType()

you should have
unsortedArrayListType<elemType>::~unsortedArrayListType()   {
   delete [] list;
}


goodluck
Was This Post Helpful? 0
  • +
  • -

#8 gdernetz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 09

Re: Template Class Selection Sort Help in C++

Posted 07 December 2010 - 08:16 AM

Thanks DaneAU! I'm made changes to my program since I first posted it. It does include the <elemType> in the destructor now. I've also took the suggestions of the others that have posted replies to me. I can re-post the whole program again if necessary; however, I will post the errors I now receive. Maybe you can give me some insight as to what I am missing. Like I said in the beginning, I'm horrible at programming, although I'm getting it somewhat.

ERRORS I AM RECEIVING NOW:
>....obj : error LNK2019: unresolved external symbol "public: __thiscall unsortedArrayListType<int>::~unsortedArrayListType<int>(void)" (??1?$unsortedArrayListType@H@@QAE@XZ) referenced in function _main
1>....obj : error LNK2019: unresolved external symbol "public: void __thiscall unsortedArrayListType<int>::print(void)const " (?print@?$unsortedArrayListType@H@@QBEXXZ) referenced in function _main
1>....obj : error LNK2019: unresolved external symbol "public: void __thiscall unsortedArrayListType<int>::insert(int const &)" (?insert@?$unsortedArrayListType@H@@QAEXABH@Z) referenced in function _main
1>....obj : error LNK2019: unresolved external symbol "public: __thiscall unsortedArrayListType<int>::unsortedArrayListType<int>(void)" (??0?$unsortedArrayListType@H@@QAE@XZ) referenced in function _main
1>C:\Users\...\documents\visual studio 2010\Projects\...\Debug\....exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

HELP!!!
Was This Post Helpful? 0
  • +
  • -

#9 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 285
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Template Class Selection Sort Help in C++

Posted 07 December 2010 - 04:05 PM

For templates you should include the class and implementations in the .h file, meaning there should not be a .h with a .cpp file, this applies for templates.


eg, Foo.h
template <class elemType>
class Foo {
  void Bar();

};

template <class elemType>
void Foo<elemType>::Bar() {
}


This post has been edited by DaneAU: 07 December 2010 - 04:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1