Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

first string program


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 JonBMN   Members   -  Reputation: 596

Like
0Likes
Like

Posted 01 August 2012 - 02:24 PM

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 ==========


Ad:

#2 fastcall22   Members   -  Reputation: 1868

Like
2Likes
Like

Posted 01 August 2012 - 03:02 PM

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.)

Edited by fastcall22, 01 August 2012 - 05:57 PM.


#3 fastcall22   Members   -  Reputation: 1868

Like
0Likes
Like

Posted 01 August 2012 - 03:14 PM

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)


#4 rip-off   Moderators   -  Reputation: 5027

Like
1Likes
Like

Posted 01 August 2012 - 03:36 PM

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

#5 JonBMN   Members   -  Reputation: 596

Like
0Likes
Like

Posted 02 August 2012 - 08:42 AM

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


Thank you rip-off I appreciate the help greatly.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS