Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

error C2065: 'cout' : undeclared identifier - WTF?

tentimestentimes Posts: 30Member
I hope someone can help with this because I'm going out of my mind trying to figure it out.

Before we go any further.. the #include is about 10 lines above where I get this error.

I doing Ivor's book on VC6++ and am at the bit where he starts writing yet another rambling bit of code making a derived class from a simple CBox class, called CandyBox.

Well I decided rather than do a 'type after me' I would use the Wizard to create my own derived class and all is working fine. UNTIL I put in a simple function ShowCandy which uses cout.(I've still the copy constructor and assignment overload to go btew I haven't forgotten)

Firstly here is "CandyBox.h":



#include "Box.h"

class CCandyBox : public CBox
{
public:

char* m_Contents;

void ShowCandy() const;

CCandyBox(char* str="Candy");
virtual ~CCandyBox();

};


Then we have "CandyBox.cpp"


#include "CandyBox.h"
#include

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

void CCandyBox::ShowCandy() const
{
cout << m_Contents;
}


CCandyBox::CCandyBox(char* str)
{
m_Contents=new char[strlen(str)+1];
strcpy(m_Contents,str);
}

CCandyBox::~CCandyBox()
{
delete[] m_Contents;
}


Comments left out for space. Basically I've only got constructor destructor there apart from this. It just won;t pick upo the cout no matter where I move the include to, if I only include one include to iostream or if I put includes in everywhere... same thing. The weird thing is that, as you can see, it picks up the include for <cstring> ok and sure enough, when I remove the include for cstring I get the same error as I am getting with the cout.

It's as if it can't see #include anymore.

AAAAAAAAAAAAAAAAAAAAAAARRRRGH!!!

Any help appreciated I'm gnawing my own fingers off.


