Thread: Please help with simple program

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    3

    Please help with simple program

    Hi, I am making a program that prints actual mood that the user sets. But I can't find on the internet the right solution of fixing an error: "no matching function for call to 'Mood::setMood(std::__cxx11::string&)'".
    main.cpp
    Code:
    #include<iostream>
    #include<Mood.h>
    #include<string>
    
    std::string input;
    std::string*pInput =&input;
    
    int main()
    {
        Mood mood1;
        std::string input;
        std::string*pInput =&input;
        std::cout <<"Enter mood: ";
        std::cin >>*pInput;
        mood1.setMood(*pInput);
    
        std::cout << mood1.getMood()<< std::endl;
    
        return 0;
    }
    Mood.h
    Code:
    #ifndef MOOD_H
    #define MOOD_H
    #include<string>
    
    
    class Mood
    {
    public:
        Mood(std::string*pInput);
        void setMood(std::string x);
        std::string getMood();
    
    protected:
    
    private:
        std::string mood;
    };
    
    #endif// MOOD_H
    Mood.cpp
    Code:
    #include"Mood.h"
    #include<iostream>
    #include<string>
    
    Mood:: Mood(std::string*pInput)
    {
        std::string x;
    }
    
    void Mood::setMood(std::string x)
    {
        mood = x;
    }
    
    std::string Mood::getMood()
    {
        return mood;
    }
    EDIT:
    I have edited your code to remove extra markup. Next time, just post your code as plain text within the code tags. The forum software will add readable markup and line numbering for you. ~laserlight
    Last edited by laserlight; 11-18-2019 at 04:08 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,397
    In main.cpp, you have these two lines before the main function:
    Code:
    std::string input;
    std::string*pInput =&input;
    Get rid of them as they should be in the main function instead, and in fact you do have the duplicate code in the main function.

    In main, you do this:
    Code:
    Mood mood1;
    The problem is that Mood doesn't have a default constructor because you declared another constructor without declaring the default constructor. You probably do want the default constructor, so let's leave this for now.

    Next, this code again, but in main:
    Code:
    std::string input;
    std::string*pInput =&input;
    There is absolutely no need for pInput. What you should do is this:
    Code:
    std::string input;
    std::cout << "Enter mood: ";
    std::cin >> input;
    mood1.setMood(input);
    Now, you mentioned an error with setMood, but as far as I can tell, the error message does not correspond to your code, i.e., your code is correct. Perhaps you confused yourself by posting an error message from an earlier version of your code.

    Now, in mood.h I would remove the protected label from the class definition since there are no members with protected access.

    Going back to the constructor:
    Code:
    Mood(std::string*pInput);
    As far as I can tell, you don't need this. You just want a default constructor that does what is default. Therefore, get rid of this. Likewise, get rid of the definition of this constructor in mood.cpp

    If you prefer, you can explicitly declare the defaulted default constructor, so that even if you add other constructors later, it will continue to exist:
    Code:
    Mood() = default;
    Next, this:
    Code:
    void setMood(std::string x);
    should be:
    Code:
    void setMood(const std::string& x);
    since you don't want a copy of the source string until you actually copy it into the mood member variable. Remember to make the corresponding change in mood.cpp

    (Well, it is okay to make a copy at that point since you do need a copy either way, but then for efficiency you would want to invoke the move assignment operator for the mood member variable instead... not much point bothering with that when you can just defer the copy and do copy assignment later.)

    Finally, this:
    Code:
    std::string getMood();
    should be:
    Code:
    std::string getMood() const;
    since you want to be able to call getMood on a Mood object that is in a const context. Again, remember to make the corresponding change in mood.cpp
    Last edited by laserlight; 11-18-2019 at 04:28 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    3
    Thank you very much! :-) It's working

  4. #4
    Registered User
    Join Date
    Nov 2019
    Posts
    1
    I'm new to c programming so this was a great help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM

Tags for this Thread