C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-08-2004, 08:34 PM   #1
Registered User
 
Join Date: Sep 2004
Posts: 3
Simple simple program

Hello, I have this very simple program which won't compile. Please help me!

Code:
#include <iostream>
#include <string>
#include "stdafx.h"


int main()
{
	std::string name("This is a test!");
	
	std::cout << name << std::endl;


	return 0;
}
I get the error:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

I am using Visual c++ .NET. Can someone please tell me why this doesn't work?

thanks!
Ryback is offline   Reply With Quote
Old 09-08-2004, 08:38 PM   #2
Arggggh
 
DeepFyre's Avatar
 
Join Date: Sep 2004
Posts: 227
hmmm.....not sure.....it works for me in dev-cpp
__________________
Keyboard Not Found! Press any key to continue. . .
DeepFyre is offline   Reply With Quote
Old 09-08-2004, 09:10 PM   #3
Registered User
 
big146's Avatar
 
Join Date: Apr 2003
Posts: 74
Worked for me too...sorry...wish i could tell ya.
__________________
big146
big146 is offline   Reply With Quote
Old 09-08-2004, 09:12 PM   #4
i dont know
 
Vicious's Avatar
 
Join Date: May 2002
Posts: 1,200
Yep worked for me, of course in dev-c++ I removed the "stdafx.h".
__________________
What is C++?
Vicious is offline   Reply With Quote
Old 09-08-2004, 09:13 PM   #5
Arggggh
 
DeepFyre's Avatar
 
Join Date: Sep 2004
Posts: 227
ya you dont get stafx.h with dev cpp
__________________
Keyboard Not Found! Press any key to continue. . .
DeepFyre is offline   Reply With Quote
Old 09-08-2004, 09:24 PM   #6
Carnivore ('-'v)
 
Hunter2's Avatar
 
Join Date: May 2002
Posts: 2,865
I would remove the stdafx.h, simply because in this case there is absolutely no use for it. Other than that however, I see nothing syntactically wrong with your code - unless of course the C++ standard does not allow use of std::string with cout's << operator, which I highly doubt since nearly every compiler allows it.

If you want it to *work* though, try using name.c_str() instead of name. It is guaranteed to be supported by the standard. And if that doesn't work, then you know your compiler's screwed
__________________
Just Google It. √

(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Hunter2 is offline   Reply With Quote
Old 09-08-2004, 10:15 PM   #7
Registered User
 
Join Date: Sep 2004
Posts: 3
Thanks all for the help. I found some more information. When I create my project in Visual Studio I am choosing a "Win32 Console Project". This is why the #include "stdafx.h" line is there (and cannot be removed). But the program does not work. But here is what else I discovered: if I instead create a "Console Application (.NET)" project, the program will work without any errors.

Does anyone have experience with Visual Studio who can help?

Thanks guys.
Ryback is offline   Reply With Quote
Old 09-08-2004, 10:38 PM   #8
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
move the line
#include "stdafx.h"

To the top of your includes. stdafx.h must always be included first.
bithub is offline   Reply With Quote
Old 09-08-2004, 10:44 PM   #9
Registered User
 
Join Date: Sep 2004
Posts: 3
Quote:
Originally Posted by bithub
move the line
#include "stdafx.h"

To the top of your includes. stdafx.h must always be included first.

Bithub, that works! Thank you!!
Ryback is offline   Reply With Quote
Old 09-09-2004, 01:34 AM   #10
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> This is why the #include "stdafx.h" line is there
Remove it, and then turn off "precompiled headers" in the project settings.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 09-09-2004, 05:48 AM   #11
Registered User
 
big146's Avatar
 
Join Date: Apr 2003
Posts: 74
If you want to run just a simple console app, select win32 >> then console project, when the application wizzard dialog pops up select application settings and then select Empty project. After the solution loads up right click source files and select add >> then new item >> from the list of templates select cpp.

that will give you a project without the stdafx.h header files.
then just add your code from the book.
__________________
big146
big146 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using variables in system() Afro C Programming 8 07-03-2007 12:27 PM
[Help] Simple Array/Pointer Program sandwater C Programming 3 03-30-2007 02:42 PM
simple silly program verbity C Programming 5 12-19-2006 06:06 PM
fopen(); GanglyLamb C Programming 8 11-03-2002 12:39 PM
Need help with simple DAQ program canada-paul C++ Programming 12 03-15-2002 08:52 AM


All times are GMT -6. The time now is 11:42 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22