Comments

  • BaafBaaf Posts: 59Member
    : I hope someone can help with this because I'm going out of my mind trying to figure it out.
    :
    : Before we go any further.. the #include is about 10 lines above where I get this error.
    :
    : I doing Ivor's book on VC6++ and am at the bit where he starts writing yet another rambling bit of code making a derived class from a simple CBox class, called CandyBox.
    :
    : Well I decided rather than do a 'type after me' I would use the Wizard to create my own derived class and all is working fine. UNTIL I put in a simple function ShowCandy which uses cout.(I've still the copy constructor and assignment overload to go btew I haven't forgotten)
    :
    : Firstly here is "CandyBox.h":
    :
    :
    :
    : #include "Box.h"
    :
    : class CCandyBox : public CBox
    : {
    : public:
    :
    : char* m_Contents;
    :
    : void ShowCandy() const;
    :
    : CCandyBox(char* str="Candy");
    : virtual ~CCandyBox();
    :
    : };
    :
    :
    : Then we have "CandyBox.cpp"
    :
    :
    : #include "CandyBox.h"
    : #include
    :
    : //////////////////////////////////////////////////////////////////////
    : // Construction/Destruction
    : //////////////////////////////////////////////////////////////////////
    :
    : void CCandyBox::ShowCandy() const
    : {
    : cout << m_Contents;
    : }
    :
    :
    : CCandyBox::CCandyBox(char* str)
    : {
    : m_Contents=new char[strlen(str)+1];
    : strcpy(m_Contents,str);
    : }
    :
    : CCandyBox::~CCandyBox()
    : {
    : delete[] m_Contents;
    : }
    :
    :
    : Comments left out for space. Basically I've only got constructor destructor there apart from this. It just won;t pick upo the cout no matter where I move the include to, if I only include one include to iostream or if I put includes in everywhere... same thing. The weird thing is that, as you can see, it picks up the include for <cstring> ok and sure enough, when I remove the include for cstring I get the same error as I am getting with the cout.
    :
    : It's as if it can't see #include anymore.
    :
    : AAAAAAAAAAAAAAAAAAAAAAARRRRGH!!!
    :
    : Any help appreciated I'm gnawing my own fingers off.
    :
    :


    ok this may sound stupid, but maybe you should change to or "iostream.h" (copy it to the dir). Have fun..

  • Eric TetzEric Tetz Posts: 2,141Member
    : I hope someone can help with this because I'm going out of my mind trying to figure it out.
    :
    : Before we go any further.. the #include is about 10 lines above where I get this error.

    [snipped: a bunch of code]

    : It just won't pick upo the cout no matter where I move the include to, if I only include one include to iostream or if I put includes in everywhere... same thing. The weird thing is that, as you can see, it picks up the include for ok and sure enough, when I remove the include for cstring I get the same error as I am getting with the cout.
    :
    : It's as if it can't see #include anymore.

    First, let's reduce this to the miminal amount of code necessary to illustrate the problem:

    [code] #include
    #include
    int main() {
    printf( "Hello" );[green]// OK[/green]
    cout << "Hello"; [red]// error![/red]
    return 0;
    }[/code]

    The problem here is that all the symbols in the C++ standard library are in the namespace std. In this app, there is no symbol in the global namespace named 'cout', so the compiler correctly compains. To tell the compiler that we're talking about the 'cout' in the namespace 'std', we need to qualify the name, which we can do in one of three ways.
    [code] #include <cstdio>
    #include
    int main() {
    printf( "Hello" );[green]// OK[/green]
    [b]std::[/b]cout << "Hello"; [green]// OK: cout explicitly qualified[/green]
    return 0;
    }[/code]

    [code] #include <cstdio>
    #include
    [b]using std::cout[/b];[green]// hoist 'cout' into the global namespace[/green]
    int main() {
    printf( "Hello" );[green]// OK[/green]
    cout << "Hello"; [green]// OK: cout visible now[/green]
    return 0;
    }[/code]

    [code] #include <cstdio>
    #include
    [b]using namespace std[/b];[green]// hoist [italic]everything[/italic] in namespace std into global namespace[/green]
    int main() {
    printf( "Hello" );[green]// OK[/green]
    cout << "Hello"; [green]// OK: cout visible now[/green]
    return 0;
    }[/code]

    I should mention that printf() is [italic]supposed[/italic] to be in the namespace std, too. So we should get the same error for the unqualified 'printf' that we did for 'cout'. However, this is one of the many areas where MSVC++ does not yet conform to the standard, so you just have to live with it. One place where this bites you is that the following valid C++ will not compile correctly in MSVC++:

    [code] #include <cstdio>
    #include
    int main() {
    [b]std::[/b]printf( "Hello" );[green]// OK: printf explicitly qualified[/green]
    [b]std::[/b]cout << "Hello"; [green]// OK: cout explicitly qualified[/green]
    return 0;
    }[/code]

    MSVC++ compains that 'printf is not in namespace std', which is simply wrong.

    Cheers,
    Eric


  • Eric TetzEric Tetz Posts: 2,141Member
    : : It's as if it can't see #include anymore.
    : :
    :
    : ok this may sound stupid, but maybe you should change to or "iostream.h" (copy it to the dir).

    That will work in MSVC++ 6.0, but it may or may not work in any other C++ compiler, or even in future versions of MSVC++. is [italic]not[/italic] valid header name in ANSI C++, is.

    Cheers,
    Eric

  • tentimestentimes Posts: 30Member
    That fixed it! Thanks :)


    : : I hope someone can help with this because I'm going out of my mind trying to figure it out.
    : :
    : : Before we go any further.. the #include is about 10 lines above where I get this error.
    :
    : [snipped: a bunch of code]
    :
    : : It just won't pick upo the cout no matter where I move the include to, if I only include one include to iostream or if I put includes in everywhere... same thing. The weird thing is that, as you can see, it picks up the include for ok and sure enough, when I remove the include for cstring I get the same error as I am getting with the cout.
    : :
    : : It's as if it can't see #include anymore.
    :
    : First, let's reduce this to the miminal amount of code necessary to illustrate the problem:
    :
    : [code] #include
    : #include
    : int main() {
    : printf( "Hello" );[green]// OK[/green]
    : cout << "Hello"; [red]// error![/red]
    : return 0;
    : }[/code]
    :
    : The problem here is that all the symbols in the C++ standard library are in the namespace std. In this app, there is no symbol in the global namespace named 'cout', so the compiler correctly compains. To tell the compiler that we're talking about the 'cout' in the namespace 'std', we need to qualify the name, which we can do in one of three ways.
    : [code] #include <cstdio>
    : #include
    : int main() {
    : printf( "Hello" );[green]// OK[/green]
    : [b]std::[/b]cout << "Hello"; [green]// OK: cout explicitly qualified[/green]
    : return 0;
    : }[/code]
    :
    : [code] #include <cstdio>
    : #include
    : [b]using std::cout[/b];[green]// hoist 'cout' into the global namespace[/green]
    : int main() {
    : printf( "Hello" );[green]// OK[/green]
    : cout << "Hello"; [green]// OK: cout visible now[/green]
    : return 0;
    : }[/code]
    :
    : [code] #include <cstdio>
    : #include
    : [b]using namespace std[/b];[green]// hoist [italic]everything[/italic] in namespace std into global namespace[/green]
    : int main() {
    : printf( "Hello" );[green]// OK[/green]
    : cout << "Hello"; [green]// OK: cout visible now[/green]
    : return 0;
    : }[/code]
    :
    : I should mention that printf() is [italic]supposed[/italic] to be in the namespace std, too. So we should get the same error for the unqualified 'printf' that we did for 'cout'. However, this is one of the many areas where MSVC++ does not yet conform to the standard, so you just have to live with it. One place where this bites you is that the following valid C++ will not compile correctly in MSVC++:
    :
    : [code] #include <cstdio>
    : #include
    : int main() {
    : [b]std::[/b]printf( "Hello" );[green]// OK: printf explicitly qualified[/green]
    : [b]std::[/b]cout << "Hello"; [green]// OK: cout explicitly qualified[/green]
    : return 0;
    : }[/code]
    :
    : MSVC++ compains that 'printf is not in namespace std', which is simply wrong.
    :
    : Cheers,
    : Eric
    :
    :


Sign In or Register to comment.