-2
struct rec
{
   char * id;
   char firstname[15];
   char lastname[15];
   int a[10];
   struct rec* prev;
   struct rec* next;
};

So I start out with this being my structure, I convert this portion

class rec
{
   private:
       char * id;
       char firstname[15];
       char lastname[15];
       int a[10];
       rec* prev;
       rec* next;
}
        rec::rec(const rec&)

Constructor seems to keep giving issues, I am on the verge of figuring it out, deleted code so it would not be copied

  • there is little difference between a structure and a class. Actually none apart from the default access specifiers, thus "converting a structure to a class" makes little sense. I know what you mean, but also note that a `struct` has already a default constructor (just as a `class` has one) and you can also add your custom constructor to a `struct` (same as with a `class`) – 463035818_is_not_an_ai Apr 14 '17 at 15:44
  • 3
    Your problem is not obvious from the example you provided. A [MCVE] would be helpful to identify your mistake. – François Andrieux Apr 14 '17 at 15:45
  • 1
    It will be best for you in the long run to go through a C++ textbook and work through the examples. Getting an answer to this particular problem won't be useful in the long run. – R Sahu Apr 14 '17 at 15:45
  • I am having great difficulty and have to use what is available here so I cannot use std:: string firstname – Penny Allison Apr 14 '17 at 15:46
  • ***I keep getting multiple errors*** Copy and paste the text of the errors to your question (no pictures of text please). If this is Visual Studio copy them from the Output Tab instead of the Errors tab. Also you don't have a minimal example. We don't want your whole program just a minimal example that produces your errors and that we can try. – drescherjm Apr 14 '17 at 15:46
  • Asking "why it has to be done in such a way" without mentioning what way that is, is asking a lot. – molbdnilo Apr 14 '17 at 15:46
  • As mentioned above, the only difference between struct and class is the default access -- in a struct it defaults to public, in a class it defaults to private. If you have any code outside the class that's trying to access the private members, errors are to be expected. You either need to declare them public, or write accessor methods and change all the other functions to use those methods. – Barmar Apr 14 '17 at 15:49
  • **I cannot get a grip on how to build a constructor for this particular problem.** That seems to be what your question is really about. What problem are you having with the constructor? Show your attempted constructor, and we can help you get it working. It looks like all you really need to do in the constructor is use `strncpy()` to copy from the input names to the corresponding structure members, and `memcpy` to copy `ans_in`. – Barmar Apr 14 '17 at 15:51

1 Answers1

1

You really need to get a good C++ book and read through it. In the meantime, to get you headed in the right direction, here's some code which should help a bit:

struct rec
{
   std::string id;
   std::string firstname;
   std::string lastname;
   std::vector<int> a;
};
std::vector<rec> records;
records.emplace_back(rec{"123", "John", "Doe", { 1, 2, 3} });

You don't need prev and next as the vector is managing the collection. If you really want to use a linked list, std::list<rec> will do the job.

A Print() routine might look like

void print(const rec& r, std::ostream& os)
{
   os << r.id << ": " < r.lastname << ", " << r.firstname << "\n";
}
Community
  • 1
  • 1
Ðаn
  • 10,934
  • 11
  • 59
  • 95