使用ctime库分文件编写后报错?

题目为:

img

img

.h文件


#ifndef TIME_H
#define TIME_H
class Time
{
    public:
        stopWatch();
        void start();
        void stop();
        int getElapseTime();

    private:
        int startTime;
        int endTime;
};

#endif

.cpp文件

#include "Time.h"
#include <ctime>
#include <cstdlib>

Time::stopWatch()
{
    startTime = time(0);
}
void Time::start()
{
    startTime = time(0);
}
void Time::stop()
{
    endTime = time(0);
}
int Time::getElapseTime()
{
    return startTime - endTime;
}

测试文件.cpp

#include <iostream>
#include "Time.h"
using namespace std;
void selectionSort(double list[], int listSize)
{
    for(int i = 0; i < listSize; i++)
    {
        double currentMin = list[i];
        int currentMinIndex = i;
        for(int j = i + 1; j < listSize; j++)
        {
            if(currentMin > list[j])
            {
                currentMin = list[j];
                currentMinIndex = j;
            }
        }
        if(currentMinIndex != i)
        {
            list[currentMinIndex] = list[i];
            list[i] = currentMin;
        }
    }

}
int main()
{
    const int s = 100000;
    int list[s];
    for(int i = 1; i < s; i++)
    {
        list[i] = rand() % s;
    }
    stopWatch watch;
    watch.start();
    selectionSort(list, s);
    watch.stop();
    cout << "Time is " << watch.getElapasedTime() << endl;
    return 0;
}


编译并运行后弹出了ctime库源代码,并报错

#pragma GCC system_header

#include <bits/c++config.h>
#include <time.h>

#ifndef _GLIBCXX_CTIME
#define _GLIBCXX_CTIME 1

// Get rid of those macros defined in <time.h> in lieu of real functions.
#undef clock
#undef difftime
#undef mktime
#undef time
#undef asctime
#undef ctime
#undef gmtime
#undef localtime
#undef strftime

namespace std
{
  using ::clock_t;
  using ::time_t;
  using ::tm;

  using ::clock;
  using ::difftime;
  using ::mktime;
  using ::time;
  using ::asctime;
  using ::ctime;
  using ::gmtime;
  using ::localtime;
  using ::strftime;
} // namespace

#endif


img

题目不是要创建Time类啊,是StopWatch.h

#ifndef TIME_H
#define TIME_H
class StopWatch
{
private:
    int startTime;
    int endTime;
public:
    StopWatch();
    void start();
    void stop();
    int getElapseTime();
};
#endif

StopWatch.cpp

#include "StopWatch.h"
#include <ctime>
#include <cstdlib>

StopWatch::StopWatch()
{
    startTime = time(0);
}
void StopWatch::start()
{
    startTime = time(0);
}
void StopWatch::stop()
{
    endTime = time(0);
}
int StopWatch::getElapseTime()
{
    return startTime - endTime;
}

测试程序

#include <iostream>
#include "StopWatch.h"
using namespace std;

void selectionSort(double list[], int listSize)
{
    for (int i = 0; i < listSize; i++)
    {
        double currentMin = list[i];
        int currentMinIndex = i;
        for (int j = i + 1; j < listSize; j++)
        {
            if (currentMin > list[j])
            {
                currentMin = list[j];
                currentMinIndex = j;
            }
        }
        if (currentMinIndex != i)
        {
            list[currentMinIndex] = list[i];
            list[i] = currentMin;
        }
    }

}
int main()
{
    const int s = 100000;
    double list[s];
    for (int i = 1; i < s; i++)
    {
        list[i] = rand() % s;
    }
    StopWatch watch;
    watch.start();
    selectionSort(list, s);
    watch.stop();
    cout << "Time is " << watch.getElapseTime() << endl;
    return 0;
}

我把报错都改正了
有帮助的话希望采纳一下