CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2008
    Posts
    6

    What's wrong with my constructor/class?

    Code:
    class Mob
    {
    public:
         Mob(int hp = 50, string name = 'Boar'); //if nothing is set, default to 50
    
         int getHP() const;
         string getName() const;
         int dmg();
    private:
         int m_HP;
         string m_Name;
    };
    
    Mob::Mob(int hp, string name):
                 m_HP(hp),
                 m_Name(name)
    {
         cout << "--" << getName() " created with " << getHP() << " HP" << endl << endl;
    }
    inline int Mob::getHP() const
    {
         return m_HP;
    }
    inline string Mob::getName() const
    {
         return m_Name;
    }
    inline int Mob::dmg()
    {
         m_HP = m_HP - 14;
         if (m_HP <= 0) { cout << "you killed it!" << endl; }
         else getHP();
    }
    any ideas?

    error is on the line
    Code:
         cout << "--" << getName() " created with " << getHP() << " HP" << endl << endl;
    thanks in advance!

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: What's wrong with my constructor/class?

    Quote Originally Posted by immanuelx2
    any ideas?

    error is on the line
    Code:
         cout << "--" << getName() " created with " << getHP() << " HP" << endl << endl;
    What error?
    Did you forget "<<" operator between "getName()" and " created with "?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    Jun 2008
    Posts
    6

    Re: What's wrong with my constructor/class?

    thanks, that was it.

    now i get a new error on this line: (in main)

    Code:
    Mob mob1;
    says "invalid conversion from `int' to `const char*' "

  4. #4
    Join Date
    Jun 2008
    Posts
    6

    Re: What's wrong with my constructor/class?

    fixed! I needed to use double quotes (") where i set the default string.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured