构造函数或代码有问题?

huangapple go评论73阅读模式
英文:

Is the constructor or the code the problem?

问题

以下是代码的翻译部分:

我是C++学习的新手。我尝试创建一个代码,用构造函数来告诉时间并添加分钟。然而,在使用CodeBlocks构建代码时,出现了“time::time()”作用域分辨错误。

我尝试在构造函数内创建参数,但没有成功。

#include <iostream>
using namespace std;
class time
{
private:
    int hours, minutes;
public:
    time();
    ~time();
    void enterTime();
    void addMinutes();
    void displayTime();
};
time::time()
{
    int timehours;
    int timeminutes;
    cout << "输入小时: " << endl;
    cin >> timehours;
    if (timehours < 0 && timehours > 23)
    {
        cout << "时间输入无效。请重试。" << endl;
        time();
    }
    else
        timehours == 0;
    cout << "输入分钟: " << endl;
    cin >> timeminutes;
    if (timeminutes < 0 && timeminutes > 59)
    {
        cout << "时间输入无效。请重试。" << endl;
        time();
    }
    else
        timeminutes == 0;
    cout << "初始时间: " << timehours << ": " << timeminutes << endl;
}
void time::enterTime()
{
    cout << "输入小时: " << endl;
    cin >> hours;
    if (hours < 0 && hours > 23)
    {
        cout << "时间输入无效。请重试。" << endl;
        enterTime();
    }
    cout << "输入分钟: " << endl;
    cin >> minutes;
    if (minutes < 0)
    {
        cout << "时间输入无效。请重试。" << endl;
        enterTime();
    }
    cout << "初始时间: " << hours << ": " << minutes << endl;
}
void time::addMinutes()
{
    if (minutes > 60 && minutes % 60 > 0)
    {
        int addhours, addMin;
        addMin = (minutes % 60 > 0);
        addhours = (minutes / 60);
        hours += addhours;
        if (hours >= 24)
            hours -= 24;
    }
}
void time::displayTime()
{
    cout << "更新后的时间:" << hours << " : " << minutes << " " << endl;
}
int main()
{
    int n;
    for (n = 0; n < 4; n++)
    {
        time Time[4];
        if ((n + 1) % 2 == 0)
        {
            Time[n].time();
            Time[n].addMinutes();
            Time[n].displayTime();
        }
        else
        {
            Time[n].enterTime();
            Time[n].addMinutes();
            Time[n].displayTime();
        }
    }
    return 0;
}

希望这可以帮助你理解代码。如果有其他问题,请随时提出。

英文:

I am new to learning C++. I tried to create a code that is meant to tell time and add minutes to it by using a constructor. However, upon building the code with CodeBlocks, an error is displayed under the scope resolution 'time::time()'.

I tried creating parameters within the constructor but to no avail.

#include&lt;iostream&gt;
using namespace std;
class time
{
private:
int hours,minutes;
public:
time(int);
~time();
void enterTime();
void addMinutes();
void displayTime();
};
time::time()
{
int timehours;
int timeminutes;
cout&lt;&lt;&quot;Enter hours: &quot;&lt;&lt;endl;
cin&gt;&gt;timehours;
if(timehours&lt;00 &amp;&amp; timehours&gt;23)
{
cout&lt;&lt;&quot;Invalid Entry of time. Try again.&quot;&lt;&lt;endl;
time();
}
else
timehours==00;
cout&lt;&lt;&quot;Enter minutes: &quot;&lt;&lt;endl;
cin&gt;&gt;timeminutes;
if(timeminutes&lt;0 &amp;&amp; timeminutes&gt;59)
{
cout&lt;&lt;&quot;Invalid Entry of time. Try again.&quot;&lt;&lt;endl;
time();
}
else
timeminutes==00;
cout&lt;&lt;&quot;Initial Time: &quot;&lt;&lt;timehours&lt;&lt;&quot;: &quot;&lt;&lt;timeminutes&lt;&lt;endl;
}
void time::enterTime()
{
cout&lt;&lt;&quot;Enter hours: &quot;&lt;&lt;endl;
cin&gt;&gt;hours;
if(hours&lt;00 &amp;&amp; hours&gt;23)
{
cout&lt;&lt;&quot;Invalid entry of time. Try Again.&quot;&lt;&lt;endl;
enterTime();
}
cout&lt;&lt;&quot;Enter minutes: &quot;&lt;&lt;endl;
cin&gt;&gt;minutes;
if(minutes&lt;00)
{
cout&lt;&lt;&quot;Invalid entry of time. Try Again,&quot;&lt;&lt;endl;
enterTime();
}
cout&lt;&lt;&quot;Initial Time:- &quot;&lt;&lt;hours&lt;&lt;&quot;: &quot;&lt;&lt;minutes&lt;&lt;endl;
}
void time::addMinutes()
{
if(minutes&gt;60 &amp;&amp; minutes%60&gt;0)
{
int addhours,addMin;
addMin=(minutes%60&gt;0);
addhours=(minutes/60);
hours+=addhours;
if(hours&gt;=24)
hours-=24;
}
}
void time::displayTime()
{
cout&lt;&lt;&quot;Updated time:&quot;&lt;&lt;hours&lt;&lt;&quot; :&quot;&lt;&lt;minutes&lt;&lt;&quot; &quot;&lt;&lt;endl;
}
int main()
{
int n;
for(n=0;n&lt;4;n++)
{
time Time[4];
if((n+1)%2==0)
{
Time[n].time();
Time[n].addMinutes();
Time[n].displayTime();
}
else
{
Time[n].enterTime();
Time[n].addMinutes();
Time[n].displayTime();
}
}
return 0;
}

答案1

得分: 2

问题1:

第一个问题是,你试图在类外部定义默认构造函数,但是在类内部没有声明它。请记住,在类外部定义任何成员函数之前,必须首先在类内部进行相应的声明。

所以为了解决这个问题,移除那个 ~,它实际上将该声明变成了析构函数声明,如下所示:

class time
{
private:
    int hours, minutes;
public:
    time(int);
    // 移除这里的 ~
    time();

    // 其他与之前相同的代码
};

问题2:

另外,避免使用 using namespace std;,以防止用户定义的类的名称与标准库名称发生冲突。【为什么"using namespace std;" 被认为是不好的做法?】(https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)

英文:

There are two problems with your code as described below:


<h4>Problem 1</h4>

The first problem is that you're trying to define the default constructor outside the class while you have not declared it inside the class. Remember that in order to define any member function outside the class, its corresponding declaration must be there inside the class first.

So to solve this remove the ~ which actually made that declaration a destructor declaration, as shown below:

class time
{
private:
int hours,minutes;
public:
time(int);
//-v----------------------&gt;removed ~ from here
time();
//other code as before
};

<h4>Problem 2</h4>

Additionally avoid using using namespace std; so that there is no conflict between names of user defined classes and standard library names. Why is "using namespace std;" considered bad practice?

答案2

得分: 2

你的类时间与标准库中的函数时间之间存在冲突。

这里

time Time[4];

编译器期望函数调用的参数。

这是避免使用 using namespace std 的一个原因。

英文:

You have a conflict between the class time, and the function time from the standard library.

Here

time Time[4];

the compiler expects a parameter for the function call.

This is one reason for avoiding using namespace std.

huangapple
  • 本文由 发表于 2023年2月26日 20:59:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定