| | |
Getting error when calling function from main()
![]() |
•
•
Join Date: May 2008
Posts: 46
Reputation:
Solved Threads: 0
Hi, I am trying to write an address book. The problem is: in my class declaration I declared a mutator.
In the implementation, I am asking the user to enter his address, city, state and zip.
So my prototype void set_add(string, string, string, int).
In other words I am trying to set the address, city, state, and zip variables to the one entered by the user. To implement this I am using reference.
But no matter what I put in as arguments while calling from main, I keep getting the error: variable 'variable' not declared.
Here is my code for clarity:
Thanks.
In the implementation, I am asking the user to enter his address, city, state and zip.
So my prototype void set_add(string, string, string, int).
In other words I am trying to set the address, city, state, and zip variables to the one entered by the user. To implement this I am using reference.
But no matter what I put in as arguments while calling from main, I keep getting the error: variable 'variable' not declared.
Here is my code for clarity:
/******************Header**************************/
#ifndef addressType_H
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class addressType
{
private:
string St_address;
string city, state;
int Zip;
public:
addressType(string, string, string, int);//default constructor;
void show_add();//accessor
void set_add(string&, string&, string&, int&);//mutator
};
#endif
/************Implementation*********************/
#include "addressType.h"
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
addressType::addressType(string street, string cit, string st, int zip)
{
street = "9803 Summer Breeze Dr.";
St_address = street;
cit = "Pearland";
city = cit;
st = "Tx";
state = st;
zip = 77584;
Zip = zip;
}
void addressType::show_add()
{
cout<<St_address<<endl;
cout<<city<<","<<state<<endl;
cout<<Zip<<endl;
return;
}
void addressType::set_add(string &street, string &cit, string &st, int &zip)
{
cout<<"Enter street address."<<endl;
cin>>street;
cout<<"Enter your city."<<endl;
cin>>cit;
cout<<"Enter your state."<<endl;
cin>>st;
cout<<"Enter the zip code."<<endl;
cin>>zip;
return;
}
/**********************main()******************/
#include "addressType.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
addressType Avi("9803 Summer Breeze", "Pearland", "Tx", 77584);
Avi.show_add();
Avi.set_add(St_address, city, state, Zip);//what will be my arguments here that would set the values to the one that user inputs?
return 0;
}Thanks.
Last edited by robgeek; Dec 13th, 2008 at 4:50 pm.
•
•
Join Date: Feb 2008
Posts: 627
Reputation:
Solved Threads: 46
it looks like your set_add function is expecting you to pass it some values, but then you are reading them in from cin anyway, but then not doing anything with them.
Maybe you want something more like this:
[code]
void addressType::set_add()
{
cout<<"Enter street address."<<endl;
string street;
cin>>street;
St_address = street;
cout<<"Enter your city."<<endl;
string cit;
cin>>cit;
city = cit;
cout<<"Enter your state."<<endl;
string st;
cin>>st;
state = st;
cout<<"Enter the zip code."<<endl;
cin>>zip;
return;
}
Dave
Maybe you want something more like this:
[code]
void addressType::set_add()
{
cout<<"Enter street address."<<endl;
string street;
cin>>street;
St_address = street;
cout<<"Enter your city."<<endl;
string cit;
cin>>cit;
city = cit;
cout<<"Enter your state."<<endl;
string st;
cin>>st;
state = st;
cout<<"Enter the zip code."<<endl;
cin>>zip;
return;
}
Dave
You are passing in to set_add variable that don't exist yet! In the beginning of your main, declare them like this:
Although your code was sort of hard to understand, the prototype for set_add said it was a mutator. But the way you wrote it, the object is never modified. If your goal was to set the object's values to user input, daviddoria's solution is the one to choose.
c++ Syntax (Toggle Plain Text)
string St_address; string city, state; int Zip;
•
•
Join Date: May 2008
Posts: 46
Reputation:
Solved Threads: 0
Thanks guys. Davidoria's method worked out. But I am confused. Why would not using reference as I did earlier is not working.
From what I understood from my course, is I am sending the reference value of street to St_address and I am manipulating the street variable which should change the St_address variable as well.
And then I was trying to call the St_address in the main().
Please explain where am I going wrong.
Thanks.
From what I understood from my course, is I am sending the reference value of street to St_address and I am manipulating the street variable which should change the St_address variable as well.
And then I was trying to call the St_address in the main().
Please explain where am I going wrong.
Thanks.
•
•
Join Date: Feb 2008
Posts: 627
Reputation:
Solved Threads: 46
passing by reference is when you want to do pass a variable to a function that you want to take a new value and be used back in the calling function, ie
In this case, we pass MyInt to Function by reference, then we can use MyInt back in the calling function.
In you're case, you are trying to set the member variables of the class using the mutator function, so there is no need to pass or return anything!
Dave
C++ Syntax (Toggle Plain Text)
int MyInt; Function(MyInt); cout << MyInt;
In this case, we pass MyInt to Function by reference, then we can use MyInt back in the calling function.
In you're case, you are trying to set the member variables of the class using the mutator function, so there is no need to pass or return anything!
Dave
![]() |
Similar Threads
- Error when calling a method of dynamic array (C++)
- problem with redirecting error from function (Shell Scripting)
- help with some error codes please. (C)
- help with creating and calling a function (C++)
- Failing to link my own function (C++)
- Calling function in a child window from main parent window (C#)
- queue implementation error (C++)
Other Threads in the C++ Forum
- Previous Thread: Help ! C++ Compiler Not WOrking
- Next Thread: Change fucntion homework help.
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets





