Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C++中返回一个STD字符串_C++ - Fatal编程技术网

在C++中返回一个STD字符串

在C++中返回一个STD字符串,c++,C++,我试图在函数中返回字符串rock。我使用cout调用函数,您似乎没有修改rock,因此它仍然是一个空字符串 这将使用空岩串构造计算机对象: 这可能会修改岩石,但不会,因为它被注释掉了: /*computertest.Computerplayer();*/ 代码看起来不错。请提供一个包含计算机类声明和使用该类的代码的列表。在显示的代码中,从returncomputer返回空白字符串的唯一方法是如果rock一开始是空白的,这意味着computertest变量没有正确初始化。您应该将数据硬编码到程序

我试图在函数中返回字符串rock。我使用cout调用函数,您似乎没有修改rock,因此它仍然是一个空字符串

这将使用空岩串构造计算机对象:

这可能会修改岩石,但不会,因为它被注释掉了:

/*computertest.Computerplayer();*/

代码看起来不错。请提供一个包含计算机类声明和使用该类的代码的列表。在显示的代码中,从returncomputer返回空白字符串的唯一方法是如果rock一开始是空白的,这意味着computertest变量没有正确初始化。您应该将数据硬编码到程序中,而不是输入语句中。通过这种方式,我们可以准确地看到正在发生的事情,并可以获取您的代码,编译它,然后查看结果。嗨,PaulMcKenzie,输入是3R S P,但只有它的大小将用于computer.cpp。大小将为7。不,将数据放入您发布的程序中,而不是作为说明。好的,我已经为main.cpp编辑了它。
//computer.h
#ifndef COMPUTER_H
#define COMPUTER_H
#include <iostream>
#include <string>
using namespace std;


class Computer{
    public:
    Computer(string Rock, int Size);
    void Computerplayer();
    string returncomputer();
    private:
    string rock;
    int size;




};
#endif
//main.cpp
#include "computer.h"
#include "human.h"
#include "referee.h"
#include <iostream>
#include <string>
#include <sstream>


using namespace std;

int main(){

    int size;
    string type = "3 S R P";
    string rock;
    getline(cin, type);
    size = type.length();

    cout<< size<<endl;
    Human humantest(size, type);
    Computer computertest(rock, size);
    /*Referee refereetest(size, type, computertest.returncomputer());
    humantest.Humanplayer();
    refereetest.Result();*/
    /*computertest.Computerplayer();*/
    cout<<computertest.returncomputer()<<endl;


}
Computer computertest(rock, size);
/*computertest.Computerplayer();*/