重新定义int main()c ++

时间:2016-01-11 20:15:53

标签: c++ redefinition

编译器不断给出重新定义int main()的错误。我不知道问题出在哪里。 MAX_CHAR也有问题。它的写入MAX_CHARS'未在此范围内声明。有什么建议,评论??

#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 个答案:

答案 0 :(得分:3)

你不能像现在这样拥有2个main()。

你做了两次。

int main()
{
    cout << "Insertion sort algorithm driver program" << endl;

和InsertionSort.h

int main()
{
    int sizee=10;
    int v[sizee];

答案 1 :(得分:3)

您的程序中只能有一个main()函数。你有两个。第一个代码段中有一个,InsertionSort.h

中有一个

#include "insertionSort.h"中你还有InsertionSort.h你不应该做的事。文件不应包含在内。