-3

In nestClassDef.h I have written code like this

  class A{
    public:
     class B{
         public:
           void BTest();
     };
 };

   class B{

   };

then in the nestClassDef.cpp I am writing code like this

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

      void A::B::BTest(){
        cout<<"Hello World!";
     }

    int main(){
      A a;
      A.B b;
      b.BTest();
    } 

But when I am compiling the above code

       g++ -o nestClassDef nestClassDef.cpp

I am getting error like this :-

      nestClassDef.cpp: In member function ‘void A::B::BTest()’:
      nestClassDef.cpp:5: error: ‘cout’ was not declared in this scope
      nestClassDef.cpp: In function ‘int main()’:
      nestClassDef.cpp:10: error: expected unqualified-id before ‘.’ token
      nestClassDef.cpp:11: error: ‘b’ was not declared in this scope 

I am at a loss how to fix this. Any understanding shared will be thankfully received.

John Doe
  • 2,752
  • 5
  • 40
  • 58
  • 1
    At first I would consider indenting the code sensible. It's ignored by the compiler but helps human readers. Second you should specify the namespace, it's `std::cout`. – harper Mar 08 '14 at 09:24
  • 2
    In case people suggest to write `using namespace std;`, read this first. It is usually [a terrible idea](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – juanchopanza Mar 08 '14 at 09:25
  • 1
    Did you consider asking google about "error: ‘cout’ was not declared in this scope"? It gives hundreds of copies of the answer... – Marc Glisse Mar 08 '14 at 09:26
  • I admit it's a mistake not to have referred google at first but I was compounded by two error messages so was little confused. – John Doe Mar 08 '14 at 09:31
  • Thanks @juanchopanza lesson learnt will use the std::cout all the time. – John Doe Mar 08 '14 at 09:34
  • 2
    When there are several errors, start with the first one. The others might just mean that the first error confused the compiler enough to start saying nonsense. – Marc Glisse Mar 08 '14 at 12:52
  • Oh! ok got it, will follow this now on. – John Doe Mar 09 '14 at 12:35

3 Answers3

2
  nestClassDef.cpp: In member function ‘void A::B::BTest()’:
  nestClassDef.cpp:5: error: ‘cout’ was not declared in this scope

Use std::cout instead of cout, or add using namespace std; (probably after your #include statements).

  nestClassDef.cpp: In function ‘int main()’:
  nestClassDef.cpp:10: error: expected unqualified-id before ‘.’ token
  nestClassDef.cpp:11: error: ‘b’ was not declared in this scope 

Use A::B instead of A.B.

Paul Draper
  • 78,542
  • 46
  • 206
  • 285
2

For the cout error: it's in std namespace, so use std::cout.

For The second error: B is not A's member, it's a nested type, so you have to use A::B b;

Michał Góral
  • 1,439
  • 2
  • 16
  • 30
1
  1. Add using namespace std; or use std::cout instead of just cout
  2. Use A::B not A.B. Dot operator is used on objects or structs/unions.