0

I had a class name address:

    #ifndef __ADRESS
#define __ADRESS

#include <iostream>
using namespace std;

class Adress
{
private:
    std::string street;
    int number;
    std::string city;

public:

    Adress(std::string name = "Hertzel", int number = 1, std::string city = "Tel Aviv"); //C'tor
    Adress(const Adress& other); //cpy c'tor
    Adress(Adress&& other);//Move c'tor

                           //Get methods
    const string& getStreet() const;
    int getNumber() const;
    const string& getCity() const;

    //Set methods
    bool setStreet(std::string streetName);
    bool setNumber(int num);
    bool setCity(std::string cityName);

    const Adress& operator=(const Adress& other);
    friend ostream& operator<<(ostream& os, const Adress& adress);

};

#endif // !__ADRESS

The street and city were originally char* and now I changed it to strings. But now I have a weird issue. While using char* I managed to use operator<< function inorder to print the content of address, now after switching to string instead char* when I try printing an address the program terminates.

This is the implementation I wrote for the function:

    ostream& operator<<(ostream& os, const Adress& adress)
{
    os << adress.street << " " << adress.number << " " << adress.city;
    return os;
}

Is anyone familiar with that problem? Thanks!

sharon
  • 21
  • 2
  • 5
    It means you have a bug in your program. The bug isn't in `operator<<` which looks absolutely fine. It's somewhere in the code you haven't posted. – john Jun 08 '20 at 20:55
  • 4
    Please provide a [mre], the problem is probably elsewhere in your program. You should be able to delete your move and copy constructors and assignment operators now that you aren't using `char*`, if these are implemented incorrectly they may be the cause of your problem – Alan Birtles Jun 08 '20 at 20:57
  • in your constructor prototype, you name your string name, instead of street. What's up with that? – Mikdore Jun 08 '20 at 20:57
  • 1
    You should almost certainly remove `Adress(const Adress& other);` and `Adress(Adress&& other);` and `const Adress& operator=(const Adress& other);` from your code. One of the points about using `std::string` is that you no longer have to define the copy constructor, assignment operator etc. The default versions do the right thing. – john Jun 08 '20 at 20:58
  • Unrelated: You probably don't want the returned `Address&` to be `const` in `const Adress& operator=(const Adress& other);` – Ted Lyngmo Jun 08 '20 at 21:03
  • Unrelated: Be careful with underscores. [Sometimes they mean something](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). For example, never use two underscores in a row unless you are writing code for a compiler or a Standard Library implementation. – user4581301 Jun 08 '20 at 21:04
  • Don't use [reserved identifiers](https://en.cppreference.com/w/cpp/language/identifiers) such as `__ADRESS`. It can result in weird errors. – eesiraed Jun 08 '20 at 21:16

1 Answers1

0

The error appears to be elsewhere. You didn't give any implementation file so I had to fill in a dummy implementation for the setters and I can't reproduce the error.

#ifndef ADDRESS_H
#define ADDRESS_H

#include <ostream>
#include <string>

class Adress{
private:
   std::string street;
   int number; //can this be negative or zero
   std::string city;

public:
   ~Adress() = default;
    Adress() : street("Hertzel"), number(1), city("Tel Aviv"){}
    Adress(const Adress& other) = default;
    Adress(Adress&& other) = default;
    Adress& operator=(const Adress& other) = default;

   //Get methods
   const std::string& getStreet() const { return street; }
   int getNumber() const { return number; }
   const std::string& getCity() const{ return city; }

   //Set methods
   bool setStreet(std::string streetName) {
     //check empty, is_alphanum etc
     street = streetName;
     return true;
   }

   bool setNumber(int num){
    //check zero negative
    number = num;
    return true;
  }
  bool setCity(std::string cityName){
    //checks here
    city = cityName;
    return true;
  }

  friend std::ostream& operator << (std::ostream& os, const Adress& adress);

};

inline std::ostream& operator << (std::ostream& os, const Adress& adress){
  os << adress.street << " " << adress.number << " " << adress.city;
  return os;
}

#endif

main

#include <iostream>
#include "address.hpp"


int main(int, char**){
  Adress a;
  std::cout << a << '\n';
}

Compiled with gcc-10 seems fine. Debugger time it appears

systemcpro
  • 856
  • 1
  • 7
  • 15