Can't get c++ contact info storing code to compile

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2007
Posts: 5
Reputation: manster is an unknown quantity at this point 
Solved Threads: 0
manster manster is offline Offline
Newbie Poster

Can't get c++ contact info storing code to compile

 
0
  #1
Dec 17th, 2007
I'm stuck because I recieve these errors when I try to compile -

pim.cpp: In method `void PIM::search(class basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >)':
pim.cpp:101: member `firstname' is a private member of class `Person'
pim.cpp:101: member `lastname' is a private member of class `Person'
pim.cpp:105: no matching function for call to `PIM::print (Person &)'
pim.cpp:47: candidates are: PIM::print(char)
pim.cpp: In method `void PIM::save()':
pim.cpp:137: no matching function for call to `Person::save ()'
pim.cpp:112: candidates are: Person::save(ofstream &)
pim.cpp: In method `void PIM::sort(char)':
pim.cpp:166: warning: implicit declaration of function `int isOutOfOrder(...)'
pim.cpp:166: warning: cannot pass objects of type `Person' through `...'
pim.cpp:167: parse error before `{'
pim.cpp:175: confused by earlier errors, bailing out


  1. using namespace std;
  2.  
  3. struct Date
  4. {
  5. int month, day, year;
  6. };
  7.  
  8. struct Address
  9. {
  10. string street, city, state, zipcode;
  11. };
  12.  
  13.  
  14. class Person
  15. {
  16. private:
  17. string firstname, lastname, homephone, mobilephone, email;
  18. Address address;
  19. Date birthdate;
  20. public:
  21. string getFirstName() { return firstname; }
  22. string getLastName() { return lastname; }
  23. void load(ifstream& infile);
  24. void save(ofstream& outfile);
  25. void enter();
  26. void print();
  27. bool isOutOfOrder(Person p, char field);
  28. };
  29.  
  30.  
  31. #define PIM_SIZE 100
  32. class PIM
  33. {
  34. private:
  35. Person contacts[PIM_SIZE];
  36. int numContacts;
  37. void sort(char field);
  38. public:
  39. void load();
  40. void save();
  41. void add(Person p);
  42. void search(string name);
  43. void print(char field);
  44. };
  45.  
  46.  
  47.  
  48. void Person::enter()
  49. {
  50. cin.ignore (100,'\n');
  51. cout << "\nEnter first name: ";
  52. getline(cin, firstname);
  53. cout << "Enter last name: ";
  54. getline(cin, lastname);
  55. cout << "Enter street: ";
  56. getline(cin, address.street);
  57. cout << "Enter city: ";
  58. getline(cin, address.city);
  59. cout << "Enter state: ";
  60. getline(cin, address.state);
  61. cout << "Enter zip code: ";
  62. getline(cin, address.zipcode);
  63. cout << "Enter home phone: ";
  64. getline(cin, homephone);
  65. cout << "Enter mobile phone: ";
  66. getline(cin, mobilephone);
  67. cout << "Enter email: ";
  68. getline(cin, email);
  69. cout << "Enter birth month: ";
  70. cin >> birthdate.month;
  71. cout << "Enter birth day: ";
  72. cin >> birthdate.day;
  73. cout << "Enter birth year: ";
  74. cin >> birthdate.year;
  75. }
  76.  
  77. void Person::print()
  78. {
  79. cout << firstname << " " << lastname << endl;
  80. cout << address.street <<endl;
  81. cout << address.city <<", "
  82. << address.state
  83. << " " << address.zipcode <<endl;
  84. cout << "Home: " << homephone << " "
  85. << "Mobile: " << mobilephone << endl;
  86. cout << email << endl;
  87. cout << "Birthday: " << birthdate.month << "/"
  88. << birthdate.day << "/"
  89. << birthdate.year << endl;
  90. }
  91.  
  92. void PIM::search(string name)
  93. {
  94. int matches = 0;
  95. for (int i = 0; i < numContacts; i++)
  96. {
  97. if ((contacts[i].firstname == name) || (contacts[i].lastname == name))
  98. {
  99. matches++;
  100. cout << "\nContact " << (i + 1) << ":" << endl;
  101. print(contacts[i]);
  102. }
  103. }
  104. cout << "\nTotal number of matching contacts: " << matches << endl;
  105. }
  106.  
  107. void Person::save(ofstream& outfile)
  108. {
  109. outfile << firstname << endl;
  110. outfile << lastname << endl;
  111.  
  112. outfile << address.street <<endl;
  113. outfile << address.city <<endl
  114. << address.state <<endl
  115. << address.zipcode <<endl;
  116. outfile << homephone << endl
  117. << mobilephone << endl;
  118. outfile << email << endl;
  119. outfile << birthdate.month << endl
  120. << birthdate.day << endl
  121. << birthdate.year << endl;
  122.  
  123. }
  124.  
  125. void PIM::save()
  126. {
  127. ofstream outfile;
  128. outfile.open("contacts.txt");
  129. outfile << numContacts << endl;
  130.  
  131. for (int i = 0; i < numContacts; i++)
  132. {
  133. contacts[i].save();
  134. }
  135. outfile.close();
  136. }
  137.  
  138.  
  139. bool Person::isOutOfOrder(Person p, char field)
  140. {
  141. if (field == 'F')
  142. {
  143. return (firstname < p.firstname);
  144. }
  145. else
  146. {
  147. return (lastname < p.lastname);
  148. }
  149. }
  150.  
  151.  
  152. void PIM::sort(char field)
  153. {
  154. Person temp;
  155. int minIndex;
  156.  
  157. for (int i=0; i < numContacts-1; i++)
  158. {
  159. minIndex = i;
  160. for (int j=i+1; j < numContacts; j++)
  161. {
  162. if (isOutOfOrder(contacts[j],field)
  163. {
  164. minIndex=j;
  165. }
  166. }
  167.  
  168. temp = contacts[i];
  169. contacts[i]=contacts[minIndex];
  170. contacts[minIndex]=temp;
  171. }
  172.  
  173. }
  174.  
  175.  
  176. void PIM::print(char field)
  177. {
  178.  
  179. sort(field);
  180.  
  181. for (int i = 0; i < count; i++)
  182. {
  183. cout << "\nContact " << (i + 1) << ":" << endl;
  184. print(pim.contacts[i]);
  185. }
  186. cout << "\nTotal number of contacts: " << count << endl;
  187. }
  188.  
  189. int main()
  190. {
  191. int numContacts = 0;
  192. int arraySize = 100; //always have space for extra 100 contacts
  193. PIM contacts;
  194.  
  195. ifstream infile;
  196. infile.open("contacts.txt");
  197.  
  198. if (!infile.fail())
  199. {
  200. infile >> numContacts;
  201. }
  202.  
  203. contacts[arraySize];
  204.  
  205. if (!infile.fail())
  206. {
  207.  
  208. for (int i = 0; i < numContacts; i++)
  209. {
  210. contacts[i].load;
  211. }
  212. infile.close();
  213. }
  214.  
  215. cout << "Personal information manager\n";
  216. string choice;
  217. int contactNum;
  218.  
  219. do
  220. {
  221. cout << "\nA = Add contact" << endl;
  222. cout << "S = Search for contact" << endl;
  223. cout << "P = Print contacts" << endl;
  224. cout << "X = Exit" << endl;
  225. cout << "\nEnter choice: ";
  226. cin >> choice;
  227. if (choice == "A" || choice == "a" )
  228. {
  229. pim.contacts[numOfContacts] = save();
  230. numContacts++;
  231. }
  232. else if (choice == "S" || choice == "s")
  233. {
  234. cout << "\nEnter first or last name to find: ";
  235. cin >> name;
  236. pim.search();
  237. }
  238. else if (choice == "P" || choice == "p")
  239. {
  240. cout << "\nSort list by [F]irst or [L]ast name: ";
  241. cin >> field;
  242. pim.print(field);
  243. }
  244. else if (choice!= "X" && choice != "x")
  245. {
  246. cout << "Invalide choice!\n";
  247. }
  248. }
  249. while (choice != "X" && choice != "x");
  250.  
  251. saveContacts(contacts, numContacts);
  252. cout << "\nContact information saved.\n";
  253.  
  254. return 0;
  255. }
  256.  
  257. string Person::getFirstName()
  258. {
  259. return firstname;
  260. }
  261.  
  262. string Person::getLastName()
  263. {
  264. return lastname;
  265. }
  266.  
  267. void Person::load(ifstream& infile)
  268. {
  269. infile.ignore();
  270. getline(infile, firstname);
  271. getline(infile, lastname);
  272. getline(infile, address.street);
  273. getline(infile, address.city);
  274. getline(infile, address.state);
  275. getline(infile, address.zipcode);
  276. getline(infile, homephone);
  277. getline(infile, mobilephone);
  278. getline(infile, email);
  279. infile >> birthdate.month;
  280. infile >> birthdate.day;
  281. infile >> birthdate.year;
  282. }
  283.  
  284. [code = language]


Please let me know what to do without changing any of the classes(PIM or Person)
Last edited by manster; Dec 17th, 2007 at 4:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Can't get c++ contact info storing code to compile

 
0
  #2
Dec 17th, 2007
The error messages are pretty clear:

pim.cpp: In method `void PIM::search(class basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >)':
pim.cpp:101: member `firstname' is a private member of class `Person'
pim.cpp:101: member `lastname' is a private member of class `Person'

These mean you cannot directly access the first/last name members, use the get____ methods you created.


pim.cpp:105: no matching function for call to `PIM::print (Person &)'
pim.cpp:47: candidates are: PIM::print(char)

Your PIM::print method take a single char as its argument - should it be taking a Person instead?


pim.cpp: In method `void PIM::save()':
pim.cpp:137: no matching function for call to `Person::save ()'
pim.cpp:112: candidates are: Person::save(ofstream &)

Again, a mismatch of parameters (or lack thereof)


pim.cpp: In method `void PIM::sort(char)':
pim.cpp:166: warning: implicit declaration of function `int isOutOfOrder(...)'
pim.cpp:166: warning: cannot pass objects of type `Person' through `...'

Again, what is (char field) doing as a parameter? Function isOutOfOrder(),needs to be called as a method with a particular person.


pim.cpp:175: confused by earlier errors, bailing out

So would I.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum


Views: 967 | Replies: 1
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC