【问题标题】:compiling crash - using a member function within another member function编译崩溃 - 在另一个成员函数中使用成员函数
【发布时间】:2011-04-17 01:50:21
【问题描述】:

我正在尝试用 C++ 编写一个简单的时间显示程序。


已编辑

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

class vClock
{
    public:

        // constructor
        vClock(int = 0, int = 0);

        // mutable member functions
        void set_time(int, int);
        void time_ahead(int);

        // constant function
        string time_notation(int) const;
        void show_time() const;

    private:
         int hour;
         int minute;
         int offset_hour;
         int offset_minute;
         int maxhour;
         int maxminute;
         int carrier;
};

// member function implementation 

vClock::vClock(int hr, int min)
{
    hour = hr;
    minute = min;
    maxhour = 24;
    maxminute = 60;
    carrier = 0;
}

void vClock::set_time(int hr, int min)
{
    hour = hr;
    minute = min;
}

void vClock::time_ahead(int add_minute)
{
            // suppose to be a short cut for all cases

        carrier = ((add_minute + minute) / maxminute);
        offset_hour = hour + carrier;
        offset_minute = (add_minute + minute) - (carrier * maxminute);

        cout << "After " << add_minute << "minutes, the time will be "
             << setfill('0') << setw(2) << offset_hour << ":" << setw(2) << offset_minute << time_notation(offset_hour)<< endl;

    return;
}

string vClock::time_notation(int hr) const
{
    if(hour < 12)
        cout << "AM";
    if (hour >= 12)
        cout << "PM";
}

void vClock::show_time() const{

    cout << setfill('0') 
          << setw(2) << hour << ':'
          << setw(2) << minute 
          << time_notation(hour) << endl;
    return;

}

// member functions implementation END


int main()
{
    vClock sample;

    sample.set_time(0,59);
//  sample.show_time();

    sample.time_ahead(118);

    return EXIT_SUCCESS;
}

似乎 time_notation 在 cout 语句之前被评估?

*AM*118分钟后,时间为02:57


解决了

我无法再编译它了 - 在我将 time_notation(offset_hour) 添加到 time_ahead() 和 show_time()(位于两个函数体的最后一行)之后,我的 IDE 将崩溃。

在实现该功能并在其他功能中使用之前,编译是可以的。该程序运行良好。

违法吗?

我收到一条很长的错误消息

clock-time.cpp:65: error: no match for 'operator&)(+std::operator&)(+(+std::operator&)(+std::operator&)(+std:: operator&)(+(+std::operator& )(&std::cout)), ((const char*)"After ")))->std::basic_ostream<_chart _traits>::operatorstd::basic_ostream<_chart _traits>::operatorwith _CharT = char, _Traits = std::char_traits)), ((const char*)":")))), std::setw(2)) )->std::basic_ostream<_chart _traits>::operatorwith _CharT = char, _Traits = std::char_traits vClock::time_notation(((vClock*)this) ->vClock::offset_hour)'

我正在使用 MinGW C++ 编译器,我的 IDE 是 jGRASP。 任何帮助表示赞赏。

谢谢!

【问题讨论】:

    标签: c++ class


    【解决方案1】:

    那是因为time_notation 返回 void。 void 类型不完整,无法打印。

    不知道为什么 IDE 会崩溃。

    修复可能是将time_notation 更改为返回'string' 类型。

    【讨论】:

    • 是的,你们俩都是对的。感谢您的提醒。我调整了你的方法,似乎 time_notation 是首先评估的。我更新了代码....输出以 AM 开头
    • 实际上,如果我将 time_notation 移动到它自己的 cout,我会得到“a.exe 遇到问题并需要关闭(Windows 上通常的 CMD 关闭错误)。我可以看到但是,时间之后是 AM。
    • 没关系。我看到了问题。因为它是一个字符串,并且唯一的返回是 cout,所以我正在复制 cout。我从另一个函数中删除了 cout,并将其用作 time_notation(hour) 。谢谢!
    【解决方案2】:

    time_notation 返回void。你希望它打印什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多