2

Hi guys I want to make an array of class objects....so that I can keep on creating as many objects during runtime as and when required I wrote the following code, but its giving me error:

class contact{
public:
    string name;//ALL CLASS VARIABLES ARE PUBLIC
    int phonenumber;
    string address;

    contact(){//Constructor
        name= NULL;
        phonenumber= NULL;
        address= NULL;
    }

    void input_contact_name(string s){//function to take contact name
        name=s;
    }
    void input_contact_number(int x){//function to take contact number
        phonenumber=x;
    }
    void input_contact_address(string add){//function to take contact address
        address=add;
    }
}

int main(){
    contact *d;
    d= new contact[200];
    string name,add;
    int choice;//Variable for switch statement
    int phno;
    static int i=0;//i is declared as a static int variable
    bool flag=false;
    cout<<"\tWelcome to the phone Directory\n";//Welcome Message
    cout<<"Select :\n1.Add New Contact\n2.Update Existing Contact\n3.Delete an Existing Entry\n4.Display All Contacts\n5.Search for a contact\n6.Exit PhoneBook\n\n\n";//Display all options
    cin>>choice;//Input Choice from user
    while(!flag){//While Loop Starts
        switch(choice){//Switch Loop Starts
        case 1:
            cout<<"\nEnter The Name\n";
            cin>>name;
            d[i]->name=name;
            cout<<"\nEnter the Phone Number\n";
            cin>>phno;
            d[i]->input_contact_number(phno);
            cout<<"\nEnter the address\n";
            cin>>add;
            d[i]->input_contact_address(add);
            i++;
            flag=true;
        }
    }
    return 0;
}

Please can some one out figure out the reason?? Thanks in advance

3
  • here's the error, code has been indented Commented Apr 15, 2011 at 9:26
  • what is the error you are getting? have you considered using std::vector? Commented Apr 15, 2011 at 9:26
  • newpro.cpp: In constructor ‘contact::contact()’: newpro.cpp:16: error: ambiguous overload for ‘operator=’ in ‘((contact*)this)->contact::name = 0’ newpro.cpp: At global scope: newpro.cpp:7: error: new types may not be defined in a return type newpro.cpp:7: note: (perhaps a semicolon is missing after the definition of ‘contact’) newpro.cpp:32: error: two or more data types in declaration of ‘main’ Commented Apr 15, 2011 at 9:28

2 Answers 2

6

Many problems:

  1. Missing semicolon on the closing brace of the class, as maverik noted
  2. Use of string without using namespace std; or using std::string; (or #include <string> for that matter)
  3. Ditto #2 for cin and cout (and <iostream>)
  4. d[i]-> is wrong; d[i] is a contact&, not a contact*, so use . instead of ->
  5. name= NULL; and address= NULL; are nonsensical -- string is not a pointer type, and already default-constructs to empty
  6. phonenumber= NULL; is technically valid, but still 'wrong'

Also, good lord, use some whitespace in your code.

EDIT (in response to comment): Your constructor should look like:

contact() : phonenumber() { }
Sign up to request clarification or add additional context in comments.

4 Comments

so what should assign these to in the constrictor?\
@Frustrated Coder : Edited to answer that.
phonenumber is a variable not a function
@Frustrated Coder : Yep, but that's not a function call, that's a constructor initialization list + value-initialization. But, if it makes more sense to you, contact() : phonenumber(0) { } is fine too.
1

You forget the ;

class contact {
    ...
};  // <- ; is neccessary

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.