2

can someone tell me how can i add data into my list of class with C++ please ?

#include <string>
class Person
{
private: 
    std::string Name;
public:
    Person();
    ~Person();
    std::string GetName()
    {
        return Name;
    }
    std::string SetName(std::string name)
    {
        name = Name;
        return name;
    }
}; 

void main()
{
    list<Person> lp = new list<Person>();
    Person p = new Person();
    p.Name = "Smith";
    lp.insert(p);
}

this is my attempt. Can someone correct me or give me another answer ? Thanks

Smiith
  • 21
  • 1
  • 2
  • 7
  • 1
    Your dynamic allocation isn't needed in this code. Lose *both* `new` invokes. (And `main` returns, `int`, btw). – WhozCraig May 03 '15 at 23:28
  • so just like this : list lp; Person p; ??????? – Smiith May 03 '15 at 23:31
  • 1
    yes, though you also need `#include ` for all of this. Honestly you probably need more than Stack Overflow is going to offer. This isn't *really* a language tutorial site. May I suggest you peruse [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – WhozCraig May 03 '15 at 23:34
  • @FalconUA: there is a lot more you could fix in this post... – Jongware May 03 '15 at 23:46

4 Answers4

1

If by some chance you are using c++11 or greater, consider using:

list.emplace_back() 

or

list.emplace_front()

which handles construction and insertion. Of course this would be even better if you had a Person constructor that took a string argument.

Person::Person(std::string& name) { Name = name; }

In which case you could do:

lp.emplace_back("Smith");
mrthotep
  • 21
  • 3
0

list.insert() takes an iterator, i.e. position at which you want to insert the new element.

You can also do

list.push_back(p);

This inserts 'p' at the end of the list.

If you do

list.push_front(p);

insertion happens at the head of list.

KalyanS
  • 527
  • 3
  • 8
0
  • First of all, since you're coding in c++ your main function should return an int:

    int main()
    
  • Second, you're using the new keyword on an object that isn't a pointer, correct that:

    // This would perhaps be better:
    list<Person*> lp;
    Person *p = new Person; // remember to delete p; later
    
  • I don't see that you have included <list> header and using std. You need that in order to use list. Or at least write std::list or using std::list;

  • You are trying to assign to a private class member, either declare it public, or use a setter function:

    p->Name = "Smith"; // remember for pointers, use the '->' or *(p).Name
    
  • emplace_back() is maybe what you are looking to use (since c++11):

    lp.emplace_back(p);
    // otherwise, specify an iterator as first argument for insert:
    // lp.insert(lp.end(), p);
    
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
-1

Why isn't your SetName only void and needs to return something? If you want to return the new Name, you also should have

Name = name;
return Name;

You have switched it.

Also, you don't need any of the new and every main function should return int value (usually 0 if no error occurred).

FIXED CODE:

#include <string>
#include <list>

class Person
{
private: 
    std::string Name;
public:
    //Person();
    //~Person();
    std::string GetName()
    {
        return Name;
    }
    void SetName(std::string name)
    {
    Name = name;
    }
};

int main()
{
    std::list<Person> lp;
    Person p;
    p.SetName("Smith");
    lp.push_back(p);
    return 0;
}

You need to include lists in order to use them, and you can't assign a private member directly. Also, your declarations are wrong - for this I would recommend some cpp tutorials and reading the documentation.

Adalee
  • 528
  • 12
  • 26
  • thank you for your help ! but the issue is still remaining. i don't know how to add data into list of class with c++ – Smiith May 03 '15 at 23:34