J
J
jspie2017-05-02 00:12:05
C++ / C#
jspie, 2017-05-02 00:12:05

How to make a method to add an element to an array of a class?

Class header:

typedef  struct {
  char firstname[30];
  char lastname[30];
} WriterData;

class Writers {
public:
  Writers();
  ~Writers();
  void getWriters(const Writers&);
  void insertWriter(char* firstname_writer, char* lastname_writer);
private:
  WriterData* writers; 
  unsigned int count;
};

How to implement the insertWriters() function? The method receives 2 arguments as input. firtsname and lastname from textBox. Can you please explain how to work with an array?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2017-05-02
@jspie

1) Forget about char*/char[] in your task, it's superfluous. There is std::string.
2) WriterData -> Writer. And it's better to just write struct Writer {};
3) The data must be stored in a std::vector (or other container, this issue should be discussed separately), then the count variable is not needed. It should probably be a std::shared_ptr vector.
4) getWriters -> writers, and it should return a vector without taking any arguments and be a const method. Another question is whether this method is needed at all.
5) insertWriter -> addWriter, again accept two std::string const&. It is also possible to return a std::shared_ptr with a newly created writer, which is optional, see the problem.
PS In general, devaloneadvised you to move in the right direction, i.e. to read books on C++ and docks.

D
devalone, 2017-05-02
@devalone

cppstudio.com

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question