Hi all,


I have a class which contains a struct that holds some variables, string, int's.

Now, one of the variables (string) in the struct is used to compare it with a name, if the name is in the struct, it has to be returned from my function with it's own integer variables to be put into a separate list.

The thing is, how is the code written to do so ?

struct ArmorArray
{
	std::string armorName;
	int armorCheck;
	int maxDext;
	int price;

	ArmorArray *nxtArmor;
};

class Armor
{
	private:
	ArmorArray ArmorList[13];
	ArmorArray *p_List;

	int m_SIZEARRAY;

	std::string m_ArmorName;
	std::string m_String;

public:
	Armor();
	~Armor() {}

	p_List BuyArmor();
	std::string CreateString(std::string &myString);
	std::string ArmorBought(std::string &bought);
};
p_List Armor::BuyArmor()
{
	m_ArmorName = CreateString (m_String);

	for (int i = 0; i < m_SIZEARRAY; i++)
	{
		if(!m_ArmorName.compare(ArmorList[i].armorName))
		{
			return ArmorList[i];
		}
	}

	//return m_ArmorName = "Armor does not exist ! Try again or return to previous menu !";
}

Thanks for any assistance.

So, essentially you want to return a pointer to ArmorArray. What's the problem?

Hi Narue,

Well, the problemis that I'm getting these error messages:

[TEXT]Removed error messages[/TEXT]

Yes, seeing as how your code is wrong (you try to use a data member as the return type), it clearly shouldn't compile. But returning a pointer to ArmorArray is quite trivial for you (unless I've misjudged your ability):

#include <string>

struct ArmorArray
{
  std::string armorName;
  int armorCheck;
  int maxDext;
  int price;

  ArmorArray *nxtArmor;
};

class Armor
{
private:
  ArmorArray ArmorList[13];
  ArmorArray *p_List;

  int m_SIZEARRAY;

  std::string m_ArmorName;
  std::string m_String;

public:
  Armor();
  ~Armor() {}

  ArmorArray *BuyArmor();
  std::string CreateString(std::string &myString);
  std::string ArmorBought(std::string &bought);
};

ArmorArray *Armor::BuyArmor()
{
  m_ArmorName = CreateString (m_String);

  for (int i = 0; i < m_SIZEARRAY; i++)
  {
    if(!m_ArmorName.compare(ArmorList[i].armorName))
    {
      return &ArmorList[i];
    }
  }

  return 0;
}

At first I thought you meant you wanted to return a pointer to an array and were struggling with the syntax, which is perfectly understandable.

commented: Thanks for the help, ... again. +4

Darn, sorry Narue. As soon as I saw your correction to my code, I felt stupid :icon_redface:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.