Error help
Jul 11, 2017 at 2:15am UTC
Hello! I am still fairly new to C++ and need a bit of help or guidance.
Here is my 3 files of code: Record.h, Record.cpp, and main.cpp;
Record.h
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
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
#ifndef RECORD_H
#define RECORD_H
class Record
{
private :
string recNum;
string firstName;
string lastName;
string age;
string phoneNum;
public :
void setRecNum(string recIn);
void setFirstName(string nameIn);
void setLastName(string nameIn);
void setAge(string ageIn);
void setTelephone(string phoneIn);
string getRecNum();
string getFirstName();
string getLastName();
string getAge();
string getTelephone();
};
#endif /* RECORD_H */
Record.cpp
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
#include "Record.h"
#include <string>
using namespace std;
void Record::setRecNum(string recIn)
{
recNum = recIn;
}
void Record::setFirstName(string nameIn)
{
firstName = nameIn;
}
void Record::setLastName(string nameIn)
{
lastName = nameIn;
}
void Record::setAge(string ageIn)
{
age = ageIn;
}
void Record::setTelephone(string phoneIn)
{
phoneNum = phoneIn;
}
string Record::getRecNum()
{
return recNum;
}
string Record::getFirstName()
{
return firstName;
}
string Record::getLastName()
{
return lastName;
}
string Record::getAge()
{
return age;
}
string Record::getTelephone()
{
return phoneNum;
}
main.cpp
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
#include <cstdlib>
#include <iostream>
#include <string>
#include "Record.h"
using namespace std;
int main()
{
int i = 0;
Record book[i];
for (i = 0; i<10; i++)
{
cout << "Record number: " ;
cin >> book[i].setRecNum();
cout << "First name: " ;
cin >> book[i].setFirstName();
cout << "Last name: " ;
cin >> book[i].setLastName();
cout << "Age: " ;
cin >> book[i].setAge();
cout << "Telephone number: " ;
cin >> book[i].setTelephone();
}
for (i=0; i<10; i++)
{
cout<<book[i].getRecNum();
cout<<book[i].getFirstName();
cout<<book[i].getLastName();
cout<<book[i].getAge();
cout<<book[i].getTelephone();
}
return 0;
}
The problem I am getting is in main.cpp. Error: no matching function for call to 'Record::setRecNum()' Same for all of the other lines dealing with input.
Jul 11, 2017 at 3:11am UTC
it takes a string but you call it with nothing.
book[i].setRecNum();
setRecNum(string recIn);
you need something like
book[i].setRecNum(somestringvariable);
Jul 11, 2017 at 3:17am UTC
Because your members function having 1 argument.
Example: change line 14 with
1 2 3
string x = "" ;
cin >> x ;
book[ i ].setRecNum( x ) ;
Jul 11, 2017 at 3:22am UTC
How would I do that so that a user can input the string variable that goes in the parenthesis of book[I].setRecNum(stringvariablehere);?
Jul 11, 2017 at 3:25am UTC
Posted on delay. :)
Jul 11, 2017 at 3:42am UTC
@ar2007, I tried replacing every cin with what you suggested(but different variables for each such as Record number variable was string r, first name variable was string f, etc. Now my program runs, but once I type in the Record number(the first time) it says run failed. But gives me no error messages.
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
int i = 0;
Record book[i];
for (i = 0; i<10; i++)
{
cout << "Record number: " ;
string r = "" ;
cin >> r;
book[i].setRecNum(r);
cout << "First name: " ;
string f = "" ;
cin >> f;
book[i].setFirstName(f);
cout << "Last name: " ;
string l = "" ;
cin >> l;
book[i].setLastName(l);
cout << "Age: " ;
string a = "" ;
cin >> a;
book[i].setAge(a);
cout << "Telephone number: " ;
string t = "" ;
cin >> t;
book[i].setTelephone(t);
}
If I change Record book[I] to just Record book the program works fine. I don't understand why it isn't working when it is an array...
Last edited on Jul 11, 2017 at 3:51am UTC
Jul 11, 2017 at 5:03am UTC
My full program is now:
Record.h
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
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
#ifndef RECORD_H
#define RECORD_H
class Record
{
private :
string recNum;
string firstName;
string lastName;
string age;
string phoneNum;
public :
Record()
{
}
Record(string recIn) :recNum(recIn)
{
}
Record(string recIn, string fnameIn, string lnameIn, string ageIn, string phoneIn)
:recNum(recIn), firstName(fnameIn), lastName(lnameIn), age(ageIn), phoneNum(phoneIn)
{
}
//set functions
void setRecNum(string recIn);
void setFirstName(string nameIn);
void setLastName(string nameIn);
void setAge(string ageIn);
void setTelephone(string phoneIn);
//get functions
string getRecNum();
string getFirstName();
string getLastName();
string getAge();
string getTelephone();
};
#endif /* RECORD_H */
Record.cpp
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
#include "Record.h"
#include <string>
using namespace std;
void Record::setRecNum(string recIn)
{
recNum = recIn;
}
void Record::setFirstName(string nameIn)
{
firstName = nameIn;
}
void Record::setLastName(string nameIn)
{
lastName = nameIn;
}
void Record::setAge(string ageIn)
{
age = ageIn;
}
void Record::setTelephone(string phoneIn)
{
phoneNum = phoneIn;
}
string Record::getRecNum()
{
return recNum;
}
string Record::getFirstName()
{
return firstName;
}
string Record::getLastName()
{
return lastName;
}
string Record::getAge()
{
return age;
}
string Record::getTelephone()
{
return phoneNum;
}
main.cpp
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 92 93
#include <cstdlib>
#include <iostream>
#include <string>
#include "Record.h"
using namespace std;
class Recordsbook
{
Record book[10];
public :
void Menu()
{
int choice = 0;
cout << "---Main Menu---\n" ;
cout << "1. Input information\n" ;
cout << "2. Display records\n" ;
cout << "3. Exit\n" ;
cin >> choice;
switch (choice)
{
case 1:
InputInfo();
break ;
case 2:
Display();
break ;
case 3:
Exit();
break ;
}
}
void InputInfo()
{
int i = 0;
for (i = 0; i<10; i++)
{
cout << "Record number: " ;
string r = "" ;
cin >> r;
book[i].setRecNum(r);
cout << "First name: " ;
string f = "" ;
cin >> f;
book[i].setFirstName(f);
cout << "Last name: " ;
string l = "" ;
cin >> l;
book[i].setLastName(l);
cout << "Age: " ;
string a = "" ;
cin >> a;
book[i].setAge(a);
cout << "Telephone number: " ;
string t = "" ;
cin >> t;
book[i].setTelephone(t);
}
Menu();
};
void Display()
{
int i = 0;
for (i=0; i<10; i++)
{
cout << "Record number: " ;
cout << book[i].getRecNum();
cout<<"\n" ;
cout<< "First name: " ;
cout<<book[i].getFirstName();
cout<<"\n" ;
cout<< "Last name: " ;
cout<<book[i].getLastName();
cout<<"\n" ;
cout<< "Age: " ;
cout<<book[i].getAge();
cout<<"\n" ;
cout<< "Telephone number: " ;
cout<<book[i].getTelephone();
cout<<"\n" ;
}
Menu();
};
void Exit()
{
cout << "Closing..." ;
};
} Recordsbook;
int main()
{
Recordsbook.Menu();
}
The solution to my previous problem was SUPER simple. I changed Record book[I] to Record book[10].
Thank you all so much for the help! I wouldn't have come up with the solution without y'alls help!
Last edited on Jul 11, 2017 at 5:12am UTC
Topic archived. No new replies allowed.