-2

This is the code for my project which I want to compile. The error is stated at the end of the question.

#include <iostream>
#include <string>
#include <stdio.h>

class substring {
    friend std::ostream& operator<<(std::ostream& output, substring const& sub);

public:
    char *str;
    int length;

    substring();
    ~substring();
    substring(std::string);
    substring(const substring &);

    substring& operator=(substring const& other);
    substring& operator=(std::string const& strz);

    substring& operator+=(substring const& other);

    bool operator>(substring const& other) const;
    bool operator<(substring const& other) const;

    char& operator[](size_t idx);
    char operator[](size_t idx) const;

};

std::ostream& operator<<(std::ostream& output, substring const& sub);
std::istream& operator >> (std::istream& input, substring const& sub);


bool operator==(substring const& one, substring const& another); 
bool operator!=(substring const& one, substring const& another); 

bool operator==(std::string const& str, substring const& sub); 
bool operator==(substring const& sub, std::string const& str);
bool operator!=(std::string const& str, substring const& sub);
bool operator!=(substring const& sub, std::string const& str); 


substring::substring()
{
    length = 0;
}

substring::~substring()
{
    delete str;
}

substring::substring(std::string)
{
}

substring::substring(const substring & sub)
{
    str = new char[length];
}

std::ostream & operator<<(std::ostream & output, substring const & sub)
{
    output << sub;
    return output;
}

std::istream & operator >> (std::istream & input, substring const & sub)
{
    std::cout << "Enter  sub:";
    input >> sub;
    return input;
}

bool operator==(substring const & one, substring const & another)
{
    if (one.length != another.length)
        return false;
    else
    {
        for (int i = 0; i < another.length; i++)
            if (one[i] != another[i])
            {
                return false;
                break;
            }
    }
    return true;
}

bool operator!=(substring const & one, substring const & another)
{
    if (one.length != another.length)
        return true;
    else
    {
        for (int i = 0; i < another.length; i++)
            if (one[i] != another[i])
            {
                return true;
                break;
            }
    }
    return false;
}

bool operator==(std::string const & str, substring const & sub)
{

    if (sub.length != str.length())
        return false;
    else
    {
        for (int i = 0; i < sub.length; i++)
            if (str[i] != sub[i])
            {
                return false;
                break;
            }
    }
    return true;
}

bool operator==(substring const & sub, std::string const & str)
{
    if (str.length() != sub.length)
        return false;
    else
    {
        for (unsigned int i = 0; i < str.length(); i++)
            if (sub[i] != str[i])
            {
                return false;
                break;
            }
    }
    return true;
}

bool operator!=(std::string const & str, substring const & sub)
{
    if (sub.length != str.length())
        return true;
    else
    {
        for (int i = 0; i < sub.length; i++)
            if (str[i] != sub[i])
            {
                return true;
                break;
            }
    }
    return false;
}

bool operator!=(substring const & sub, std::string const & str)
{
    if (sub.length != str.length())
        return true;
    else
    {
        for (int i = 0; i < sub.length; i++)
            if (str[i] != sub[i])
            {
                return true;
                break;
            }
    }
    return false;
}

substring & substring::operator=(substring const & other)
{
    delete str;
    length = other.length;
    str = new char[length];
    for (int i = 0; i<length; i++)
    {
        str[i] = other.str[i];
    }
    return *this;
}

substring & substring::operator=(std::string const & strz)
{
    length = strz.length();
    str = new char[length];
    for (int i = 0; i<length; i++)
    {
        str[i] = strz[i];
    }
    return *this;
}

substring & substring::operator+=(substring const & other)
{
    char* new_str = new char[length + other.length];
    for (int i = 0; i<length; i++)
    {
        new_str[i] = str[i];
    }
    for (int i = length; i<other.length; i++)
    {
        new_str[i] = other.str[i];
    }
    delete str;
    str = new_str;
    return *this;
}

bool substring::operator>(substring const & other) const
{
        return true;
}

bool substring::operator<(substring const & other) const
{
        return true;
}

char & substring::operator[](size_t idx)
{
    return str[idx];
}

char substring::operator[](size_t idx) const
{
    return str[idx];
}

int main()
{
    std::string str = "abc";
    substring sub = str;

    std::cout << sub;

    return 0;
}

The problem is that when I run this code, it seems that the compiler just skips this: substring sub = str;.

I cannot even change this line to substring sub = "aaa"; because it shows an error which says I cannot convert subtring to std::string (although there is an operation overloading for this in the code).

  • 2
    Well, you don't tell it to do anything `substring::substring(std::string){}` – NathanOliver Jun 09 '17 at 13:08
  • Can we see some implementation? – Jonathan Mee Jun 09 '17 at 13:09
  • 1
    ``substring s = ...`` is not an assignment but an initialisation: it call the constructor (you should not initialise value with ``=`` but with the constructor). – nefas Jun 09 '17 at 13:11
  • @NathanOliver What should I write in there? – SaeedZeroOne Jun 09 '17 at 13:15
  • @SaeedZeroOne You need to initialize `str` and `length` – NathanOliver Jun 09 '17 at 13:23
  • @NathanOliver I wrote `substring::substring(std::string s){ length = s.length(); str = new char[length]; }` and it keeps to output only "1". I mean it throws infinite "1"s. Exactly like an infinite loop that couts "1" – SaeedZeroOne Jun 09 '17 at 13:30
  • @SaeedZeroOne You need to copy the contents of `s` into `str`. – NathanOliver Jun 09 '17 at 13:30
  • @NathanOliver Now it is this: `length = s.length(); str = new char[length]; for (int i = 0; i – SaeedZeroOne Jun 09 '17 at 13:33
  • @NathanOliver if you wanna check it out, here it is: [link](http://cpp.sh/9ybof). Just unckeck Warning panel. – SaeedZeroOne Jun 09 '17 at 13:35
  • You never null terminated the string, also your buffer is not big enough to hold that null terminator. Seriously though lets not code by guessing. Get yourself a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn from that. – NathanOliver Jun 09 '17 at 13:35
  • @NathanOliver The code isn't mine. It's a university project and the proffessor just copied the code from a website. I don't write c++ at all :) Can you please fix it for me? – SaeedZeroOne Jun 09 '17 at 13:40

1 Answers1

0

The problem is that when I run this code, it seems that the compiler just skips this: substring sub = str;

The compiler did not "skip it". It compiled succesfully, and the new object was created succesfully. However, the converting constructor that you defined leaves the objects members default initialized:

substring::substring(std::string)
{
}

I cannot even change this line to substring sub = "aaa"; because it shows an error which says I cannot convert subtring to std::string

I highly doubt that. I suspect that you misread.

My compiler says that const char [4] cannot be converted substring

(although there is an operation overloading for this in the code).

There certainly isn't a substring::substring(const char(&)[4]) (nor substring::substring(const char*)).

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • For the first one, what exactly should i write inside the brackets? And for the second and the third parts of the answer, how should I convert const char to string? – SaeedZeroOne Jun 09 '17 at 13:22
  • @SaeedZeroOne I don't know. How should your class behave? You should certainly do something about `str` because it will be deleted in the destructor and deleting an uninitialized pointer has undefined behaviour. – eerorika Jun 09 '17 at 13:23
  • @SaeedZeroOne did you really mean a const char or did you mean const char array? – eerorika Jun 09 '17 at 13:29
  • The copy constructor also does not copy, but that doesn't effect this code at least. – NathanOliver Jun 09 '17 at 13:29