0

I want to use array of objects(in data.h file) to my user.h file .i tried this thing as below but my try is not succeed .i want help from you guys .what is wrong in my script. please correct me

// data.h file

class data
{
    private:
        int id;
        char name;

    public:
        data();    
        int getid()

            {
                return id;
            }

        void setid(int id)
            {
                id = id;
            }

        char getname()

            {
                return name;
            }

        void setname(char name)
            {
                name=name;
            }
    };

//data.cpp file

data::data(){

       int xyz;
       char pqr;
       data firstobject;
       firstobject.setid(10);
       xyz=firstobject.getid();
       firstobject.setname(hello);
       pqr=firstname.getname();
       return 0;
   }

//user.h file

class user
{
    private:


        vector<data> data11;
    public:
        user();

        void add_element(int a)
        {
            data11.push_back(a);
        }

        void get_element()
        {
            for(int x=0; x<data11size(); x++)
            {
                cout<<data11[x]<<" \n";
            };
            cout<<" \n";
        }

};

//user.cpp file

user::user()
{   user object;
    object.add_element(xyz);
    object.get_element();
}
user3213849
  • 11
  • 1
  • 1
  • 6
  • 1
    There's a lot wrong with your code: in `add_element` your trying to add an `int` to a `vector` which won't work. `data` has a private constructor, so can't be created in the first place. `setid` is assigning the `id` to iteself (you proably meant `this->id = id`), `name` has the same problem. `data::data()` uses a variable called `hello` that isn't defined. etc etc etc – Sean Jan 22 '14 at 10:32
  • WE want to hear from you, what's wrong? (Does not compile? Does not run? Doesn't do what's expected?) – zoska Jan 22 '14 at 10:49

2 Answers2

0

This is your constructor and defined as private? If you dont have any specific reason, move it public:

data();

Same with user()

Edit: There are many other things.. explain what problem you are facing exactly

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
0

Are those the complete files? Of course you need to

#include "data.h"  

in user.h

David Zaadstra
  • 111
  • 1
  • 5
  • yes i have included data.h file .my file is not able to compile – user3213849 Jan 22 '14 at 10:55
  • There are some more issues: - A constructor never returns a value, so remove that return 0; - You can't allocate another data object within the constructor. That will cause infinite recursion (and I don't even know if it would compile) – David Zaadstra Jan 22 '14 at 11:09