C++ Classes

I need a little bit of help here. I'm new to C++ Classes.
There are some errors which I do not know how to troubleshoot.

The error of "Declaration terminated incorrectly".
Multiple errors of "Type name expected". Why is that?
Multiple errors of "Declaration missing ;". Why is that?

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

class Address {
  int    houseNumber;
  string street;
  int    apartmentNumber;
  string city;
  string state;
  string zipCode; // e.g., "47405-1234"
  Address(int houseNumber, 
          string street, 
          // no apartmentNumber  
          string city, 
          string state, 
          string zipCode) { 
    this.houseNumber = houseNumber; 
    this.street = street; 
    this.city = city; 
    this.state = state; 
    this.zipCode = zipCode; 
  } 
  Address(int houseNumber, 
          string street, 
          int apartmentNumber, 
          string city, 
          string state, 
          string zipCode) { 
    this(houseNumber, street, city, state, zipCode);
    this.apartmentNumber = apartmentNumber; 
  } 
  void print() {
    System.out.println("Street: "      + street); 
    System.out.println("City: "        + city 
                     + "State: "       + state 
                     + "Postal Code: " + zipCode); 
  } 
  int compareTo(Address a) { 
    // same conventions as for Strings
    return this.zipCode.compareTo(a.zipCode); 
  } 
Last edited on
This looks like Java...?

this is a pointer. Use -> not . e.g. this->street, etc...
this( ... ) dosen't work in C++
look up "initializer list" in c++

You also missed a } closing brace at the end I think.
Last edited on
1
2
3
4
int compareTo(Address a) { 
    // same conventions as for Strings
    return this.zipCode.compareTo(a.zipCode); 
  } 


1) If it isn't a built-in datatype then be sure to pass it in by reference.
2) What is this function supposed to do?

This function takes an Address object as an argument but you are passing in a string. Why is that? And this is a pointer, so the code should look like this.

1
2
3
4
int compareTo(const Address &a) { 
    // same conventions as for Strings
    return this->zipCode.compareTo(a); 
  } 

EDIT: The function is supposed to return an integer. >_> Why is that?

1
2
3
4
void print(void) {
         std::cout << "Street: " << street << "\nCity: " 
    	        << city << "\nState: " << state << "\nPostal Code: " << zipCode;
}

This is C++, not Java. How do you confuse the two? The code above is a valid print function for your class.
Last edited on
Topic archived. No new replies allowed.