Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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++新手,我正在编写一个C++程序来模拟一群兔子。程序会自动添加它们,给出它们的姓名、年龄等。我有点搞不懂为什么我的程序会给出这些信息。我已经研究了好几个想法,但似乎还是不知所措。到目前为止,在我的尝试中,我一直专注于获得一个要展示的名字。当程序执行变量name、sex、color时,不返回任何内容_C++_Constructor - Fatal编程技术网

为什么我的程序输出为零? 我是C++新手,我正在编写一个C++程序来模拟一群兔子。程序会自动添加它们,给出它们的姓名、年龄等。我有点搞不懂为什么我的程序会给出这些信息。我已经研究了好几个想法,但似乎还是不知所措。到目前为止,在我的尝试中,我一直专注于获得一个要展示的名字。当程序执行变量name、sex、color时,不返回任何内容

为什么我的程序输出为零? 我是C++新手,我正在编写一个C++程序来模拟一群兔子。程序会自动添加它们,给出它们的姓名、年龄等。我有点搞不懂为什么我的程序会给出这些信息。我已经研究了好几个想法,但似乎还是不知所措。到目前为止,在我的尝试中,我一直专注于获得一个要展示的名字。当程序执行变量name、sex、color时,不返回任何内容,c++,constructor,C++,Constructor,代码如下: #include <iostream> #include <string> #include <ctime> #include <vector> #include <cstdlib> using namespace std; void setSex(); char getSex(); void setColor(string color); string getColor(); void setAge(int age)

代码如下:

#include <iostream>
#include <string>
#include <ctime> 
#include <vector>
#include <cstdlib>

using namespace std;

void setSex();
char getSex();
void setColor(string color);
string getColor();
void setAge(int age);
int getAge();
void setName(string name);
string getName();
void printBunny();
int randomGeneration(int x);

//static std::string  POSSIBLE_NAMES = 18;
//static std::string   POSSIBLE_COLORS = 4;

static std::string possibleNames[] ={
    "Jen",
    "Alex",
    "Janice",
    "Tom",
    "Bob",
    "Cassie",
    "Louis",
    "Frank",
    "Bugs",
    "Daffy",
    "Mickey",
    "Minnie",
    "Pluto",
    "Venus",
    "Topanga",
    "Corey",
    "Francis",
    "London",
};
static std::string possibleColors[] ={

    "White",
    "Brown",
    "Black",
    "Spotted"
}; 

struct Bunny
{
    public:

        string name;
        int age;
        string color;
        char sex;

        Bunny(){
            name = "";
            age = 0;
            color = "";
        }
        Bunny(string name, string color, int age){

            name = name;
            color = color;
            age = age;
            setName(name);
            setColor(color);
            setAge(age);

        }

        //radioactive_mutant_vampire_bunny(radioactive_mutant_vampire_bunny)

        /*bool sex;
        std::string name, color;
        int age;
        //string name;
        bool radioactive_mutant_vampire_bunny;*/

    int randomGeneration(int x){
        return rand() % x;
    }
    void setSex()
    {
        int randomNumber = 1 + rand() % 2;

        ( randomNumber == 1 ) ? sex = 'm' : sex = 'f';
    }

    char getSex() 
    {
        return sex;
    }

    void setColor(string color)
    {
        //color = possibleColors[ 0 + rand() % POSSIBLE_COLORS ];
    }

    string getColor() 
    {
        return color;
    }

    void setAge(int age)
    {
        age = 0;
    }

    int getAge() 
    {
        return age;
    }

    void setName(string name)
    {
        int i = randomGeneration(18);
        name = possibleNames[i];
        //name = possibleNames[ 0 + rand() % POSSIBLE_NAMES ];
    }

    string getName() 
    {
        return name;
    }

    void printBunny() 
    {
        cout << "Name: " << getName() << endl;
        cout << "Sex: " << getSex() << endl;
        cout << "Color: " << getColor() << endl;
        cout << "Age: " << getAge() << endl;
    }
};

int main()
{

    vector< Bunny > colony;

    cout << "Welcome to Bunny Graduation!" << endl << endl;

    for( int i = 0; i < 5; ++i )
    {
        colony.push_back( Bunny() );
    }

    for( int i = 0; i < 5; ++i )
    {
        colony[ i ].printBunny();
        cout << endl;
    }

    return 0;
}

您的代码在很多方面都有缺陷,我宁愿给出某种实现,希望最终结果是您所期望的

请注意,此代码并不代表实际的C++1x标准,它的发布仅用于教育目的

#include <iostream>
#include <ctime>
#include <vector>

using namespace std;

const int  POSSIBLE_NAMES = 18;
const int  POSSIBLE_COLORS = 4;

static std::string possibleNames[] ={
    "Jen",
    "Alex",
    "Janice",
    "Tom",
    "Bob",
    "Cassie",
    "Louis",
    "Frank",
    "Bugs",
    "Daffy",
    "Mickey",
    "Minnie",
    "Pluto",
    "Venus",
    "Topanga",
    "Corey",
    "Francis",
    "London",
};
static std::string possibleColors[] ={

    "White",
    "Brown",
    "Black",
    "Spotted"
};

struct Bunny
{
public:

string name;
int age;
string color;
char sex;

Bunny(){
    setSex();
    setColor();
    setAge(0);
    setName();
}

int randomGeneration(int x){
    return rand() % x;
}

void setSex()
{
    int randomNumber = randomGeneration(2);

    ( randomNumber == 1 ) ? sex = 'm' : sex = 'f';
}

char getSex()
{
    return sex;
}

void setColor()
{
    int randomNumber = randomGeneration(POSSIBLE_COLORS);
    color = possibleColors[randomNumber];
}

string getColor()
{
    return color;
}

void setAge(int age)
{
    this->age = age;
}

int getAge()
{
    return age;
}

void setName()
{
    int i = randomGeneration(POSSIBLE_NAMES);
    name = possibleNames[i];
}

string getName()
{
    return name;
}

void printBunny()
{
    cout << "Name: " << getName() << endl;
    cout << "Sex: " << getSex() << endl;
    cout << "Color: " << getColor() << endl;
    cout << "Age: " << getAge() << endl;
}
};

