Simple simple program

This is a discussion on Simple simple program within the C++ Programming forums, part of the General Programming Boards category; Hello, I have this very simple program which won't compile. Please help me! Code: #include <iostream> #include <string> #include "stdafx.h" ...

  1. #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!

  2. #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. . .

  3. #3
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Worked for me too...sorry...wish i could tell ya.
    big146

  4. #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++?

  5. #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. . .

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.

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

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    move the line
    #include "stdafx.h"

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

  9. #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!!

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > 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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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