please help with class(.h) file

\\I have no idea what I'm doing wrong
#include <iostream>
using namespace std;

class Address{
private:
string streetName, city, state;
int zip, streetNumber;
public:
void setStreetNumber(int);
void setZip(int);
void setNames(string, string, string );
int getStreetNumber();
int getZip();
char getStreetName();
char getCity();
char getState();

};
void Address::setStreetNumber(int a){
streetNumber = a;
}
void Address::setZip(int a){
zip = a;
}
void Address::setNames( string sn, string s, string c){
streetName = sn;
state = s ;
city = c ;
}
int getStreetNumber(){
return streetNumber;
}
int Address::getZip(){
return zip;
}
string Address::getStreetName(){
return streetName;
}
string Address::getCity(){
return city;
}
string Address::getState(){
return state;
}

Last edited on
Please use code tags.

With code tags, this:
[code]
int Address::getZip(){
int zip;
return zip;
}
[/code]

Becomes this:
1
2
3
4
int Address::getZip(){
    int zip;
    return zip;
}


Much easier to read.

Anyway, in this code snippit, you are declaring a variable named 'zip' on line 2. This will create a new variable with that name. Note that this means you now have two seperate variables with the same name: The one that's part of the class, and the one that you just created which is local to this function.

Note that the local zip is uninitialized and so it will contain garbage.

When you return zip you are not returning the 'zip' that is part of the class, but instead are returning the local zip. You want to return the class's zip.


The solution here is to not declare a new variable in this function. You do not need or want it.
thanks
sorry about the code tags I didn't understand it before

1
2
int Address::getZip(){
          return zip;

I did what you said but now its saying that zip was not declared in scope
I do not get that error when I compile the code in your edited post.

Here are the errors I get when compiling (See my comments):

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 <iostream>
using namespace std;

class Address{
private:
    string streetName, city, state;
    int zip, streetNumber;
public:
    void setStreetNumber(int);
    void setZip(int);
    void setNames(string, string, string );
    int getStreetNumber();
    int getZip();
    char getStreetName();       // <- these 3 functions all return 'char' when you probably
    char getCity();             //   want them to return a string
    char getState();

};
void Address::setStreetNumber(int a){
    streetNumber = a;
}
void Address::setZip(int a){
    zip = a;
}
void Address::setNames( string sn, string s, string c){
    streetName = sn;
    state = s ;
    city = c ;
}
int getStreetNumber(){          // <- This is a global function named 'getStreetNumber', and is not
    return streetNumber;        //  part of your Address class.  (Notice, you are missing the :: operator to
}                               //  make it part of the class)
int Address::getZip(){
    return zip;
}
string Address::getStreetName(){
    return streetName;
}
string Address::getCity(){
    return city;
}
string Address::getState(){
    return state;
}


After fixing those, it compiles without error.
THANKYOU SO MUCH, and for your time to
I cant believe I didn't notice that
God bless
Topic archived. No new replies allowed.