简体   繁体   中英

Cout isnt printing string name(No errors are showing)

Why this code doesn't print the name? I defined the name in the Animals constructor class,but when i`m running the code it just ignore the definition.

Animal class:

#pragma once

#include <vector>
#include <string>

class Animals
{
private:

    std::string name;

    std::vector<int> disgust = {1,3,5};
    std::vector <int> sleepTime = { 1,3,5 };
    std::vector <int> childs = { 1,2,3 };
    std::vector<Animals> theAnimals;

    int disgustBar;
    int sleepBar;
    int animalTotal;

    bool reproduce;

public:
    Animals(std::string name);
    ~Animals();

    void feeding(int i);
    void sleeping(int i);
    void description();
};

Animals.cpp :

#include "stdafx.h"
#include "Animals.h"
#include <iostream>

Animals::Animals(std::string name)
{
    disgustBar = 7;
    sleepBar = 7;
}

Animals::~Animals()
{
}

void Animals::feeding(int i)
{
    disgustBar += i;
    return;
}

void Animals::sleeping(int i)
{
    sleepBar += i;
    return;
}

void Animals::description()
{
    std::cout << "The animal name is " + name  << std::endl;
}

Main:

#include "stdafx.h"
#include "Animals.h"
#include <string>
#include <iostream>

int main()
{
    Animals a("Allahu");
    a.description();

    return 0;
}

(This is my very first post,sorry if i made any kind of mistake)

You simply forgot to initialize the name member, do this:

Animals::Animals(std::string name) 
     : name(name) // <- init name
{
    disgustBar = 7;
    sleepBar = 7;    
}

Suggestion 1: Initialize all members using the constructor initialization list :

Animals::Animals(std::string name) 
     : name(name), disgustBar(7), sleepBar(7)
{}

Related: Constructor initialization Vs assignment

A note about identifier names:

   : name(name)
//     ^    ^
//     |    +---- Constructor argument
//     +--------- Class member

Suggestion 2: To avoid confusion, I would use a different attribute name for the class member. For example ' name_ ' (underscore at the end).

Suggestion 3: As mentioned by @Biffen, enable all warnings and do not ignore warnings.

Compiling your code with g++ -Wall -Wextra shows the following warning:

 Animals.cpp:7:1: warning: unused parameter 'name' [-Wunused-parameter] Animals::Animals(std::string name) ^ 

Your constructor needs to initialize the data member name :

Animals::Animals(std::string n){
    disgustBar = 7;
    sleepBar = 7;
    name = n;
    //---^
}

or your constructor could be modified to initialize all the data members, like so:

Animals::Animals(std::string n)
   : disgustBar(7), sleepBar(7), name(n) 
{ // body of the constructor }

To avoid future similar problems include a function that test the validity of your input (ie if all data members are initialized to reasonable values) in the body of the constructor.


Note: your data member animalTotal may be best declared as static and incremented in the body of the constructor (decremented in the body of the destructor), if the intention is to count the number of all the instantiated Animals objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM