收到有关默认构造函数的错误,我不知道为什么 - C++

发布于 2024-11-09 21:28:53 字数 1767 浏览 2 评论 0原文

我只是编写一个简单的 C++ 程序进行练习,但我不断收到相同的错误,即我没有默认构造函数。但是,我尝试显式声明默认构造函数,但仍然收到完全相同的错误。我不明白问题是什么。这是我从 GNU C++ 编译器收到的错误:

sorting.cpp: In function 'int main()':
sorting.cpp:5:错误:没有匹配的函数可用于调用“Time::Time()”
sorting.h:12:注意:候选者是:Time::Time(int, int)
sorting.h:7: 注意:Time::Time(const Time&)

这是代码: sorting.cpp

#include "sorting.h"

int main()
{
    Time* courses = new Time[3];
    courses[1].setTime(9,30);
    courses[2].setTime(10,30);
    courses[3].setTime(12,0);
    Course this_year(3, courses);
    this_year.printTimes();
    delete [] courses;

    return 0;
}

sorting.h
#include <iostream>

using namespace std;

class Time
{
private:
        int                 hours;
        int                 minutes;
public:
                    Time();
        Time(int h = 0, int m = 0) ;
        void                setTime(int h, int m) ;
        void                printTime();
};

class Course
{
private:
        int                 period_count;
        Time*               periods;
public:
        Course(int count, ...);
        void                printTimes();
};

Time::()
{
};

Time::Time(int h, int m)
{
    hours = h;
    minutes = m;
};

void Time::setTime(int h, int m)
{
    hours = h;
    minutes = m;
    return;
};

void Time::printTime()
{
    cout << hours << ":" << minutes;
    return;
}

Course::Course(int count, Time* the_times)
{
    period_count = count;
    for (int i = 0; i < count; ++i)
    {
            periods[i] = the_times[i];
    }
}

void Course::printTimes()
{
    for(int i = 0; i < count; ++i)
    {
            periods[i].printTime();
    }
    return;
}

I was just making a simple C++ program for practice, and I keep getting the same error, which is saying that I don't have a default constructor. However, I have tried explicitly declaring a default constructor, and I still get the exact same error. I don't understand what the problem is. Here is the error I am getting from GNU C++ Compiler:

sorting.cpp: In function ‘int main()’:
sorting.cpp:5: error: no matching function for call to ‘Time::Time()’
sorting.h:12: note: candidates are: Time::Time(int, int)
sorting.h:7: note: Time::Time(const Time&)

And here is the code:
sorting.cpp

#include "sorting.h"

int main()
{
    Time* courses = new Time[3];
    courses[1].setTime(9,30);
    courses[2].setTime(10,30);
    courses[3].setTime(12,0);
    Course this_year(3, courses);
    this_year.printTimes();
    delete [] courses;

    return 0;
}

sorting.h:
#include <iostream>

using namespace std;

class Time
{
private:
        int                 hours;
        int                 minutes;
public:
                    Time();
        Time(int h = 0, int m = 0) ;
        void                setTime(int h, int m) ;
        void                printTime();
};

class Course
{
private:
        int                 period_count;
        Time*               periods;
public:
        Course(int count, ...);
        void                printTimes();
};

Time::()
{
};

Time::Time(int h, int m)
{
    hours = h;
    minutes = m;
};

void Time::setTime(int h, int m)
{
    hours = h;
    minutes = m;
    return;
};

void Time::printTime()
{
    cout << hours << ":" << minutes;
    return;
}

Course::Course(int count, Time* the_times)
{
    period_count = count;
    for (int i = 0; i < count; ++i)
    {
            periods[i] = the_times[i];
    }
}

void Course::printTimes()
{
    for(int i = 0; i < count; ++i)
    {
            periods[i].printTime();
    }
    return;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遇到 2024-11-16 21:28:53

Time* course = new Time[3]; 创建一个包含 3 个 Time 对象的数组 - 这要求 Time 有一个默认构造函数(因为在创建数组时需要构造对象)。

如果您不想为 Time 创建默认构造函数,请考虑使用 std::vector,并一次插入一个必要的对象到向量中。

Time* courses = new Time[3]; creates an array of 3 Time objects - which requires Time to have a default constructor (as the objects need to be constructed when the array is created).

If you don't want to create a default constructor for Time consider using std::vector, and insert the necessary objects one at a time into the vector.

四叶草在未来唯美盛开 2024-11-16 21:28:53

看来,编译器无法将您的 Time(int, int) 构造函数识别为默认构造函数(使用 new Time[3] 时需要),尽管 int 具有默认参数。只需尝试向 Time 添加一个真正的默认构造函数即可。

It seems, The compiler cannot identify your Time(int, int) constructor as a default constructor (that is needed when using new Time[3]), although the ints have default arguments. Just try adding a real default constructor to Time.

错々过的事 2024-11-16 21:28:53

我有两个错误,都与你的不同。

使用 g++ 4.4.5,变量 count 在 Course::printTimes() 中未定义,并且 Course 构造函数的原型 ... 与声明不匹配。不过,Time() 构造函数没有问题。

I got two errors, both different from yours.

Using g++ 4.4.5, the variable count was undefined in Course::printTimes(), and the prototype for the Course constructor's ... didn't match the declaration. No problem with the Time() constructor, tho.

万人眼中万个我 2024-11-16 21:28:53
Time:() 
{
};

应该是

Time::Time() 
 : hours(0), minutes(0)
{
}

或同等的

Time:() 
{
};

should be

Time::Time() 
 : hours(0), minutes(0)
{
}

or some equivalent

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文