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

Recommended Answers

All 3 Replies

I don't think you even want the array, but instead are after this?

inFile.open(file);

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.