0

I'm working on a problem from class. It's about creating an "addressType" class, and using it to interact with a main function that's already written out. The program doesn't work, and the main error I get is "addressType.cpp:6:7: error: redefinition of 'addressType' class addressType {". Why does it think I'm redeclaring the class? I guess I'm just not familiar enough with C++ syntax. I'm really inexperienced with C++, I was a Java guy.

Here's a sample of my .h file code...

#ifndef ADDRESSTYPE_H_INCLUDED
#define ADDRESSTYPE_H_INCLUDED

#include <string>
#include <cstring>
#include <iostream>

using namespace std;

class addressType {

private:
  string address;
  string city;
  string state;
  int zipcode;

public:
  addressType();

  addressType(string inputAddress, string inputCity, string inputState, int inputZipcode);

  void setAddress(string inputAddress);

  void setCity (string inputCity);

//more functions i didn't paste
}; #endif

and so on. Here's a sample of the .cpp:

#include "addressType.h"
#include <iostream>

using namespace std;

class addressType {

private:
  string address;
  string city;
  string state;
  int zipcode;

public
  addressType() {
    address = "aa";
    city = "aa";
    state = "XX";
    zipcode = 10000;
  }

  addressType(string inputAddress, string inputCity, string inputState, int inputZipcode) {
    address = inputAddress;
    city = inputCity;
    setState(inputState);
    setZipcode(inputZipcode);
  }

  void setAddress(string inputAddress) {
    address = inputAddress;
  }
  //more functions I didn't paste
};

The main message I get is: addressType.cpp:6:7: error: redefinition of 'addressType' class addressType.

eduherminio
  • 1,514
  • 1
  • 15
  • 31
IanIce
  • 1
  • 1
  • I see two classes called addressType – nicomp Sep 16 '19 at 23:25
  • 1
    `class addressType {...` in `my.h` and `class addressType {...` in the `.cpp` file. That's a redefinition if I've ever seen one. Leave the one in the header and simply create an instance of the class in the `.cpp` file. – David C. Rankin Sep 16 '19 at 23:27
  • 1
    As far as I know, `#endif` not at the start of a line (only after blanks) won't works. – Phil1970 Sep 17 '19 at 01:40

3 Answers3

1

You need to do two things:

  • get rid of the definition of addressType in the .cpp file completely

  • prefix the methods that make up the class with addressType::, e.g. void addressType::setAddress(string inputAddress) {...

In that way, you define the class only once (which is a requirement, in any single translation unit), and you tell the compiler to which class the method bodies belong.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
0

Why does it think I'm redeclaring the class?

Because you are.

More specifically though, redeclaration is not a problem. Redefinition is the problem. The first definition is in the addressType.h file. You include this file into "the .cpp" and then redefine addressType. This violates the one definition rule.

The solution is to not define the class more than once.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

@Paul Sanders answered it for you, but just to show you what he is saying:

.h

#pragma once

#include <string>

class AddressType {

private:
  std::string address;
  std::string city;
  std::string state;
  int zipcode;

public:
  AddressType();
  AddressType(std::string inputAddress, std::string inputCity, std::string inputState, int inputZipcode);
};

.cpp

#include "add.h"

using namespace std;

AddressType::AddressType()
{
  address = "aa";
  city = "aa";
  state = "XX";
  zipcode = 10000;
}

AddressType::AddressType(string inputAddress, string inputCity, string inputState, int inputZipcode)
  : address(inputAddress)
  , city(inputCity)
  , state(inputState)
  , zipcode(inputZipcode)
{
  // no-op
}

Also unrelated to your question, a suggestion, avoid using namespace in your headers: "using namespace" in c++ headers

Mason
  • 71
  • 4