Thread: class methods to cout stream

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    44

    class methods to cout stream

    I have a class method that outputs a string to cout and am getting an error message for it, Im having trouble finding info on how (or if) class methods can output strings to cout.

    Code:
    cout << applicant2.GetName();
    
    //this is the get method
    string DateProfile::GetName()
    {
       return name;
    }
    
    //this is the set method
    void DateProfile::SetName(string inClassName)
    {
       name = inClassName;
    }
    
    //this is where the set method gets its value
    applicant2.SetName("Jane");
    So what is going wrong?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You forgot to post the error message you're getting.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    44
    cpjust, ok here it is, and the lower part of the error just repeats over and over for about a mile. Im using VC++ Express
    Code:
    ------ Build started: Project: assignment 7, Configuration: Debug Win32 ------
    Compiling...
    assignment 7.cpp
    c:\documents and settings\drak\my documents\visual studio 2008\projects\assignment 7\assignment 7\assignment 7.cpp(71) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
            c:\program files\microsoft visual studio 9.0\vc\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
            with
            [
                _Elem=char,
                _Traits=std::char_traits<char>
            ]

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you have other strings that work? I.e., like
    Code:
    string bob = "Hello!";
    cout << bob;
    Or is it only strings that are returned from functions?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Looks like you forgot to #include <string>.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    44
    tabstop- Youre right, its some kind of problem with cout in general. I tried to run your bob string and it gave me the exact error same messages.

    Edit:
    Daved- Yes that is it. And yes when this C++ class is over Im really going to stop programming and no longer bother people with my hair brain programming problems
    Last edited by shintaro; 11-11-2008 at 07:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class + Main() = error cout?
    By Lincoln_Poh in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2008, 04:36 AM
  2. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. exporting class methods to library results in .lib and .exp
    By Shadow12345 in forum C++ Programming
    Replies: 15
    Last Post: 01-05-2003, 08:01 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM