tsgfx 2022-05-10 15:14 采纳率: 83.3%
浏览 55
已结题

使用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

  • 写回答

1条回答 默认 最新

  • 程序喵正在路上 后端领域新星创作者 2022-05-10 15:45
    关注

    题目不是要创建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;
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月18日
  • 已采纳回答 5月10日
  • 创建了问题 5月10日

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