c++ vs2003 treeview and adding a save to a text file causing errors

I was using vs2008 express but found  had a lot of mfc issues with some tree view code, so i have a full version of vs2003 so i loaded that up to try that,
the program that i am running is a tree view that i got from codeproject http://www.codeproject.com/KB/tree/Win32TreeList/TreeListSrc.zip
great little example and does what i need, and after i convert it to run in vs2003 it works just fine.
I then decided to load up some extra code into the project to save to a text file.
I loaded this and ran it in vs2003 as its own little project to test it and it worked fine, the code is below

// savetext.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
      
  ofstream myfile;
  myfile.open ("C:\\log\\examplexxx.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
      
      return 0;
}

ok so we have this running
we also have the tree view running as well
So i think i will add the code into the treeview and get it running in bits
I add #include <iostream> to the container.c file
and it throws up all sorts of errors as you can see by the images
I have only added the #include <iostream> into the container code and then it breaks
because they run by them selves and when you start modifying it breaks i am thinking some sort of comparability problem
any help would be most appreciated at this time
erroroutpt.png
errortasklist.png
sydneyguyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffric\Commented:
Container.c and TreeList.c are written in C, not in C++.
The .c extension results in VS compiling these files using its C-compiler, not its C++ compiler, so including <iostream> into a C-module will not work.
Container.c already includes <stdio.h>.

You could build your program in a C-project instead of C++.
You can also import your treelist into a C++ program, but that requires additional steps since your main program written in C++ will still be calling C-functions. If you take this approach, you will have a mixed-language program (C and C++) .
phoffric\Commented:
If you are required to have a C++ program that uses the tree list C-code, then you can wrap the C-header files in a special construct to enable C++ functions to call C-functions.

This FAQ explains how to do it:
"How do I call a C function from C++? "
https://isocpp.org/wiki/faq/mixing-c-and-cpp#call-c
https://isocpp.org/wiki/faq/mixing-c-and-cpp

This FAQ gives a common usage:
"How can I modify my own C header files so it’s easier to #include them in C++ code?"
https://isocpp.org/wiki/faq/mixing-c-and-cpp#include-c-hdrs-personal

In a nutshell, add to your C-headers files two sets of code:

Step #1: Put the following lines at the very top of your C header file
(Note: the symbol __cplusplus is #defined if/only-if the compiler is a C++ compiler):
#ifdef __cplusplus
extern "C" {
#endif

Open in new window

Step #2: Put the following lines at the very bottom of your C header file:
#ifdef __cplusplus
}
#endif

Open in new window

Now, when you include your C-header from a C++ module, then the result is that the compiler will call the C-function using the C-name-mangling rules (that you don't need to worry about) rather than use the C++ rules. When you include your C-header in a C-module, then the extern "C" line of code will not be present in the preprocessed code since the symbol __cplusplus will not be defined by the C-compiler when processing the C-module.
phoffric\Commented:
Just wanted to clarify an earlier point:
>>so including <iostream> into a C-module will not work. Container.c already includes <stdio.h>.

Both headers iostream and stdio.h handle input/output. But the iostream header is strictly a C++ header and may not be included in a C-language module. The stdio.h header can be included in either a C++ or C language file; although the preferable usage for the stdio.h header is now called cstdio (without the .h extension).
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

sarabandeCommented:
you normally could compile a .c file without issues in c++. right-click on container.c in the solution tree and choose properties. go to configuration properties - c/c++ - advanced and choose 'compile as c++ code'. from your error description it might be possible that container.c already was compiled with c++ despite of its .c extension. you would do that both for debug and release configuration.

the errors you encounter when including <iostream> may be due to the compiler used with the source. but even if you already used c++ compiler for the sourec or change to c++ compiler, you also could get such errors easily with c++ sources if there are some macros in the c code of a header file which conflicts with names used in the iostream.

so first make sure that the c++ compiler was used. then comment include statements of pure c headers like string.h, iostream.h. then, try to include stdafx.h as first header which would include mfc and winapi. you don't need to switch-on precompiled headers (pch) using stdafx.h. from my experience pch makes it more difficult and is error-prone. then avoid to including stl headers into header files if possible. for example iostream header sureley is only needed in .c or .cpp sources. next is to avoid 'using namespace std' clause. instead use std:: prefix when using stl code.

if the errors still exist after all that, try to move include statements of stl containers above all other header includes (but below stdafx.h) or below all other headers. if the error still exist try to eliminate include statements which actually were not needed. i always achieve to eliminate more than 50 percent of all (old) hearder incluse statements without any issue and further 30 percent after some work like removing redundant macros, forward declarations, and others. since we surely have some name conflict (either a class type was not recocnized by the c compiler, or a name in c++ already was defined in a c header), reducing the number of include statements is a good means to spot the issue and get a good workaround.

Sara

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
I was using vs2008 express but found  had a lot of mfc issues with some tree view code, so i have a full version of vs2003 so i loaded that up to try that

you may think of using vs 2015 community which is a full visual studio prof. with mfc but free.

Sara
sydneyguyAuthor Commented:
you may think of using vs 2015 community which is a full visual studio prof. with mfc but free.
thankss for the heads up over this one, will look into it when i get furthur down the track and have some time to have a play around with the new version, most helpfull though
sydneyguyAuthor Commented:
thanks for all the above information understand the lay out of the prcompilers much better now so we are moving on in this project

I am now trying to access the data from a ms access db an have posted a new question up on this subject if you are interested
again thanks for all your help we are getting there

http://www.experts-exchange.com/questions/28852559/c-msaccess-db-access-preferably-not-going-though-a-from.html
sydneyguyAuthor Commented:
thanks fo4 the help we are getting there
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.