Link to home
Start Free TrialLog in
Avatar of steva
steva

asked on

Errors in simple C++ program with Visual Studio 2010 Express

Can someone tell me why I'm getting the errors below for the simple C++ program shown?

#include "stdafx.h"
#include <tchar.h>
#include <iostream>

#using <mscorlib.dll>
#using <System.dll>
#using <System.xml.dll>
      
using namespace System;
using namespace System::Xml;
      
void main(void)
{
      XmlTextReader^ reader = gcnew XmlTextReader ("books.xml");
      cout << reader->Read() ;
   
}

------ Build started: Project: Q815658, Configuration: Debug Win32 ------
  Q815658.cpp
Q815658.cpp(17): error C2065: 'cout' : undeclared identifier
Q815658.cpp(17): warning C4804: '<<' : unsafe use of type 'bool' in operation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Thanks for your help...
Avatar of rockiroads
rockiroads
Flag of United States of America image

urm not sure, I think in mfc it was okay not to specify .h but not sure about 2010

does it make a difference if you change

#include <iostream>

to

#include <iostream.h>
or maybe specify the std lib like this

std::cout << reader->Read() ;

what is reader->Read() ; doing? is that expecting input?
Avatar of steva
steva

ASKER

Actually,I made a mistake.  The code should be:

void main(void)
{
      XmlTextReader^ reader = gcnew XmlTextReader ("books.xml");
       reader->Read() ;   // read the file
       cout << reader->Value;  
}

But no matter, the cout still doesn't compile.  If I change the #include to <iostream.h>  the build says it can't fine iostream.h. At std::cout doesn't work either.

"reader" here is an XmlTextReader object frm .NET  that reads the next node of the xml file "books.xml."

ASKER CERTIFIED SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of steva

ASKER

Nope.  Putting
                         
using namespace std;

after the includes doesn't make any difference.  cout is still an "undeclared identifier."
Avatar of steva

ASKER

Ah!  Got it.

cout requires the two statements

#include <iostream>
using namespace std;

But even then I can't write

                     cout << reader->Value;

because the thing in the .NET reader->Value  is not a string.

After adding

#include <iostream>
using namespace std;

and changing that line to

                   cout << "A string";

I compile ok.  So I'm still not sure how to display what's in reader->Value but at least the build problem is solved.  Thanks for your help.  I gave you the points for working with me on it.

Avatar of steva

ASKER

Solved it myself, though the dialog with rockiroads was helpful.
Hi Steva. Went to bed soon after last posting.

So what did you do when you solved it. Did you assign reader->value to a string variable then outputted that variable?
Avatar of steva

ASKER

The magic combination to get cout to be recognized was

#include <iostream>
using namespace std;

or

#include <iostream>

std::cout

iostream.h doesn't work.

But even after that the combination of cout and reader->Value didn't work because the .NET reader->Read() function put something into reader.Value that wasn't compatible with the type of string that cout was expecting. I ended up using String^ s = reader.Value.

By the way, I posted two more questions on .NET and Visual Studio Express at

https://www.experts-exchange.com/questions/26497579/Is-there-an-XMLTextReader-without-NET.html

and

https://www.experts-exchange.com/questions/26496756/Converting-a-Visual-C-6-0-MFC-application-to-Visual-Studio-2010.html

if you have time and feel like looking at them.

Steve