收到有关默认构造函数的错误,我不知道为什么 - C++
我只是编写一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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 usingstd::vector
, and insert the necessary objects one at a time into the vector.看来,编译器无法将您的
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 usingnew Time[3]
), although the ints have default arguments. Just try adding a real default constructor toTime
.我有两个错误,都与你的不同。
使用 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.
应该是
或同等的
should be
or some equivalent