Skip to main content
GameDev.net gamedev.net
🔒 Locked

first string program

Started by JonBMN Aug 1, 2012 at 8:24 PM 3 replies 1.4k views
Original Post
JonBMN
JonBMN
This is an example from a book, but it doesn't seem to build. Any help?

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
#include <iostream>
using namespace std;
int main()
{
string mystring;
mystring = "Hello there";
cout << mystring << endl;
system("pause");
return 0;

}


the error

[
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> while trying to match the argument list '(std::ostream, std::string)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
fastcall22
fastcall22
Recreate your Visual C++ console project, be sure that the "Empty project" option is checked, then try the code again.

EDIT: Also, what rip-off said. (I can't believe I missed that.)
fastcall22
fastcall22
The code looks fine, aside from the automatically generated stuff from Visual Studio. If you are just starting C++, the recreate the project with the "empty project" option set. Otherwise, read on:

Remember that in C++, code can be compiled and linked differently, depending on the many options and configurations the compiler and linker offer. Visual Studio offers several preset projects that you can create your project on. To the best of my recollection, the non-empty C++ project in visual studio has the following "features" set automtaically:
1. Precompiled headers. These files are stdafx.h and stdafx.cpp. What happens here is all of the includes for stdafx.cpp and stdafx.h are processed into a pch file. This pch file is referred to by the compiler when other cpp files include the precompiled header. This greatly reduces compile times, since the separate cpp files do not have to reprocess each header for each cpp file. Useful? Yes, but not necessary for the beginner.
2. "Standard win32 defines and includes" to allow your program to be built with and without Unicode support. With the switch of a compiler option, the _TCHARs convert to and from char and wchar_t types. If you plan on using Unicode in your program, then make the decision before coding and just stick to using wchar_t. Useful? Yes, if you're developing for Windows, but not for the beginner grasping the basics.
3. Sets entry point to _tmain (as opposed to standard-C++ main) for the same reason as #2. (Not quite sure about this one, it may just be a macro alias)
rip-off
rip-off
It appears you haven't pasted the full error. However, I suspect the error is becayse you did not #include .
JonBMN
JonBMN

It appears you haven't pasted the full error. However, I suspect the error is becayse you did not #include .


Thank you rip-off I appreciate the help greatly.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.