1

just a simple program but can anyone point out why this error is occuring, (i am using dev C++ version 5.11)

#include <stdio.h>
#include <conio.h>

class animal
{
 public :
    void sound();

     void eat() ;

};
void animal::eat()
{
        cout<<"i eat animal food" ; 


}


void animal::sound()
{
        cout<<"i sound like an animal" ;

     }

void main()
{
    animal a ;
    a.sound()
    a.eat()
    getch()
}

the error is coming like this:

In member function 'void animal::eat()':
15  4   C:\Users\chetna\Documents\Untitled1.cpp [Error] 'cout' was not declared in this scope
1   0   C:\Users\chetna\Documents\Untitled1.cpp In file included from C:\Users\chetna\Documents\Untitled1.cpp
rubenvb
  • 74,642
  • 33
  • 187
  • 332
M Desmond
  • 113
  • 1
  • 3
  • 1
    The error is quite clear, the compiler doesn't know what `cout` is. You need to include the appropriate header, as well you need to reference the namespace it belongs in (`std`) – ChrisMM Nov 05 '19 at 13:49
  • 1
    @ChrisMM Although the error is quite clear, the actual solution is not apparent from it. Neither of the two things required follow directly from that error. Please don't post answers as comments. – rubenvb Nov 05 '19 at 15:11

2 Answers2

3

At least you have to include

#include <iostream>

and

using namespace std;

The name cout is declared in the namespace std. So either use the using directive as shown above or use a qualified name (that is better) like

std::cout<<"i eat animal food" ; 

An alternative approach is to use a using declaration. For example

#include <iostream>

using std::cout;

//...
void animal::eat()
{
        cout<<"i eat animal food" ; 
}

And remove this directive

#include <stdio.h>

Also place semicolons

a.sound();
a.eat();
getch();

Pay attention to that the function main shall be declared like

int main()
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I wouldn't necessarily advocate `using namespace std` in an answer. – sweenish Nov 05 '19 at 14:26
  • 1
    @sweenish For starters there is written "at least" in my answer. Secondly he is a beginner that starts to learn C++. All books on C++ for beginners usually use the using directive. So there is nothing wrong for a beginner to use the directive. – Vlad from Moscow Nov 05 '19 at 14:28
  • I figured this response was coming, and I **generally** agree with it. But whenever I've taught intro, that line of code comes with a 1 minute spiel about being beginner "friendly" and that they shouldn't get too attached to it. In fact, I'm not planning on using that line in the future as it just creates a pain point for too many students in future classes. As an answer, advocating that line with no explanation has the potential to imply that it's required. I find the "at least" to be an inadequate guard. – sweenish Nov 05 '19 at 14:36
1

Please, stop using the old Borland C++ et al.

Use a modern standards compliant C++ compiler (g++, clang, Microsoft Visual Studio) instead.

Do not use conio.h, it is a very old compiler specific library, not a standard one.

Do not use stdio.h it is not a bad library, but it declares several C functions, not C++ ones.

Declare your main function as

int main()

not void main(), because standard C++ needs the main function to return an int (0 for success).

Instead of using cout, use std::cout , because it is an object representing the standard output defined inside the std namespace.

ebasconp
  • 1,608
  • 2
  • 17
  • 27