Thread: String append

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    String append

    I just want to add to a string

    Code:
    string Character::toString() {
    	string result = "    ";
    	result += getName() + "     \n";
    	result += "    " + getClass() + "    \n";
    	result += "    " + getHealth() + "    \n";  //HERE
    	return result;
    }
    Tells me I cannot add two pointers. But the thing that confuses me is that it gives me the error message on the last line before returning and not the top line. I thought C++ strings were able to use + to append to them.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Things between "" characters are C-style string literals, not std::strings. Even so, this code should be working. The reason it isn't is probably because getHealth() is returning a number instead of a std::string.

    You really should use a stringstream for this:

    Code:
    string Character::toString() {
    	stringstream result;
            result << "    ";
    	result << getName() << "     \n";
    	result << "    " << getClass() << "    \n";
    	result << "    " << getHealth() << "    \n";
    	return result.str();
    }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM