Template errors?

My load function isnt working. I've tested it and returned it as an int and it worked but when i tried to compile when i changed it to a template i started getting the errors and I'm not sure how to fix it:


1
2
3
4
5
6
7
all_sort.cpp:41:15: error: no matching function for call to 'load'
        int *value = load("./integers.txt", size);
                     ^~~~
./bubble_sort.h:44:4: note: candidate template ignored: couldn't infer template
      argument 'T'
T *load(const char* x, int &size) {
   ^ 


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
#ifndef BUBBLE_SORT_H
#define BUBBLE_SORT_H

#include <fstream>
#include <iostream>

template <typename T>
void swap(T &a, T &b);

template <typename T>
T *load(const char* x, int& size);

//int* load (int x, int&);

template <typename T>
void bubbleSort(T arr[], int n) {
    bool isSorted = false;
	for (int last = n-1; last > 0 && !isSorted; last--) {
		isSorted = true;
		for (int i = 0; i < last; i++)
			if (arr[i] > arr[i+1]) {
				swap(arr[i], arr[i+1]);
				isSorted = false;
			}
	}
}


template <typename T>
void print(T arr[], int n) {
    for (int i = 0; i < n; i ++)
        std::cout << arr[i] << " ";
    std::cout << std::endl;
}

template <typename T>
void swap(T &a, T &b) {
	T t = a;
	a = b;
	b = t;
}

template <typename T>
T *load(const char* x, int &size) {
    
    std::ifstream infile;
    infile.open(x);
    T c;
    
    infile >> c;
    size++;
    //std::cout << c << std::endl;
    
    T *a = new T[10];
    int i = 0;
    
    while(infile) {
        a[i] = c;
        std::cout << a[i] << " ";
        i++;
        size++;
        infile >> c;
    }
    std::cout << "Size " << size << std::endl;
    std::cout << std::endl;
    
    infile.close();
    
    return a;
}

#endif 


I'm trying to use my load function to load integers from a file into and array and then return it back.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "bubble_sort.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main(){

	int n, pick = 1, size = 0;
	n = 10;

	cout << "Bubble Sort: " << endl;

	int *value = load("./integers.txt", size);

	return 0;
}


The diagnostic is quite clear about what is wrong, isn't it?

candidate template ignored: couldn't infer template argument 'T'

1
2
// int *value = load("./integers.txt", size);
int *value = load<int>("./integers.txt", size);


Use std::vector<>
http://www.mochima.com/tutorials/vectors.html
Topic archived. No new replies allowed.