... = new std::string[size] giving me problems

Started by
4 comments, last by jake_Ghost 18 years, 5 months ago
Ok, i guess i have to two problems here and took the more expensive route to solve it i guess, but it doesnt matter cause it is only done once at start up. I have these string arrays that keep commands in them, but i used vector strings to store the data cause i dont know how many commands for each thing there are. So i send al lthis off to a function that stores the values, but unfortunatly, i dont know how to convert vector data into a string array, so i did this

	std::string *commandclick = new std::string[CommandDown.size()];
	std::string *commanddown = new std::string[CommandDown.size()];
	std::string *commandup = new std::string[CommandUp.size()];
	std::string *commandover = new std::string[CommandOver.size()];

	for (i = 0; i < CommandClick.size(); i++)
	{
		commandclick = CommandClick;
	}

	for (i = 0; i < CommandDown.size(); i++)
	{
		commanddown = CommandDown;
	}

	for (i = 0; i < CommandUp.size(); i++)
	{
		commandup = CommandUp;
	}

	for (i = 0; i < CommandClick.size(); i++)
	{
		commandover = CommandOver;
	}

	button->SetCommands(commandclick, CommandClick.size(),commanddown, CommandDown.size(),commandup, 
						CommandUp.size(),commandover, CommandOver.size());

	delete commandclick;
	delete commanddown;
	delete commandup;
	delete commandover;

Incase you need to see the whole function here it is...

int cWindow :: AddButton(cButtonA atr, std::string Filename)
{
	if (c_button.size() > MAXBUTTONS)
	{
		return LIMITEXCEEDED;
	}

	std::ostringstream oss;
	int i = 0;
	cButton *button = new cButton();

	oss << Filename << atr.Name << ".dat";

	std::fstream File(oss.str().c_str(), std::ios::in);
	std::string Input = "";
	std::vector<std::string> CommandClick;
	std::vector<std::string> CommandUp;
	std::vector<std::string> CommandDown;
	std::vector<std::string> CommandOver;
	std::string CommandNone = "none";

	if (File.is_open())
	{	
		while(std::getline(File, Input))
		{
			if (Input == "::Click")
			{
				while (std::getline(File, Input))
				{
					if (Input == "::EndClick")
					{
						break;
					}

					CommandClick.push_back(Input);
				}
			}
			else if (Input == "::Up")
			{
				while (std::getline(File, Input))
				{
					if (Input == "::EndUp")
					{
						break;
					}

					CommandUp.push_back(Input);
				}
			}
			else if (Input == "::Down")
			{
				while (std::getline(File, Input))
				{
					if (Input == "::EndDown")
					{
						break;
					}

					CommandDown.push_back(Input);
				}
			}
			else if (Input == "::Over")
			{
				while (std::getline(File, Input))
				{
					if (Input == "::EndOver")
					{
						break;
					}

					CommandOver.push_back(Input);
				}
			}
		}

		if (CommandClick.size() == 0)
		{
			CommandClick.push_back(CommandNone);
		}

		if (CommandDown.size() == 0)
		{
			CommandDown.push_back(CommandNone);
		}

		if (CommandUp.size() == 0)
		{
			CommandUp.push_back(CommandNone);
		}

		if (CommandOver.size() == 0)
		{
			CommandOver.push_back(CommandNone);
		}
	}

	std::string *commandclick = new std::string[CommandDown.size()];
	std::string *commanddown = new std::string[CommandDown.size()];
	std::string *commandup = new std::string[CommandUp.size()];
	std::string *commandover = new std::string[CommandOver.size()];

	for (i = 0; i < CommandClick.size(); i++)
	{
		commandclick = CommandClick;
	}

	for (i = 0; i < CommandDown.size(); i++)
	{
		commanddown = CommandDown;
	}

	for (i = 0; i < CommandUp.size(); i++)
	{
		commandup = CommandUp;
	}

	for (i = 0; i < CommandClick.size(); i++)
	{
		commandover = CommandOver;
	}

	button->SetCommands(commandclick, CommandClick.size(),commanddown, CommandDown.size(),commandup, 
						CommandUp.size(),commandover, CommandOver.size());

	delete commandclick;
	delete commanddown;
	delete commandup;
	delete commandover;


	button->SetName(atr.Name);
	button->SetCaption(atr.Caption);
	button->SetX(atr.X);
	button->SetY(atr.Y);
	button->SetWidth(atr.Width);
	button->SetHeight(atr.Height);
	
	if (!button->SetUp(atr.ButtonUp))
	{
		return FAILURE;
	}

	if (!button->SetDown(atr.ButtonDown))
	{
		return FAILURE;
	}

	if (!button->SetHighlight(atr.ButtonHighlight))
	{
		return FAILURE;
	}

	if (!button->SetExtraA(atr.ButtonExtraA))
	{
		return FAILURE;
	}

	if (!button->SetExtraB(atr.ButtonExtraB))
	{
		return FAILURE;
	}
	
	if (!button->SetDisabledT(atr.ButtonDisabled))
	{
		return FAILURE;
	}

	button->SetDisabled(atr.Disabled);
	button->SetVisible(atr.Visible);
	button->SetFontSize(atr.FontSize);
	button->SetFontColour(atr.FontColour);

	c_button.push_back(button);

	Write(c_button[0]->GetName());

	return SUCCESS;
}

