Click here to Skip to main content
15,884,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this class named String and i have made it so it has a variable called data which is a std::string. I have also done the appropriate stuff so you can do String hey = "hello"; and it assigns "hello" to data. I know this part of it works. But when i try to make an array of Strings like this:

C++
String a = "hello"; //works perfectly
cout << a.data; //prints "hello"
String arr[3];
arr[0] = a; //Crashes, tells me std::out_of_range

This is the only thing showing up in the command prompt:
terminate called after throwing an instance of 'std::out_of_range'
  what(): basic_string::erase __pos (Which is 4294967295) > this->size()


I have overloaded the operator[] but this should not be a problem, they even did it with the std::string class.

As per request here is the first couple of lines of code:
C++
#define cstr std::string
class String {
public:
    cstr data;
    String() {}
    String(char *str) : data(str) {}
    String(const char *str) {
      data = const_cast<char*>(str);
    }

    #pragma region operators
    String& operator=(char* str) {
        if (RemoveLastChar().data != str) {
            data = str + '\0';
        }
        return *this;
    }

    String& operator=(const char* str) {
        if (RemoveLastChar().data != str) {
            data = (char*)str + '\0';
        }
        return *this;
    }

    String& operator=(String& str) {
        if (RemoveLastChar().data != str.data) {
            data = str.data + '\0';
        }
        return *this;
    }

    String& operator=(const String& str) {
        if (RemoveLastChar().data != str.data) {
            data = str.data + '\0';
        }
        return *this;
    }

    char operator[](int index) { //I also tried commenting this out, did not work
        return data[index];
    }
    #pragma endregion


I have continued trying and have found out where the problem is by commenting out sections and running through it.
C++
String hey = "Hello World!"; //This works
String* h = hey.Split(' '); //This works
String p = h[0]; //This causes the CMD to go blank and crash within a second or two. This is how you access array elements no? I have removed the [] operator and still no difference, this specific array seemingly doesn't work like every single array in the history of C++. Why? I don't understand i have been coding for years. What have i missed? Having a full on identity crisis here. xD


What I have tried:

I have tried assigning directly like arr[0] = "hello"; same problem. I have no idea why this is a problem at all.
Posted
Updated 5-Dec-21 11:56am
v4
Comments
k5054 5-Dec-21 11:41am    
You will need to provide the code for the String class, otherwise we cannot know. There might be anything in there that's messing up the array assignment. Does you assignment operator work for non array instances? As you know from your earlier question the code String a = "hello" does not call the operator=() method, but a constructor. So prove to yourself, at least, that assignment works for your String class. If basic assignment works, then come back here and post the code for your String class and maybe we can help you.
Member 14769677 5-Dec-21 11:52am    
The code is 1200 lines xD But i can give you the operator assignment code.
Richard MacCutchan 5-Dec-21 12:15pm    
Check what your RemoveLastChar method is doing when data does not contain any text.
Richard MacCutchan 5-Dec-21 12:17pm    
There is no need to end all your questions with "WTF". Spending some time with the debugger would be a better use of your time.
Member 14769677 5-Dec-21 13:51pm    
I have updated the question with more information

1 solution

I'd have to agree with Richard a take a look at what RemoveLastChar is doing. If I strip your String class down to the bare essentials needed for your question, then things work fine
C++
#include <iostream>
#include <string>

class C {
public:
	std::string data;
	
	C(const char* str) : data(str) {}
	C() = default;
	C& operator=(const char *str) {
		data = str;
		return *this;
	}
	C& operator=(const C& other) {
		data = other.data;
		return *this;
	}
};

int main()
{
	C a = "Hello";
	std::cout << "a = '" << a.data << "'\n";

	a = "Goodbye";
	std::cout << "a ='" << a.data << "'\n";

	C b[3];
	b[0] = a;
	std::cout << "b[0] = '" << b[0].data << "'\n";

	b[1] = "foobar";
	std::cout << "b[1] ='" << b[1].data << "'\n";
}
which produces the expected output, and does not crash
a = 'Hello'
a ='Goodbye'
b[0] = 'Goodbye'
b[1] ='foobar'
you could try a version of your class without the tests using RemoveLastChar() function call and see if that works better. You'll then know if RemoveLastChar() is the culprit or not. If not, then its probably time to delve into the debugger.
 
Share this answer
 
Comments
Member 14769677 5-Dec-21 18:39pm    
I fixed it, i had to replace String ret[s] with String* ret = new String[s]; now i just have to worry about memory management. Damn it. Well it doesn't crash but it doesn't seem to actually do the values either, it literally just gives me an array of empty strings back, as when i print them it gives me nothing. But the program works. Why is C++ so unexplainably difficult jesus christ...
Member 14769677 5-Dec-21 18:45pm    
Like how is String hey = "Hello"; not the EXACT same as doing String b[3]; b[0] = "Hello";??? to me that should literally be like doing String b_0 = "Hello" which would work perfectly. This language is annoying.
k5054 6-Dec-21 10:31am    
Because String hey = "Hello" is NOT an assignment. The compiler treats this as construction with an initial value. I.E. the compiler treats this as String hey{"Hello"}. Perhaps if you were to construct, then assign a non-array String using your original code, you would also get an error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900