編譯器不斷給int main()重定義錯誤。我不知道問題在哪裏。 MAX_CHAR也有問題。它正在寫入MAX_CHARS'未在此範圍內聲明。任何建議,評論?重新定義int main()C++
#include <iostream> // cin cout endl
#include <fstream> // ifstream
#include <sstream> // stringstream
#include <stdlib.h> //exit
#include "insertionSort.h"
#include <vector>
#include <climits>
using std::cerr;
using std::cout;
using std::endl;
using std::ifstream;
const int MAX_CHAR = 500; // max length of each line to read from the input file
template<class T>
void readSortOutput(char* typeName, vector<T> v, ifstream &inStream);
int main()
{
cout << "Insertion sort algorithm driver program" << endl;
ifstream inStream("/home/Downloads/input.txt");
if(inStream.fail())
{
cerr << "Input file opening failed.\n";
exit(1);
}
vector<int> intVector;
readSortOutput((char*)"int", intVector, inStream);
vector<double> dblVector;
readSortOutput((char*)"double", dblVector, inStream);
inStream.close();
return 0;
}
template<class T>
void insertionSort(vector<T>& data)
{
for (size_t i = 0; i < data.size(); i++)
for (size_t j = i; j < data.size(); j++)
if (data[ j ] < data[ i ])
{ // swap values
T temp = data[ j ];
data[ j ] = data[ i ];
data[ i ] = temp;
}
return;
}
template<class T>
void readSortOutput(char* typeName, vector<T> v, ifstream &inStream)
{
char fileLine[MAX_CHARS];
std::stringstream ss;
inStream.getline(fileLine, MAX_CHARS);
ss << fileLine;
T elem;
while (ss >> elem) {
v.push_back(elem);
}
cout << endl << typeName << " vector before insertion sort: " << endl;
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
insertionSort(v); // the sort itself
cout << typeName << " vector after insertion sort: " << endl;
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
return;
}
InsertionSort.h
#ifndef INSERTIONSORT_H
#define INSERTIONSORT_H
#include <iostream>
#include <iostream> // cin cout endl
#include <fstream> // ifstream
#include <sstream> // stringstream
#include <stdlib.h> //exit
#include "insertionSort.h"
#include <vector>
#include <climits>
using namespace std;
int main()
{
int sizee=10;
int v[sizee];
for (int i=0;i<sizee;i++){
cout<<"Sorting array: ";
cin>>v[i];
}
int i,j,val;
for (i=1; i<sizee; i++) {
val=v[i];
j = i-1;
while (j>=0 && v[j]>val) {
v[j+1] = v[j];
j--;
}
v[j+1] = val;
}
for (int i=0;i<sizee;i++){
cout<<"v["<<i<<"]="<<v[i]<<endl;
}
return 0;
}
爲什麼你有2條主線? –
有頭文件。 (InsertionSort.h) –