Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1011097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:11:00+00:00 2026-05-16T09:11:00+00:00

I guess this is a really simple question and, probably, one that has been

  • 0

I guess this is a really simple question and, probably, one that has been answered several times over. However, I really do suck at C++ and have searched to no avail for a solution.
I would really appreciate the help.

Basically:

#ifndef ANIMAL_H
#define ANIMAL_H

class Animal 
{
 public:
  void execute();
  void setName(char*);
  Animal();
  virtual ~Animal(); 

 private:
  void eat();
  virtual void sleep() = 0;

 protected:
  char* name;
};

class Lion: public Animal 
{
 public:
  Lion();

 private:
  virtual void sleep();
};



class Pig: public Animal 
{
 public:
  Pig();

 private:
  virtual void sleep();
};



class Cow: public Animal
{
 public:
  Cow();

 private:

  virtual void sleep();
};

#endif

Is the header file, where:

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

using namespace std;

Animal::Animal()
{
 name = new char[20];
}
Animal::~Animal()
{
 delete [] name;
}

void setName( char* _name )
{
 name = _name;
}

void Animal::eat() 
{
 cout << name << ": eats food" << endl;
}
void Animal::execute() 
{
 eat();
 sleep();
}


Lion::Lion()
{
 name = new char[20];
}  
void Lion::sleep()
{
 cout << "Lion: sleeps tonight" << endl;
}


Pig::Pig()
{
 name = new char[20];
}   
void Pig::sleep()
{
 cout << "Pig: sleeps anytime, anywhere" << endl;
}


Cow::Cow()
{
 name = new char[20];
}
void Cow::sleep()
{
 cout << "Cow: sleeps when not eating" << endl;
}

is the C file.
As you can see, really simple stuff, but, I get the: “error: ‘name’ was not declared in this scope” whenever I try to compile.

It compiles if I comment out the setName method. Iv tried setting ‘name’ to public and still get the same error. I have also tried using “this->name = _name” in setName(), which results in “invalid use of ‘this’ in non-member function”.

I don’t know what else to search for. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T09:11:01+00:00Added an answer on May 16, 2026 at 9:11 am
    void setName( char* _name )
    {
     name = _name;
    }
    

    should be

    void Animal::setName( char* _name )
    {
      this->name = _name;
    }
    

    You need to have Animal:: if you use the this parameter. Without Animal:: it thinks you are just creating a new global function called setName

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, I guess this day had to come. My client's website has been compromised
I'm new to web programming in general so this is probably a really simple
I'm sure this is a really simple question. The following code shows what I'm
I guess this is an odd one, and the answer is most likely it
I have a HashMap (although I guess this question applies to other collections) of
Ok another WPF question, well I guess this is just general .NET. I have
I realize that the answer to this question is likely quite obvious (if somewhat
Okay, I guess this is entirely subjective and whatnot, but I was thinking about
I am busy writing my thesis (so, I guess this could count as a
I'm learning about POSIX threads right now, but I guess this is just a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.