【问题标题】:Returning different types of variables返回不同类型的变量
【发布时间】:2019-05-01 09:07:46
【问题描述】:

对不起,如果我的标题有点误导,不知道如何总结我目前遇到的问题。

基本上我的任务是使用继承。 但我目前的问题是我不确定如何在同一个函数中返回 int 和字符串以显示在另一个函数中。

下面是例子,希望更有意义。

我尝试过使用引用,但显然我做错了,因为我无法让它工作。

Main.cpp:

#include <iostream>
#include "Text.h"

int main(){

    Text text1;
    text1.SetText("Arial", 12, "Black", "This is a sample text"); //String, int, string, string
    text1.PrintText();

    return 0;
}

文本.h:

#ifndef TEXT_H
#define TEXT_H

class Text{

    public:
        Text();
        void SetText(std::string font, int size,  std::string color, std::string data);
        void GetParameters(); /*This is the problem area. I have setText that will set it, but how do I return these values to use in PrintText if it has different variable types?*/
        void PrintText();

    private:
        std::string font;
        int size;
        std::string color;
        std::string data;
};

#endif // TEXT_H 

文本.cpp:

#include <iostream>
#include "Text.h"

Text::Text(){
}

void Text::SetText(std::string font, int size,  std::string color, std::string data){
    font = font;
    size = size;
    color = color;
    data = data;
}

void Text::GetParameters (){//Any pointers in this would be much appreciated.
}

void Text::PrintText(){
    cout <<"Text parameters are: "  <<GetParameters() <<endl;//this is where Im trying to simply display the font, size, color and data values.
}

对不起,如果它有点冗长,我不确定要包含多少才能正确说明我遇到的问题。

我试图达到的结果是非常基本的:

Text parameters are:
Font = Arial
Size = 12
Color = Black
Data = This is a sample text

值得一提的是,我不能在这个作业中使用结构。

【问题讨论】:

  • 如果你想要对象中的所有字段,那与仅仅拥有对象有什么不同呢?看起来你想要的是用这些内容组装一个字符串以便于打印。

标签: c++ oop inheritance


【解决方案1】:

看起来您只需要返回一个字符串作为函数GetParameters 的输出。请注意,目前您的返回类型是void(不返回任何内容)。

示例代码:

std::string Text::GetParameters() {
    std::string stringToReturn = "";
    stringToReturn += "Font = " + font + "\n";
    stringToReturn += "Size = " + std::to_string(size) + "\n";
    // leave it to you to figure out the rest of the function
    return stringToReturn;
}

【讨论】:

    【解决方案2】:

    只需打印出数据成员:

    void Text::PrintText()
    {
        cout << "font: " << font << ", size: " << size << ", color: " << color
             << ", data: " << data << "\n";
    }
    

    如果你想从非成员函数中访问这些成员,那么添加公共 getter 成员函数:

    class Text {
    public:
        // ...
    
        const std::string& getFont() const { return font; }
        int getSize() const { return size; }
        const std::string& getColor() const { return color; }
        const std::string& getData() const { return data; }
    };
    

    我建议重命名私有成员变量,以清楚地区分它们与公共成员。一种流行的方法是在他们的名字后面加上一个下划线:

    class Text {
        // ...
    
    private:
        std::string font_;
        int size_;
        std::string color_;
        std::string data_;
    };
    

    【讨论】:

      【解决方案3】:

      std::string Text::GetParameters() 函数已被其他答案所涵盖,但我想指出您的 void Text::SetText 实现的一个错误:

      void Text::SetText(std::string font, int size,  std::string color, std::string data){
          font = font;
          size = size;
          color = color;
          data = data;
      }
      

      这不会做任何事情,因为font = font; 只是将font 参数设置为它已经存在的值,其他参数也是如此。相反,试试这个:

      void Text::SetText(std::string font, int size, std::string color, std::string data) {
          this->font = font;
          this->size = size;
          this->color = color;
          this->data = data;
      }
      

      通过说this-&gt;font,您是在告诉它您不是指font 参数,而是对象的font 字段。现在它正确地将字段设置为相应的参数。

      【讨论】:

        猜你喜欢
        • 2013-09-28
        • 1970-01-01
        • 1970-01-01
        • 2012-08-06
        • 2016-02-01
        • 2023-01-29
        • 2020-06-13
        • 2019-05-15
        • 1970-01-01
        相关资源
        最近更新 更多