Need help with a basic c++ code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

class Contact
{
 private:

  char Name[50],Num[10];

    //operator overloading
  friend istream& operator >> (istream &in, Contact &str)
  {   cout<<"enter contact information"<<endl;
      in>>str.Name>>str.Num;
      return in;
  }

  friend ostream& operator << (ostream &out, Contact &str)
  {
      cout<<"contact details are:\n";
      out<<str.Name<<"\n"<<str.Num;
      return out;
  }
 public:
     //set and get methods
    void AddContact(Contact S)
    { cout<<" -------------------------------------\n";
      cout<<"|      enter contact details          |\n";
      cout<<" -------------------------------------\n";

      cin>>S.Name>>S.Num;
    }
    void DisplayContact(Contact D)
    { cout<<" -------------------------------------\n";
      cout<<"|      contact details are:           |\n";
      cout<<" -------------------------------------\n";

      cout<<D.Name<<D.Num;
    }
};



class AddressBook
{
 private:

 static int n;
 Contact Rec[n];

 AddressBook()
 {
     n=1;
 }
 public:


 void Add()
 {

     Rec[n].AddContact(Rec[n]);
     Choice();
 }
 void Choice()
 {
     char c;
     cout<<"do you want to enter another contact?\n enter 'y' for yes and 'n' for no"<<endl;
     cin>>c;
     if(c=='y')
     {
       n++;
       Add();
     }
     else Display();

 }
 void Display()
 {
   Contact Rec[n];
   Rec[n].DisplayContact(Rec[n]);
 }

};


int main()
{
    AddressBook a;
    a.Add();
}




the errors I get are:

D:\C++ Project\test.cpp|50|error: data member may not have variably modified type `Contact[((unsigned int)((int)AddressBook::n))]'|
D:\C++ Project\test.cpp||In member function `void AddressBook::Add()':|
D:\C++ Project\test.cpp|62|error: `Rec' undeclared (first use this function)|
D:\C++ Project\test.cpp|62|error: (Each undeclared identifier is reported only once for each function it appears in.)|
D:\C++ Project\test.cpp||In function `int main()':|
D:\C++ Project\test.cpp|53|error: `AddressBook::AddressBook()' is private|
D:\C++ Project\test.cpp|89|error: within this context|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|


where am I going wrong? what do I need to do?
Last edited on
this is your issue:
1
2
static int n;
 Contact Rec[n];

have a read:
http://stackoverflow.com/questions/4057948/initializing-a-member-array-in-constructor-initializer
http://stackoverflow.com/questions/11610338/how-to-initialise-a-member-array-of-class-in-the-constructor

edit: it would be nice if you could use code tags when you post code so people can read it more easily.
Last edited on
I'm sorry, I'm a complete newbie, couldn't understand with those examples, could you please tell me what I could do to make my code work?
This one might be easier to understand

http://en.cppreference.com/w/cpp/language/static

Topic archived. No new replies allowed.