Search code examples
c++undefined-referencestdstring

Class and object error, undefined reference, string has no member named


Hy, I try to learn to use classes and objects in c++, I made this code:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

class plant

{
    public:
        plant();

    void setWater(int vz)
    {
        water = vz;
    }

    void setFood(int tp)
    {
        food = tp;
    }

    int getWater()
    {
        return water;
    }

    int getFood()
    {
        return food;
    }

private:
int water, food;
};

void plantCreate(string name[])
{
    for(int i=0;i<sizeof(name);++i)
    {
        plant name[i];  // undefined reference to `plant::plant()' 
        name[i].setWater(rand()%20);
        name[i].setFood(rand()%20);
    }
}

void plantPrint(string name[])
{
    for(int i=0;i<sizeof(name);++i)
    {
        cout << name[i] << ": Water= " << name[i].getWater() << ", Food= " << name[i].getFood() << endl;
        //error: ‘std::string’ has no member named ‘getWater’/'getFood'
    }
}

int main()
{
    string plantNames[5]={"Liliom", "Tulipan", "Narcisz", "Rozsa", "Pipacs"};
    plantCreate(plantNames);
    plantPrint(plantNames);
}

I get those two errors, first the string member, than the undefined reference if I comment out the cout from the plantPrint I get the second error. I tired to create the default constructor with plant(); but it'sc just not working correctly. Any ideas? (I'm using a codeBlocks on Ubuntu)


Solution

  • For the second error //error: ‘std::string’ has no member named ‘getWater’/'getFood':

    You are tryng to call getWater and getFood on a string, in facts, your name variable is an array of string, and I think you want an array of plant.

    Edit: A correct PlantCreate function:

    plantCreate(plant[] plantlist, string name[], int length)
    {
        for(int i=0;i<length;i++)
        {
            planlist[i].setName(name[i]);  
            planlist[i].setWater(rand()%20);
            planlist[i].setFood(rand()%20);
        }
        return results;
    }
    

    Like that plantCreate will set your array of multiple plant instances.

    Now a compatible plantPrint function with our plantCreate function:

      void plantPrint(plant[] plantlist, int length)
        {
            for(int i=0;i<length;++i)
            {
                cout << plantlist[i].getName() << ": Water= " << plantlist[i].getWater() << ", Food= " << plantlist[i].getFood() << endl;
            }
        }
    

    And finally a compatible main with our functions:

    int main()
    {
        string plantNames[5]={"Liliom", "Tulipan", "Narcisz", "Rozsa", "Pipacs"};
        plant plantList[5];
        plantCreate(plantList, plantNames, 5);
        plantPrint(plantList,5);
    }
    

    Like that this might work. But sinces my c++ is a bit rusted, there can be some errors but you will be able to correct them. Also, maybe you should re-learn how instancation works, how types and variables work etc... :)

    Moreover be careful when you use sizeof on an array, I dont think it returns the length of the array.