title: Return a std string in C++
body: <p>I'm trying to return the <code>string rock</code> in my function. I call the function by using <code>cout&lt;&lt;computertest.returncomputer()&lt;&lt;endl;</code> in main function. However I got a empty string. But if I change rock to "abc", it will return abc. What am I doing wrong in my code? By the way, test1 and test2 give me R R R R when size = 7.</p>
  
  <pre><code>//computer.cpp
  #include "computer.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;stdio.h&gt;
  using namespace std;
  Computer::Computer(string Rock, int Size){
      rock = Rock;
      size = Size;
  }
  void Computer::Computerplayer(){
  
      int i = 0;
      int l = 1;
  
      for(i=0;i&lt;size;i+=2){
  
          rock += 'R';
          rock += ' ';
  
      }
      /*cout&lt;&lt;"test1 "&lt;&lt;rock&lt;&lt;endl;*/
  
  }
  
  string Computer::returncomputer(){
    /* cout&lt;&lt;"test2 "&lt;&lt;rock&lt;&lt;endl; */
      return rock;
  }
  </code></pre>
  
  <p>header</p>
  
  <pre><code>//computer.h
  #ifndef COMPUTER_H
  #define COMPUTER_H
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  
  class Computer{
      public:
      Computer(string Rock, int Size);
      void Computerplayer();
      string returncomputer();
      private:
      string rock;
      int size;
  
  
  
  
  };
  #endif
  </code></pre>
  
  <p>main</p>
  
  <pre><code>//main.cpp
  #include "computer.h"
  #include "human.h"
  #include "referee.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  
  
  using namespace std;
  
  int main(){
  
      int size;
      string type = "3 S R P";
      string rock;
      getline(cin, type);
      size = type.length();
  
      cout&lt;&lt; size&lt;&lt;endl;
      Human humantest(size, type);
      Computer computertest(rock, size);
      /*Referee refereetest(size, type, computertest.returncomputer());
      humantest.Humanplayer();
      refereetest.Result();*/
      /*computertest.Computerplayer();*/
      cout&lt;&lt;computertest.returncomputer()&lt;&lt;endl;
  
  
  }
  </code></pre>