alaskaman
alaskaman

Reputation: 49

in constructor 'cout' was not declared in this scope

i wanted to practise some basic c++ stuff. I focused on inheritance just to experiment with that. Everything went well until i faced some strange problem: 'cout' was not declared in this scope. I have looked on some topics but on most of them the hints were like append library or write 'using namespace std' but it doesn't solve my problem.

#include <iostream>

class podst
{
public:
    float a;
    float b;
    float dodaw();
    podst(float c,float d) : a(c), b(d)
    {

    }
};
float podst::dodaw()
{
    return (a+b);
}

class poch : public podst
{
    poch() : podst(5,4)
    {
       cout << a << endl << b << dodaw() << endl;
    }

};


using namespace std;
int main()
{
    podst podst(1,2);
    cout <<  podst.dodaw() << endl;
    poch poch2;

    return 0;


}

Upvotes: 0

Views: 3689

Answers (6)

Arnaud Dhaene
Arnaud Dhaene

Reputation: 11

If you want to omit the standard library namespace caller ::std, you should write

using namespace std;

before (usually written right after #include statements and before int main()"

cout << ...

as the compiler reads the code sequentially. Note that it is considered bad practice to use 'using'. I recommend getting used to using with the std:: namespace caller as such:

std::cout << "Hello World!" << std::endl;

To fix your specific problem:

#include <iostream>

using namespace std;

class podst { ...

Upvotes: 1

Frank Siwale
Frank Siwale

Reputation: 11

class poch : public podst
{
    poch() : podst(5,4)
    {
       //you should have used std::cout
       std::cout << a << endl << b << dodaw() << std::endl;
    }

};

Upvotes: 0

Frank Siwale
Frank Siwale

Reputation: 11

well since poch is a class inheriting from a parent class you needed to either declare the standard namespace at the beginning of the file to make it global to all classes and functions but you declared it before main so the poch class was not able to make use of it, rather you could have explicitly used std::cout and std::endl and the program would have no trouble compiling.

Upvotes: 0

besc
besc

Reputation: 2647

A using declaration starts to take effect at the point where you write it. Since the call to cout is above that point it isn’t covered by the using and you’ll have to spell out std::cout there.

You should avoid using namespace std. A better alternative is to only pull in what you need, e.g. with using std::cout;. But the even better choice is to just use the qualified names. It’s a bit more to type, but it prevents ambiguity; and the qualified names are so ubiquitous in every code base I’ve seen that – at least to me – an unqualified name like cout is always a bit surprising and trips me up slightly when reading code.

Upvotes: 0

user7860670
user7860670

Reputation: 37488

poch calls cout from global namespace, not from std namespace. Notice that using namespace std; directive is actually below it, not above. If you don't want to write whole namespace prefix every time it would be better to place that directive on per-function basis, rather then per-file basis:

poch() : podst(5,4)
{
   using namespace std;
   cout << a << endl << b << dodaw() << endl;
}

This approach will help to keep large code bases in piece.

Upvotes: 1

Audrey Brooke
Audrey Brooke

Reputation: 99

the using namespace std is below the first time you use cout. try using std::cout and std::endl in the poch class.

Upvotes: 2

Related Questions