getline()方法没有重载函数的实例 [英] getline() method no instance of overloaded function

查看:1601
本文介绍了getline()方法没有重载函数的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为C ++做类时遇到一些问题。这是我的头文件.h:

I am having some problem when trying to do classes for C++. This is my header file.h:

#include <iostream>
#include <string>
#ifndef MESSAGES__H__
#define MESSAGES__H__

class Message
{
    public:
        Message(std::string recipient, std::string sender);
        void append(std::string text);
        std::string to_string() const;
        void print() const;
    private:
        std::string recipient;
        std::string sender;
        std::string message;
        std::string text_input;
        char* timestamp;
};

#endif

当我运行main方法时,getline cin,)给我一些错误消息:

And when I run the main method, the getline(cin,) is giving me some error message:

int main()
{
    vector <Message*> message_list;
    Message* message1 = new Message("Student1", "Gabriel");
    cout << "Enter message text line, enter . on new line to finish: " << endl;
    while(getline(cin, text_input))
    {
    }
}

getline方法不给我重载函数的实例。另外,从同一行,text_input显示的标识符是未定义的。我想我已经在.h类中声明了。

The getline method is giving me no instance of overloaded function. Also, from the same line, the text_input is showing identifier is undefined. I thought I declared in .h class already?

提前感谢。

/ strong>

Updated portion

现在所有错误已修复:

vector <Message*> message_list;
Message* message1 = new Message("Saiful", "Gabriel");
cout << "Enter message text line, enter . on new line to finish: " << endl;
while(getline(cin, message1->get_text_input()))
{
    if(message1->get_text_input() == ("."))
    {
        break;
    }
    else
    {
        message1->append(message1->get_text_input());
    }
}

在while循环中,一次。在新行的开始处被检测到,它假定将停止。但是,无论我多少次进入。在新的一行,它只是继续提示。任何人都知道为什么?

Inside the while loop, once "." is detected at the beginning of the new line, it supposingly will stop. However, no matter how many times I entered "." at the new line, it just keep prompting. Anybody know why?

推荐答案

感觉如果你过于复杂的事情。只需在 getline 中使用一个临时变量。如果输入是。然后中断,否则将该行追加到Message对象。因此,您应该可以从消息头文件中删除 text_input 成员变量。

It feels as if you are over complicating things. Just use a temporary variable in getline. If the input is a "." then break, otherwise append the line to the Message object. As a consequence, you should be able to remove the text_input member variable from your Message header file.

std::vector<Message*> message_list;
Message* message1 = new Message("Saiful", "Gabriel");
std::cout << "Enter message text line, enter . on new line to finish: " << std::endl;
std::string input = "";
while(getline(std::cin, input))
{
    if(input == ".")
    {
        break;
    }
    else
    {
        message1->append(input);
    }
}

这篇关于getline()方法没有重载函数的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