no appropriate default constructor available :(

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 1
Reputation: arussell817 is an unknown quantity at this point 
Solved Threads: 0
arussell817 arussell817 is offline Offline
Newbie Poster

no appropriate default constructor available :(

 
0
  #1
Feb 22nd, 2009
Hello, I looked around and saw similar problems, but None of the answers seemed to help my situation. Im almost done with my HW Just have come across this minor bump.

The error is in the title, and is happening in my addressEntry header, or cpp. not sure: : error C2512: 'addressEntry' : no appropriate default constructor available

its confusing me because I cant find any reason it won't compile. any help would be appreciated.
  1. #include <iostream>
  2. #include <cctype>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. #include "name.h"
  9. #include "address.h"
  10. #include "addressEntry.h"
  11. #include "addrbook.h"
  12.  
  13.  
  14. void askUser(char & option);
  15. void printMenu();
  16. bool isValid(char option);
  17. void optionOne();
  18. void optionThree();
  19.  
  20. int main()
  21. {
  22. Name myName("Andrew", "Russell");
  23. Name operatorName;
  24. Address operatorAddress;
  25. addressEntry operatoraddressEntry;
  26. char option = 0;
  27. char newOption = 0;
  28. char another = 'y';
  29. addrBook stuff;
  30. double itemToAdd;
  31. int counterToRemove = 0;
  32. string itemRemove;
  33. string newName,newLast,newAddress, newCity, newState, newZip, newPhone, newEmail, newBirthday, newPict;
  34.  
  35.  
  36. ofstream myfile ("address.dat");
  37.  
  38.  
  39.  
  40. while(option != '5')
  41. {
  42. myName.printName();
  43.  
  44. askUser(option);
  45.  
  46. if(isValid(option))
  47. cout << "You selected: " << option << endl;
  48. else
  49. cout << "Invalid Entry selected\n";
  50. switch(option)
  51. {
  52. case '1':
  53. cout << "Enter your First Name: ";
  54. cin >> newName;
  55. operatorName.setFirstName(newName);
  56. cout << "Enter your Last Name: ";
  57. cin >> newLast;
  58. operatorName.setLastName(newLast);
  59. cout << "\nPlease Enter your Street Address: ";
  60. if(cin.peek() == '\n')
  61. cin.ignore();
  62. getline(cin, newAddress);
  63. operatorAddress.setStreet(newAddress);
  64. cout << "\nPlease Enter your City: ";
  65. cin >> newCity;
  66. operatorAddress.setCity(newCity);
  67. cout << "\nPlease Enter your State: ";
  68. cin >> newState;
  69. operatorAddress.setState(newState);
  70. cout << "\nPlease Enter your Zip Code: ";
  71. cin >> newZip;
  72. operatorAddress.setZip(newZip);
  73. cout << "\nPlease Enter your Phone Number: ";
  74. if(cin.peek() == '\n')
  75. cin.ignore();
  76. getline(cin, newPhone);
  77. operatoraddressEntry.setPhoneNumber(newPhone);
  78. cout << "\nPlease Entr your Birthday: ";
  79. if(cin.peek() == '\n')
  80. cin.ignore();
  81. getline(cin, newBirthday);
  82. operatoraddressEntry.setBirth(newBirthday);
  83. cout << "\nPlease Enter your Email Address: ";
  84. cin >> newEmail;
  85. operatoraddressEntry.setEmail(newEmail);
  86. cout << "\nPlease Enter your Pict File: ";
  87. cin >> newPict;
  88. operatoraddressEntry.setPict(newPict);
  89.  
  90. stuff.add(operatorName.getFullName() + operatorAddress.getFullAddress() + operatoraddressEntry.getFullInfo());
  91. break;
  92. case '2':
  93. stuff.printUsed();
  94. break;
  95. case '3':
  96. stuff.print();
  97. break;
  98. case '4':
  99. // prompt for counting
  100. // number to remove
  101. stuff.remove(counterToRemove-1);
  102. break;
  103. case '5':
  104. cout << "Goodbye!\n";
  105. break;
  106. default:
  107. cout << "Error";
  108.  
  109.  
  110. }
  111.  
  112. }
  113.  
  114. myfile.close();
  115.  
  116.  
  117. return 0;
  118. }
  119.  
  120.  
  121. void askUser(char & option)
  122. {
  123. printMenu();
  124. cin >> option;
  125. }
  126. void printMenu()
  127. {
  128. cout << "1. Add a new addressEntry to the AddressBook\n";
  129. cout << "2. Find out how many items in the AddressBook\n";
  130. cout << "3. Print out all the items in the AddressBook\n";
  131. cout << "4. Delete an item from the AddressBook\n";
  132. cout << "5. Exit the Program\n\n";
  133. }
  134.  
  135.  
  136. bool isValid(char option)
  137. {
  138. if(option > 53 || option < 49)
  139. return false;
  140. else
  141. return(true);
  142. }
  143.  
  144. void optionOne()
  145. {
  146. string newName,newLast,newAddress, newCity, newState, newZip, newPhone, newEmail, newBirthday;
  147. Name operatorName;
  148. Address operatorAddress;
  149.  
  150. addrBook stuff;
  151.  
  152. cout << "Enter your First Name: ";
  153. cin >> newName;
  154. operatorName.setFirstName(newName);
  155. cout << "Enter your Last Name: ";
  156. cin >> newLast;
  157. operatorName.setLastName(newLast);
  158. cout << "\nPlease Enter your Street Address: ";
  159. if(cin.peek() == '\n')
  160. cin.ignore();
  161. getline(cin, newAddress);
  162. operatorAddress.setStreet(newAddress);
  163. cout << "\nPlease Enter your City: ";
  164. cin >> newCity;
  165. operatorAddress.setCity(newCity);
  166. cout << "\nPlease Enter your State: ";
  167. cin >> newState;
  168. operatorAddress.setState(newState);
  169. cout << "\nPlease Enter your Zip Code: ";
  170. cin >> newZip;
  171. operatorAddress.setZip(newZip);
  172.  
  173.  
  174.  
  175.  
  176. // End of, Input
  177.  
  178. cout << "\n****This is the Information that you Have provided****" << endl << endl;
  179. cout << "Name & Address:\n" << newName << " " << newLast << endl;
  180. cout << newAddress << "\n" << newCity << ", " << newState << " " << newZip << endl;
  181. cout << "Phone Number: " << newPhone << endl;
  182. cout << "Birth Date: " << newBirthday << endl;
  183. cout << "Email: " << newEmail << endl;
  184.  
  185.  
  186.  
  187.  
  188. }
  189.  
  190.  
  191. void optionThree()
  192. {
  193. string line;
  194. ifstream myfile ("address.dat");
  195. cout << "\n\n";
  196. if (myfile.is_open())
  197. {
  198. while (! myfile.eof() )
  199. {
  200. getline (myfile,line);
  201. cout << line << endl;
  202. }
  203. myfile.close();
  204. }
  205.  
  206. else cout << "Unable to open file";
  207. }

Here is the Header and The cpp that are causing the problem.

  1. #ifndef ADDRESSENTRY_H
  2. #define ADDRESSENTRY_H
  3.  
  4. #include <string>
  5.  
  6. class addressEntry
  7. {
  8. public:
  9. addressEntry::addressEntry(string phoneIn, string birthIn, string emailIn, string pictIn);
  10. string addressEntry::getPhoneNumber() const;
  11. string addressEntry::getBirthday() const;
  12. string addressEntry::getEmail() const;
  13. string addressEntry::getPict() const;
  14. string addressEntry::getFullInfo() const;
  15.  
  16. void addressEntry::setPhoneNumber(string phoneIn);
  17. void addressEntry::setBirth(string birthIn);
  18. void addressEntry::setEmail(string emailIn);
  19. void addressEntry::setPict(string pictIn);
  20.  
  21.  
  22.  
  23. private:
  24. string phone;
  25. string birth;
  26. string email;
  27. string pict;
  28. };
  29.  
  30. #endif
  31.  
  32.  
  33.  
  34. #include <iostream>
  35. #include <string>
  36.  
  37. using namespace std;
  38.  
  39. #include "addressEntry.h"
  40.  
  41. addressEntry::addressEntry(string phoneIn, string birthIn, string emailIn, string pictIn)
  42. {
  43. phone = phoneIn;
  44. birth = birthIn;
  45. email = emailIn;
  46. pict = pictIn;
  47. }
  48.  
  49. string addressEntry::getPhoneNumber() const
  50. {
  51. return(phone);
  52. }
  53. string addressEntry::getBirthday() const
  54. {
  55. return(birth);
  56. }
  57. string addressEntry::getEmail() const
  58. {
  59. return(email);
  60. }
  61.  
  62. string addressEntry::getPict() const
  63. {
  64. return(pict);
  65. }
  66.  
  67. string addressEntry::getFullInfo() const
  68. {
  69. return("Phone Number:" + phone + "\n" + email + "\n" + birth + "\n" + pict + "\n");
  70. }
  71.  
  72.  
  73. void addressEntry::setPhoneNumber(string phoneIn)
  74. {
  75. phone = phoneIn;
  76. }
  77. void addressEntry::setBirth(string birthIn)
  78. {
  79. birth = birthIn;
  80. }
  81. void addressEntry::setEmail(string emailIn)
  82. {
  83. email = emailIn;
  84. }
  85. void addressEntry::setPict(string pictIn)
  86. {
  87. pict = pictIn;
  88. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: no appropriate default constructor available :(

 
0
  #2
Feb 23rd, 2009
1. Use code tag properly:
[code=c++]
source(s)
[/code]
It's too hard to answer you without line numbers in formatted (by code tag) source.

2. It's a very typical error. You define a constructor of the class addressEntry:
  1. addressEntry::addressEntry(
  2. string phoneIn,
  3. string birthIn,
  4. string emailIn,
  5. string pictIn);
Therefore you must explicitly define all constructors needed for this class using. You forgot this C++ rule ...

Look at this declaration from the main function:
  1. addressEntry operatoraddressEntry;
Obviously, a default constructor needed (constructor without parameters). No such constructor in your class. Define it - that's all.

3. Try to avoid class name style like addressEntry. Common practice is AddressEntry (see Name and Address in your code).
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 76
Reputation: r.stiltskin is an unknown quantity at this point 
Solved Threads: 9
r.stiltskin r.stiltskin is offline Offline
Junior Poster in Training

Re: no appropriate default constructor available :(

 
0
  #3
Feb 23rd, 2009
The error message is telling you exactly what the problem is. In your main function you are declaring an addressEntry object addressEntry operatoraddressEntry; without supplying any arguments. In order to do that, your addressEntry class must have a default constructor, meaning a constructor that takes no arguments: addressEntry(); But the only constructor in your addressEntry class is this one addressEntry(string phoneIn, string birthIn, string emailIn, string pictIn); which takes 4 arguments.

Just add a default constructor to the class header and implementation files.

By the way, in your header file the "addressEntry::" part of each line is unnecessary. You only need that in the implementation file.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: no appropriate default constructor available :(

 
0
  #4
Feb 23rd, 2009
Look,
Here's the key:
If you do dont provide any constructor to the Class, the compiler generates, automatically, a defalt no-argument constructor.
But, if define even one constructor yourself, the compiler won't generate any constructor for you.
Saying this, I mean that if you defined a 2 argument constructor and no other constructor is defined, you cannot create object without specifying those two arguments. You have two options, either define a dummy no-argument constructor, or use the default arguments in your 2-argument constructor.
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 766 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC