Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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))
    {
    }
}

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?

Thanks in advance.

Updated portion

Now all the error has been fixed:

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());
    }
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
367 views
Welcome To Ask or Share your Answers For Others

1 Answer

To fix "text_input is showing identifier is undefined"

You need to change

 while(getline(cin, text_input))

to

 while(getline(cin, message1->text_input))

Possibly this will fix the first error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...