0

I'm making a header file to make, sort, and print a vector using heapsort. I keep getting a compiler error about my const char[100] array. Probably something really simple I'm missing...I hope.

In function `void build_list(std::vector<T, std::allocator<_CharT> >&, const char*)':
16 expected primary-expression before "const"
16 expected `;' before "const"

#ifndef HEAPSORT_H
#define HEAPSORT_H

#include <iostream>
#include <vector>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <algorithm>

using namespace std;

template <class T>
void build_list(vector<T>& itemList, const char* fileName)
{
    const char[100] file = fileName;
    ifstream inFile;
	inFile.open("file");

	if(!inFile)
	{
       cout << "input file did not open";
       cin.get();
       exit(0);
	}
	
	T item;
    while(inFile >> item)
	{
	  itemList.push_back(item);
    }
    
    inFile.close();
}


template <class T>
void print_list(const vector<T>& itemList, int itemWidth, int numPerLine)
{
     int counter = 0;
     //for(int i = itemList.begin(); i != itemList.end(); ++i)
     for(int i = 0; i != itemList.size(); ++i)
     {
        cout << setw(itemWidth) << itemList.at(i);
        if(counter == numPerLine) 
        {
           cout << endl;
           counter = 0;
        }
        counter++;
     }
}
     

template <class T, class Pred>
void sort_list(vector<T>& itemList, Pred predicate)
{
     sort_heap(itemList.begin(), itemList.end());
}

#endif

Edited by railmaster7: n/a

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Dave Sinkula
0

good call. I had one other question. in

template <class T, class Pred>
void sort_list(vector<T>& itemList, Pred predicate)
{
sort_heap(itemList.begin(), itemList.end());
}

I need to use the Pred predicate to determine whether to use the heapsort in ascending or descending order and I'm not sure how to do that.

The description of the argument is : The second argument will be a predicate (a Boolean function) from the STL. This predicate will either be less<T>() (to build a heap that will sort items in ascending order) or greater<T>() (to build a heap that will sort items in descending order).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.