#include <iostream>
#include <stdlib.h>
usingnamespace std;
struct a {
string name;
int recordnum;
int age;
int birthdate;
string desc;
};
class b {
private:
int c,d,e;
string f;
public:
void addpatient() // here i wanna add full record for patient
{
a add;
int r;
cout<<"enter the the record num"<<endl;
cin>>r;
cout<<"enter name"<<endl;
cin>>add[r].name;
cout<<"enter record num"<<endl;
cin>>add[r].recordnum;
cout<<"enterage"<<endl;
cin>>add[r].age;
cout<<"enter birth"<<endl;
cin>>add[r].birthdate;
cout<<"enter desc"<<endl;
cin>>add[r].desc;
}
void editdesc() // here i wanna edit patient descrption (only) already exist
{
a edit;
int l;
cout<<"enter the the record num"<<endl;
cin>>l;
cout<<"enter desc"<<endl;
cin>>edit[l].desc;
}
}
int main(){
editpatient();
editdesc();
system ("pause");
return 0;
};
the errors
1 2 3 4 5 6 7 8 9 10 11 12 13 14
26 C:\Users\Owner n\Desktop\ddddd\lol.cpp no match for'operator[]' in 'add[r]'
28 C:\Users\Owner n\Desktop\ddddd\lol.cpp no match for'operator[]' in 'add[r]'
30 C:\Users\Owner n\Desktop\ddddd\lol.cpp no match for'operator[]' in 'add[r]'
32 C:\Users\Owner n\Desktop\ddddd\lol.cpp no match for'operator[]' in 'add[r]'
34 C:\Users\Owner n\Desktop\ddddd\lol.cpp no match for'operator[]' in 'add[r]'
34 C:\Users\Owner n\Desktop\ddddd\lol.cpp At global scope:
43 C:\Users\Owner n\Desktop\ddddd\lol.cpp no match for'operator[]' in 'edit[l]'
43 C:\Users\Owner n\Desktop\ddddd\lol.cpp At global scope:
48 C:\Users\Owner n\Desktop\ddddd\lol.cpp new types may not be defined in a return type
48 C:\Users\Owner n\Desktop\ddddd\lol.cpp extraneous `int' ignored
48 C:\Users\Owner n\Desktop\ddddd\lol.cpp `main' must return `int'
49 C:\Users\Owner n\Desktop\ddddd\lol.cpp `editpatient' undeclared (first use this function)
50 C:\Users\Owner n\Desktop\ddddd\lol.cpp `editdesc' undeclared (first use this function)
First of all look at your code carefully before asking question.
1. You haven't made function editpatient
2. You haven't instantiated the class. Without making object class's member functions can't be accessed. you are trying to access editdesc without object
Also look at the error messages you're getting. They will tell you a lot about what is wrong.
26...no match for 'operator[]' in 'add[r]'
You are using both add and edit as arrays. Since you didn't declare either as an array, the compiler is expecting class a to have a [] operator.
In both addpatient and editdesc you ask the use to enter the record number and then use that as a subscript to something that is not an array. Did you intend to have a global array for patient records? If so, you want to keep a global count of the number of records, then in addpatient use that as your index and increment it rather than ask the user.
Your add and edit objects have local scope. i.e. They go away when you exit the respective function. Is that what you intended? You probably want to make them pointers to the global patient record array and set the pointer to the address of the record you want to deal with.
#include <iostream>
#include <stdlib.h>
usingnamespace std;
struct a {
string name;
int recordnum;
int age;
int birthdate;
string desc;
};
class b {
private:
int c,d,e;
string f;
public:
void addpatient() // here i wanna add full record for patient
{
a add[100];
int r;
cout<<"enter the the record num"<<endl;
cin>>r;
cout<<"enter name"<<endl;
cin>>add[r].name;
cout<<"enter record num"<<endl;
cin>>add[r].recordnum;
cout<<"enterage"<<endl;
cin>>add[r].age;
cout<<"enter birth"<<endl;
cin>>add[r].birthdate;
cout<<"enter desc"<<endl;
cin>>add[r].desc;
}
void editdesc() // here i wanna edit patient descrption (only) already exist
{
a edit[100];
int l;
cout<<"enter the the record num"<<endl;
cin>>l;
cout<<"enter desc"<<endl;
cin>>edit[l].desc;
}
}
int main(){
addpatient();
editdesc();
system ("pause");
return 0;
};
the errors
1 2 3 4 5 6 7
48 C:\Users\Owner n\Desktop\ddddd\lol.cpp new types may not be defined in a return type
48 C:\Users\Owner n\Desktop\ddddd\lol.cpp extraneous `int' ignored
48 C:\Users\Owner n\Desktop\ddddd\lol.cpp `main' must return `int'
49 C:\Users\Owner n\Desktop\ddddd\lol.cpp `addpatient' undeclared (first use this function)
50 C:\Users\Owner n\Desktop\ddddd\lol.cpp `editdesc' undeclared (first use this function)
thank you again for the replay
nice forum and nice people i realy like it.
okay, you got the bit about using an index with an array.
However, you missed the point about your arrays being local in scope. i.e. addpatient gets executed once, you enter information into one occurance of add, then exit. The add array is now gone.
Did you intend for some level of persistence of patient information?
#include <iostream>
#include <stdlib.h>
#include <string>
usingnamespace std;
struct patient_record {
string name;
// recordnum is not needed as an element
// int recordnum;
int age;
int birthdate;
string desc;
};
#define MAX_PATIENTS 100
// Global storage for all patients
class DB
{ struct patient_record patients[MAX_PATIENTS];
int num_patients;
private:
int c,d,e;
string f;
public:
DB () // Constructor
{ num_patients = 0;
}
void addpatient() // here i wanna add full record for patient
{ struct patient_record * add;
if (num_patients == MAX_PATIENTS)
{ // error - can't add another patient
}
// Set pointer to the next available record
add = &patients[num_patients];
// Don't need to prompt for the patient number
// cout<<"enter the the record num"<<endl;
// cin>>r;
cout<<"enter name"<<endl;
cin >> add->name;
// Record number is not needed as an element
// cout<<"enter record num"<<endl;
// cin>>add->recordnum;
cout<<"enterage"<<endl;
cin>>add->age;
cout<<"enter birth"<<endl;
cin>>add->birthdate;
cout<<"enter desc"<<endl;
cin>>add->desc;
// Now increment the number patient records
num_patients++;
}
void editdesc() // here i wanna edit patient descrption (only) already exist
{ struct patient_record * edit;
int l;
cout<<"enter the the record num"<<endl;
cin>>l;
if (l > MAX_PATIENTS)
{ // error - number out of range
}
edit = &patients[l];
cout<<"enter desc"<<endl;
cin>>edit->desc;
}
};
// Declare database as global
DB db;
int main()
{ db.addpatient();
db.editdesc();
system ("pause");
return 0;
};
Areas of possible improvement:
1) Add error handling where indicated.
2) Add multiple patients
3) Edit multiple patients
4) Use a vector for patients instead of an array. This will remove the fixed limit.
thank you alot brother for your time , i read all what you wrote but i wanna fix this code first
here no errors only messages dont let the program work
#include <iostream>
#include <stdlib.h>
usingnamespace std;
void addpatient();
void editdesc();
struct a {
string name;
int recordnum;
int age;
int birthdate;
string desc;
};
class b {
private:
int c,d,e;
string f;
public:
b() // Constructor
{
}
void addpatient() // here i wanna add full record for patient
{
a add[100];
int r;
cout<<"enter the the record num"<<endl;
cin>>r;
cout<<"enter name"<<endl;
cin>>add[r].name;
cout<<"enter record num"<<endl;
cin>>add[r].recordnum;
cout<<"enterage"<<endl;
cin>>add[r].age;
cout<<"enter birth"<<endl;
cin>>add[r].birthdate;
cout<<"enter desc"<<endl;
cin>>add[r].desc;
}
void editdesc() // here i wanna edit patient descrption (only) already exist
{
a edit[100];
int l;
cout<<"enter the the record num"<<endl;
cin>>l;
cout<<"enter desc"<<endl;
cin>>edit[l].desc;
}
};
int main(){
addpatient();
editdesc();
system ("pause");
return 0;
};
messages
1 2 3 4
[Linker error] undefined reference to `addpatient()'
[Linker error] undefined reference to `editdesc()'
ld returned 1 exit status
Your linker errors are occuring because you declared addpatient and editdesc as global functions at lines 5 hand 6. However they are written as function members of class b.
As was explained by Bibek Subedi and as I showed in my example, member functions must be called through an object instance. See lines 74 and 75 in my example.
You still haven't fixed the problem with add and edit going out of scope.