int main()
{

vector< Bunny > colony;

cout << "Welcome to Bunny Graduation!" << endl << endl;

for( int i = 0; i < 5; ++i )
{
    colony.push_back( Bunny() );
}

for( int i = 0; i < 5; ++i )
{
    colony[ i ].printBunny();
    cout << endl;
}

return 0;
}
#包括
#包括
#包括
使用名称空间std;
常量int可能的_名称=18;
常量int可能的颜色=4;
静态标准::字符串可能名称[]={
“珍”,
“亚历克斯”,
“珍妮丝”,
“汤姆”,
“鲍勃”,
“卡西”,
“路易斯”,
“弗兰克”,
“臭虫”,
“达菲”,
“米奇”,
“米妮”,
“冥王星”,
“维纳斯”,
“托潘加”,
“科里”,
“弗朗西斯”,
“伦敦”,
};
静态标准::字符串可能颜色[]={
“白色”,
“棕色”,
“黑色”,
“斑点”
};
结构兔
{
公众:
字符串名;
智力年龄;
字符串颜色;
性别;
兔子(){
setSex();
setColor();
设置(0);
setName();
}
int随机生成(int x){
返回rand()%x;
}
void setSex()
{
int randomNumber=随机生成(2);
(随机数==1)?性别='m':性别='f';
}
char getSex()
{
回归性;
}
void setColor()
{
int randomNumber=随机生成(可能的颜色);
颜色=可能的颜色[随机数];
}
字符串getColor()
{
返回颜色;
}
无效设置(整数年龄)
{
这个->年龄=年龄;
}
int getAge()
{
回归年龄;
}
void setName()
{
int i=随机生成(可能的名称);
名称=可能的名称[i];
}
字符串getName()
{
返回名称;
}
void printBunny()
{

你到底搞不懂什么?你期望什么?你从来没有分配(或初始化)
sex
,所以它包含不确定的值(并且
color
被初始化为空字符串)@KerrekSB我不明白为什么
name:
没有输出任何东西。我本来希望它返回name的值。但是
name
的值会是什么。您没有在代码中的某个地方指定名称。是的,name的返回值是正确的,这是一个空字符串。
Bunny(字符串名称、字符串颜色、整数年龄)
-这个构造函数没有被调用。@ErvinSzilagyi我希望
name
的值从
possibleNames
数组返回一个值。我会在方法
setName
的内部调用这个构造函数吗?那么,这些方法如何知道
struct Bunny
内部的变量呢?@rilethomas我不知道nt的意思是,但你应该知道构造函数是什么。当你在向量中插入一个新的兔子时(
colony.push_back(bunny());
),如果调用了
bunny
(此代码:
bunny(){setex();setColor();setAge(0);setName();}
)构造函数执行setter。方法如,
setex
setName
getName
setName
等。我觉得这些方法属于结构之外,但当我将它们放在结构之外时,我收到一些错误,声称没有声明方法和变量。我有一个构造函数,但我认为我混淆了变量的使用,而不是方法。你应该看一些教程/阅读关于C++中的OOP的一些书。对你以前的问题的回答,设置者应该是成员函数,否则他们不知道他们为哪些变量设置了兔子。
#include <iostream>
#include <ctime>
#include <vector>

using namespace std;

const int  POSSIBLE_NAMES = 18;
const int  POSSIBLE_COLORS = 4;

static std::string possibleNames[] ={
    "Jen",
    "Alex",
    "Janice",
    "Tom",
    "Bob",
    "Cassie",
    "Louis",
    "Frank",
    "Bugs",
    "Daffy",
    "Mickey",
    "Minnie",
    "Pluto",
    "Venus",
    "Topanga",
    "Corey",
    "Francis",
    "London",
};
static std::string possibleColors[] ={

    "White",
    "Brown",
    "Black",
    "Spotted"
};

struct Bunny
{
public:

string name;
int age;
string color;
char sex;

Bunny(){
    setSex();
    setColor();
    setAge(0);
    setName();
}

int randomGeneration(int x){
    return rand() % x;
}

void setSex()
{
    int randomNumber = randomGeneration(2);

    ( randomNumber == 1 ) ? sex = 'm' : sex = 'f';
}

char getSex()
{
    return sex;
}

void setColor()
{
    int randomNumber = randomGeneration(POSSIBLE_COLORS);
    color = possibleColors[randomNumber];
}

string getColor()
{
    return color;
}

void setAge(int age)
{
    this->age = age;
}

int getAge()
{
    return age;
}

void setName()
{
    int i = randomGeneration(POSSIBLE_NAMES);
    name = possibleNames[i];
}

string getName()
{
    return name;
}

void printBunny()
{
    cout << "Name: " << getName() << endl;
    cout << "Sex: " << getSex() << endl;
    cout << "Color: " << getColor() << endl;
    cout << "Age: " << getAge() << endl;
}
};

int main()
{

vector< Bunny > colony;

cout << "Welcome to Bunny Graduation!" << endl << endl;

for( int i = 0; i < 5; ++i )
{
    colony.push_back( Bunny() );
}

for( int i = 0; i < 5; ++i )
{
    colony[ i ].printBunny();
    cout << endl;
}

return 0;
}