【问题标题】:C++ Setters not assigning values to derivative classesC++ Setter 不给派生类赋值
【发布时间】:2020-10-02 15:04:56
【问题描述】:

我的二传手没有设置。我已经解决了这个问题大约一个半星期了。就像一切都到位,代码编译并且似乎一切都在工作,但我的设置器没有设置,即使在从派生中正确实例化两个不同的对象之后:

#include <iostream>
#include <conio.h> // _getch()
#include <string>
#include <ctype.h>

using namespace std;

#define TMSG_LEN_PRICE 0.02 // Price of Text Message


// 1st Derived Class, Text Message
class tMsg
{
private:
    float textMessageInCharacters;
public:
    tMsg() {
        textMessageInCharacters = 0.0f;
    }
    // Getter
    int getTextMessageLength()
    {
        return textMessageInCharacters;
    }

    // Setter
    void setTextMessageLength(int textLength)
    {
        textMessageInCharacters = textLength;
    }
};

int main()
{
    string usrInput;   // User Input
    string tMessage; // Text message string
    int vMessage_Min;    // Voice message length in minutes

    try
    {

        tMsg textService; // Text messaging service

        // Menu output for user.
        printf("Select an option:\n\nT. Text Message\nV. Voice Message\n\nOption: ");
        getline(cin, usrInput);

        // Get text message from user
        if (usrInput == "t") {
            printf("Input your message then hit [ENTER] when done: \nMessage: ");
            getline(cin, tMessage);
            textService.setTextMessageLength(tMessage.size());

        }
    }

    // Catch exception
    catch (exception& ex) {
        // Throw exception
        cout << ex.what();

        // Pause screen wait for user input
        _getch();
    }

    // Exit
    return 0;

}

它并没有真正创建输出,当我中断派生类中的某些分配时,程序会终止,就好像没有分配任何内容一样。我确保局部变量正在接受分配的值,但它不喜欢实例化的东西。

编辑:需要缩短不必要的代码。

【问题讨论】:

  • 您可以通过删除示例程序中未使用的所有函数来缩短它以创建minimal reproducible example,例如getMessagePricegetTextMessageLengthgetVoiceMessageLengthsetMessagePricesetVoiceMessageLength。看起来你使用的唯一setter是setTextMessageLength
  • 没有分配任何内容的原因是因为您从不调用任何 setter。
  • 代码的哪一部分应该创建任何输出?您永远不会尝试打印出任何成员的值。
  • 为什么要创建基类的实例而不是语音类

标签: c++ getter-setter derived-class


【解决方案1】:

Getter Setter 正在工作。您没有在 main 中调用 getter。希望对您有所帮助!

#include <iostream>
#include <conio.h> // _getch()
#include <string>
#include <ctype.h>

using namespace std;

#define VMSG_LEN_PRICE 0.03 // Price of Voice Message
#define TMSG_LEN_PRICE 0.02 // Price of Text Message

// Base Class
class qMsg
{
private:
    float messagePrice; // Final Return Price of Messages Voice/Text
public:
    qMsg() {
        messagePrice = 0.0f;
    }

    // Getter
    int getMessagePrice()
    {
        return messagePrice;
    }

    // Setter
    void setMessagePrice(float price)
    {
        messagePrice = price;
    }
};

// 1st Derived Class, Text Message
class tMsg : public qMsg
{
private:
    float textMessageInCharacters;
public:
    tMsg() {
        textMessageInCharacters = 0.0f;

    }

    // Setter
    void setTextMessageLength(int textLength)
    {
        textMessageInCharacters = textLength;

    }
    // Getter
    int getTextMessageLength()
    {
        return textMessageInCharacters;
    }

};

// 2nd Derived Class, Voice Message
class vMsg : public qMsg
{
private:
    float voiceMessageInMinutes;
public:
    vMsg() {
        voiceMessageInMinutes = 0.0f;
    }
    // Getter
    int getVoiceMessageLength()
    {
        return voiceMessageInMinutes;
    }

    // Setter
    void setVoiceMessageLength(int voiceLength)
    {
        voiceMessageInMinutes = voiceLength;
    }

};

int main()
{
    string usrInput;   // User Input
    string tMessage; // Text message string
    int vMessage_Min;    // Voice message length in minutes

    try
    {
        qMsg commServices; // Voice messaging service
        tMsg textService; // Text messaging service

                          // Menu output for user.
        printf("Select an option:\n\nT. Text Message\nV. Voice Message\n\nOption: ");
        getline(cin, usrInput);

        // Get text message from user
        if (usrInput == "t") {
            printf("Input your message then hit [ENTER] when done: \nMessage: ");
            getline(cin, tMessage);
            textService.setTextMessageLength(tMessage.size());
            cout << "GET MESSAGE LENGTH :  ";
            cout<<textService.getTextMessageLength();
            cout << endl;        


        }
        else if (usrInput == "v")
        {
            // Return minutes for voice message
            printf("Input the length of your voice message: ");
            cin >> vMessage_Min;

        }

    }

    // Catch exception
    catch (exception& ex) {
        // Throw exception
        cout << ex.what();

        // Pause screen wait for user input
        _getch();
    }

    // Exit
    system("pause");
    return 0;

}

【讨论】:

  • 谢谢,我只是不知道为什么设置断点时我的代码没有中断。我没有想到要调用我的 getter 进行调试 =\。
猜你喜欢
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
  • 2021-07-31
  • 2012-02-12
  • 2016-01-28
  • 2020-10-12
相关资源
最近更新 更多