Simple C++ Program

Pages: 12345
Not sure what i need to put in for the insertionSort (???)
insertionSort(v); insetead of insertionSort(fin,v); because we got rid of the infile stuff
im getting a error when i go to run it

error:

>P8220.obj : error LNK2019: unresolved external symbol "void __cdecl insertionSort<double>(class std::vector<double,class std::allocator<double> > &)" (??$insertionSort@N@@YAXAAV?$vector@NV?$allocator@N@std@@@std@@@Z) referenced in function _main

it does compile though. i commented all the median stuff out.
1
2
3
4
5
6
7
8
9
insertionSort(v);
cout << "Sorted values: ";
  
   vector<double>::iterator k;
   for (k = v.begin(); k != v.end(); k++)
   {
	   cout << *k << ",";
   }
   cout << endl;
post all of the code. I want to try in my compiler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

// Vector and median
// December 3rd 2009

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

//template <typename T>
//double median(const vector<T> & v) ;

template <typename T>
void insertionSort(vector<T>& a_values);

template <typename T>
void fill_vector(ifstream& in_file, vector<T> & v) 
{

	T a_value;
	while (!in_file.eof()) 
	{
		in_file >> a_value;
		v.push_back(a_value);
	}
}

int main() // begin main
{
	vector<double> v;
	char filename[16];
	//double m = median(v);



	cout << "Enter a file name: ";
	cin >> filename;
	cout << endl;
	
	//double is = insertionSort(v);
	
	ifstream fin(filename);
	if (!fin) 
	{
		cerr << "Could not open the file" << endl;
		return 1;
	}

	fill_vector(fin,v);
	cout << "Values entered: ";

	vector<double>::iterator p;
	for (p = v.begin(); p != v.end(); p++) 
	{
		cout << *p << ",";
	}
	cout << endl;


insertionSort(v);
cout << "Sorted values: ";
  
   vector<double>::iterator k;
   for (k = v.begin(); k != v.end(); k++)
   {
	   cout << *k << ",";
   }
   cout << endl;
	
	//cout << "Median = " << m << endl;

} //end main




template <typename T>
void insertionSort(ifstream& in_file, vector<T>& a_value) //vector is used as an implementation of a List
{
	int i, j, n = v.size(); //i is the pseudo-Code Index and j is the scanIndex
	T temp;
	// Select List[Index] as element to be inserted into sorted sublist.
	// place v[i] into the sublistv[0] ... v[i-1], 1 <= i < n,
	// so it is in the correct position
	for (i = 1; i < n; i++)
	{
		// index j scans down list from v[i] looking for correct position to locate temp assigns it to v[j]
		j = i;
		temp = a_value[ i];
		// find where to insert in the sorted sublistand shift elements to right while scanning
		//from right-to-left
		while ( j > 0 && target < a_value[ j-1 ])
		{
			// shift elements up list to make room for insertion
			a_value[ j ] = a_value[ j-1];
			j--;
		}
		// the location is found; insert temp
		a_value[j] = temp;
	}
	
}

//template <typename T>
//double median(const vector<T> & v)
//{
//	double product = 0;
/*

	if( v.size() % 2 )
	{
		product = v * (v.size() / 2); 
	}
	else
	{
		product = (product = v * (v.size() / 2) + (v.size() / 2) ) / 2;
	}

	
	return product;
}*/
I'm just guessing at this point haha... dont really have any idea what that extneral void error is coming from.
im looking at it right now.
Ok, thanks.
what is targe supposed to be in this code

1
2
3
4
5
6
7
while ( j > 0 && target < a_value[ j-1 ])
		{
			// shift elements up list to make room for insertion
			a_value[ j ] = a_value[ j-1];
			j--;
		}

Its never declared anywhere in your function
hmm let me go over it.
I'm not sure. I think its supposed to be a temp. Do you have much experience with Insertion Sort?
haha, thanks =P
lol i thought that would be great. but yea an insertion sort takes for example a vector.

Vectors start out empty. and it gets the name insertionSort because as you insert a number via a file or just inserting it from main the function will place that number in a sorted fashion automatically.

http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Sorting/insertionSort.htm
thats a good link.

i think you can change the function back to use the file as a paramater because i think that is what you are supposed to do. read in a number from the file and put the numbers into a vector. but make sure you put the number in the correct spot etc.
its really up to you though if you wanna read out of the file and sort them as you go or you can already have the vector and sort it. but either way take a look at the youtube video i posted. it is a very good explanation and it helps to visualize it. after that look at the link 2 posts up it has an example of an array. then after that make yours do the same but just for a vector.
so im now using a bubble sort and im still getting the linker error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
template <typenameT>
void bubbleSort(vector<T>& v)
{
inti,j, n = v.size(); // index of last exchange
boolexchangeOccurs= true;
T temp;
// iis the index of last element in the current sublist
i= n-1;
// continue the process until we make no exchanges or
// we have made n-1 passes
while (i> 0 && exchangeOccurs)
{
// assume no exchange occurs
exchangeOccurs= false;
// scan the sublistv[0] to v[i]
for (j = 0; j < i; j++)
// exchange a pair and assign true to exchangeOccurs
if (v[j+1] < v[j])
{
temp = v[j];
v[j] = v[j+1];
v[j+1] = temp;
exchangeOccurs= true;
}
// move idownward one element
i--;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

//template <typename T>
//double median(const vector<T> & v) ;

template <typename T>
void insertionSort(vector<T>& a_values);

template <typename T>
void fill_vector(ifstream& in_file, vector<T> & v) 
{

	T a_value;
	while (!in_file.eof()) 
	{
		in_file >> a_value;
		v.push_back(a_value);
	}
}

int main() // begin main
{
	vector<double> v;
	char filename[16];
	//double m = median(v);



	cout << "Enter a file name: ";
	cin >> filename;
	cout << endl;

	//double is = insertionSort(v);

	ifstream fin(filename);
	if (!fin) 
	{
		cerr << "Could not open the file" << endl;
		return 1;
	}

	fill_vector(fin,v);
	cout << "Values entered: ";

	vector<double>::iterator p;
	for (p = v.begin(); p != v.end(); p++) 
	{
		cout << *p << ",";
	}
	cout << endl;

	insertionSort(v);
	cout << "Sorted values: ";

	vector<double>::iterator k;
	for (k = v.begin(); k != v.end(); k++)
	{
		cout << *k << ",";
	}
	cout << endl;

	//cout << "Median = " << m << endl;

} //end main




template <typename T>
void bubbleSort(vector<T>& v)
{
inti,j, n = v.size(); // index of last exchange
boolexchangeOccurs= true;
T temp;
// iis the index of last element in the current sublist
i= n-1;
// continue the process until we make no exchanges or
// we have made n-1 passes
while (i> 0 && exchangeOccurs)
{
// assume no exchange occurs
exchangeOccurs= false;
// scan the sublistv[0] to v[i]
for (j = 0; j < i; j++)
// exchange a pair and assign true to exchangeOccurs
if (v[j+1] < v[j])
{
temp = v[j];
v[j] = v[j+1];
v[j+1] = temp;
exchangeOccurs= true;
}
// move idownward one element
i--;
}
}


//template <typename T>
//double median(const vector<T> & v)
//{
//	double product = 0;
/*

if( v.size() % 2 )
{
product = v * (v.size() / 2); 
}
	else
	{
		product = (product = v * (v.size() / 2) + (v.size() / 2) ) / 2;
	}

	
	return product;
}*/





Could it possibly be something else other then the actual sorting function?
I fixed the linker error on my compiler by putting the whole template before main. You should try that
Pages: 12345