heres the SetCommands function

void cButton :: SetCommands(std::string CommandClick[], int NumCommandClick, std::string CommandDown[], int NumCommandDown, 
		                    std::string CommandUp[], int NumCommandUp, std::string CommandOver[], int NumCommandOver)
{
	int i = 0;

	cprs_CommandClick_I = new std::string[NumCommandClick];

	for (i = 0; i < NumCommandClick; i++)
	{
		cprs_CommandClick_I = CommandClick;
	}

	cprs_CommandDown_I = new std::string[NumCommandDown];

	for (i = 0; i < NumCommandDown; i++)
	{
		cprs_CommandDown_I = CommandDown;
	}

	cprs_CommandUp_I = new std::string[NumCommandUp];

	for (i = 0; i < NumCommandUp; i++)
	{
		cprs_CommandUp_I = CommandUp;
	}

	cprs_CommandOver_I = new std::string[NumCommandOver];

	for (i = 0; i < NumCommandOver; i++)
	{
		cprs_CommandOver_I = CommandOver;
	}
}

This is there error i get... Debug assertoin failed! File: dbgdel.cpp line 51 expression: _BLOCK_TYPE_IS_VALID(pHead->nblockUse) Thanks in advance Jake
Advertisement
std::string *commandclick = new std::string[CommandDown.size()];

CM
lol guess i should take a brake, cant believe i missed that, but it still doesnt seem to work...

	std::string *commandclick = new std::string[CommandClick.size()];	std::string *commanddown = new std::string[CommandDown.size()];	std::string *commandup = new std::string[CommandUp.size()];	std::string *commandover = new std::string[CommandOver.size()];	for (i = 0; i < CommandClick.size(); i++)	{		commandclick = CommandClick;	}	for (i = 0; i < CommandDown.size(); i++)	{		commanddown = CommandDown;	}	for (i = 0; i < CommandUp.size(); i++)	{		commandup = CommandUp;	}	for (i = 0; i < CommandOver.size(); i++)	{		commandover = CommandOver;	}	button->SetCommands(commandclick, CommandClick.size(),commanddown, CommandDown.size(),commandup, 						CommandUp.size(),commandover, CommandOver.size());	delete commandclick;	delete commanddown;	delete commandup;	delete commandover;


Jake
No no no...

std::vector's store their data in contiguous memory...

vector<string> v;

v.push_back( "hey" );
v.push_back( "hi" );
v.push_back( "how's it going?" );

if you want a "string array", or a pointer to a bunch of string objects...

std::string* s_array = &v[0];

//s_array[0] is a std::string representing "hey"
//s_array[1] is a std::string representing "hi"
//s_array[2] is a std::string representing "how's it going?"


As an aside, you used new[], but you didn't use delete[]

And you should rewrite your function to not take string arrays as arguments - take a vector of strings - why not?

void cButton :: SetCommands( const std::vector<std::string>& CommandClick, const std::vector<std::string>& CommandDown, const std::vector<std::string>& CommandUp, const std::vector<std::string>& CommandOver )


your cButton should use vector<string> if that's really what's needed for each member (the members like cprs_CommandDown_I, which appear to be string*)

Then it's just a matter of cprs_CommandDown_I = CommandDown; and that will copy the entire vector<string> for you. The vectors are passed by const reference to enjoy not copying them on the function call


Thx for the advice, time to change it. I guess i used a string for some reason, who knows?

Jake
Alrite changed all that stuff, its working now, thx again.

Jake

This topic is closed to new replies.

Advertisement