Array of String doesn't work properly

Hello.

I've made my own Array and String class.

The GArray (of a given type) accepts, as costructor parameter, a size

1
2
3
4
5
6
7
8
9
10
template<typename GType>
class GArray
{
   GType* array_type = nullptr;
   int size = 0;
   explicit GArray(int _Size)
   {
       size = _Size;
       array_type = new GType[size];
   }


The GString accepts, as costructor parameter, a C-Style string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class GString
{
private:
	char* mainString;
	int size;
public:
        GString(const char* CstyleString);
};

###

GString(const char* CstyleString)
{
	// +1 for the NULL character at the end (/0)
	size = strlen(CstyleString);
	mainString = new char[size+1];

	int i = 0;
	// -1 because we want to set the NULL character AFTER the copy of the character from one vector to another
	for (i = 0; i < size; i++)
		mainString[i] = CstyleString[i];

	mainString[i] = '\0';
}


The problem comes in the main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	// GArray of 1 GString... doesn't seem to have the \0 character at the end
	GArray<GString> strs(1);
	strs[0] = "test";
	cout << strs[0] << endl;

	// Simple GString... gets printed out correctly
	GString test = "test";
	cout << test;

	_getch();
	return 0;
}


The first cout prints "test" and after that it comes a random character, like: "testC", "test:".

But as you can see, in the constructor of GString, I do put a \0 at the end of the string.

In the second cout i can see the string without random characters...

It's like there's a problem with GString AND GArray.

Screenshot: http://i.imgur.com/Xn0gkHQ.png




I don't see how this could compile. This strs[0] is an attempt to use the subscript operator [] on the GArray<GString> object, but no such operator support has been written.

You also have no support for the << operator. I'm not convinced that this code compiles, so I've no idea what you're running to get the output that you see.
Last edited on
I actually overloaded those operators!

GArray
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
GType& operator[](int Index)
{
	if (Index >= 0 && Index < size)
		return array_type[Index];
}

const GType& operator[](int Index) const
{
	if (Index >= 0 && Index < size)
		return array_type[Index];
}

friend std::ostream& operator<<(std::ostream& s, const GArray<GType>& other)
{
	for (int i = 0; i < other.Size(); i++)
	{
		std::cout << other[i] << std::endl;
	}

	return s;
}



GString
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
char& GString::operator[](int Index)
{
	return mainString[Index];
}

const char& GString::operator[](int Index) const
{
	return mainString[Index];
}

GString& GString::operator=(const GString & other)
{
	size = other.size;

	delete[] mainString;
	mainString = new char[size];

	for (int i = 0; i < size; i++)
	{
		mainString[i] = other.mainString[i];
	}

	return *this;
}
Last edited on
I see no support for the << operator for GString, required here:
cout << test;

We could go round and round like this forever. Just put all the code up.
Last edited on
Here it is:
1
2
3
4
5
std::ostream& operator<<(std::ostream & s, const GString & other)
{
	std::cout << other.mainString;
	return s;
}
Your operator [] functions can complete without returning anything.

At least once you try to call the non-existent function Size() rather than the member size. Basically, what you have shown should repeatedly not compile.

I cannot see your screen. I cannot see the code you're trying to compile.

I have no idea what code you really are running, because the code you've shown does not compile.
Last edited on
That's not the full code. I tried to take some parts of it and place them here.

If you want the full code of the GArray and GString, here it is: https://github.com/gedamial/GedamialLibrary/tree/master/Data
In the constructor GArray(int) you feed the value NULL to the GString(const char*) constructor which requires a non-null pointer. The constructor dereferences that pointer resulting in undefined behavior.

Since GArray is meant to hold an arbitrary data type, why would you ever assign the pointer-value NULL to an element? Furthermore, why are you mixing the use of NULL and nullptr? Prefer the latter.

1
2
3
4
    explicit GArray(int size) 
        : size(size), array_type(new GType[size]()) // <- note the extra parenthesis that value-initialize elements of an array.
    {
    }


And you should probably also change the constructor of GString to properly handle a nullptr value.

[edit: Also, your GString::operator=(const GString&) does not ensure the copied-to GString is 0-terminated. (Coincidentally, neither does the copy constructor.)]
Last edited on
@cire Before posting that code on GitHub, I deleted the part on the GArray constructor in which I assign NULL value to all the elements

But I still get an extra character in the first COUT

Last edited on
See the edit in my previous post.
Oh gosh, what a stupid.

There was the problem!
Thanks!
Last edited on
Topic archived. No new replies allowed.