Hi Guys, this is my first post here so please go easy. Anyways, I'm obviously something of a beginner with C++ and have been teaching myself using CodeBlocks and VC++ 2010 Express. I'm trying to get the hang of header files, and for the most part I seem to have done that with CodeBlocks. My problem is that I cannot seem to get my projects to compile in VC++ if I include headers.
I'll create a header source file in VC++ and input the same exact code as CodeBlocks, but it will not compile. I went ahead and took out all the other misc. parts of the code to make it easier to examine and (hopefully) find the problem:
#pragma once
#include <iostream>
using namespace std;
string strFunction(int x);
When I try and run this in VC++, I get pages of errors (I'm hoping that this is easy enough of a fix for one of you C++ Masters out there that I wont spam all the errors now, but if they're needed, just let me know).
The maddening thing is that the header WILL work in VC++ for some things. I include <iostream> in there just fine, can declare global variables, and even include function prototypes for some other types of functions.
I have not been able to figure out if my problems are due to VC++ using precompiled headers (something I don't quite as of yet understand deeply), or a newer syntax or w/e. I've tried so many fixes I'm about to pull my hair out.
I'd really appreciate any help with this and look forward to meeting you guys. Hopefully one day I'll be good enough to help people on here as well.
I have not been able to figure out if my problems are due to VC++ using precompiled headers (something I don't quite as of yet understand deeply)
This might be the problem.
When you created the project, did it start you off with premade files? If yes, then you created the wrong kind of project. Scrap that one and start over.
When you are creating the project, make sure you select the "create empty project" option in the wizard.
Other than that there's nothing terribly wrong with your code (a few bad practices but nothing worth getting twisted up in a knot over). It should be compiling just fine.
Can't give a definitive answer without seeing the errors though. For your future reference, always post errors. Just saying "my code gives me errors" is not very helpful. The error tells us exactly what the problem is. Without it, we have to guess.
If you get hundreds of errors, they're probably all related so you don't have to post them all. You can just post the first couple. Like 1 or 2.
I've tried it several ways. I loaded a pre-configured Console project and toyed around with putting my header stuff in stdafx.h and resource.h (the pre-made headers) and I've also started from complete scratch and made my own header (as I did with the code here...these are the only three files in my project and the only code within them). Both ways eventually lead me to the same issue.
Sorry for not including the error msgs, I guess I figured I was just missing something completely obvious and they wouldn't be needed.
Here they are:
Main.cpp
1>c:\users\michael\documents\visual studio 2010\projects\project7\project7\main.cpp(5): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
1> c:\program files\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\ostream(726): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\ostream(764): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
----
And it pretty much goes on like that for much longer. I've stared at it for a while and tried some things but I'm really just not knowledgeable enough yet to really grasp what the heck it wants me to do differently.
Please also let me know what bad practices you saw in there if you don't mind. It'd likely be easier to correct all that stuff now before I get used to doing it. Thanks
Can anybody else with VC++ try running this program and see if it works for them? Maybe I'm just missing some include files or something from a bad install?