Jump to content
  • Advertisement
Sign in to follow this  
Lordimm

Class Problem

This topic is 3556 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to make a text-based RPG, and I've put in a class for items that you can use, and also made a class for healing items inherited from the item class. But for some reason, when I try to set values for different healing items (like for potions), it doesn't work. I'm still new to classes, so I might be overlooking something. Here's what I have: Database file:
// Includes all the items, scrolls, weapons, enemies, etc.

#include "Library.h"

// Make copies for each type of healing item

void Database()
{	
	HEAL bread;

	void bread.SetHeal(int amount)
	{
		amount = 10;
	}
}

Not too big, I haven't put in all the items I want for now because I need to get this to work. The different objects also need a SetPrice function, and a SetName, but again, I need this to work first before I go on. Here's the C++ header file and cpp file for the item class. Header:

// Item class

class ITEM
{
public:
	char name[32];
	int price;
	char newName;

	void SetName(char* newName);
	char* GetName();
	int GetPrice();
	void SetPrice(int amount);
};

class HEAL: public ITEM
{
public:
	int heal;

	int GetHeal();
	void SetHeal(int amount);
};

The item cpp file:

#include "Library.h"

void ITEM::SetName(char* newName)
{
	strcpy_s(name, newName);
}

char* ITEM::GetName()
{
	return name;
}

int ITEM::GetPrice()
{
	return price;
}

void ITEM::SetPrice(int amount)
{
	price = amount;
}

int HEAL::GetHeal()
{
	return heal;
}

void HEAL::SetHeal(int amount)
{
	heal = amount;
}

Library also includes the item header file. The errors I get upon building it is: c:\documents and settings\owner\my documents\visual studio 2008\projects\world of magic\world of magic\database.cpp(12) : error C2143: syntax error : missing ';' before '.' c:\documents and settings\owner\my documents\visual studio 2008\projects\world of magic\world of magic\database.cpp(12) : error C2182: 'bread' : illegal use of type 'void' c:\documents and settings\owner\my documents\visual studio 2008\projects\world of magic\world of magic\database.cpp(12) : error C2371: 'bread' : redefinition; different basic types c:\documents and settings\owner\my documents\visual studio 2008\projects\world of magic\world of magic\database.cpp(10) : see declaration of 'bread' c:\documents and settings\owner\my documents\visual studio 2008\projects\world of magic\world of magic\database.cpp(13) : error C2143: syntax error : missing ';' before '{' c:\documents and settings\owner\my documents\visual studio 2008\projects\world of magic\world of magic\database.cpp(14) : error C2065: 'amount' : undeclared identifier All of these are in the database file. What's wrong with it?

Share this post


Link to post
Share on other sites
Advertisement
HEAL bread;
bread.SetHeal(10);

makes the errors:
c:\documents and settings\owner\my documents\visual studio 2008\projects\world of magic\world of magic\database.cpp(9) : error C2143: syntax error : missing ';' before '.'
c:\documents and settings\owner\my documents\visual studio 2008\projects\world of magic\world of magic\database.cpp(9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\owner\my documents\visual studio 2008\projects\world of magic\world of magic\database.cpp(9) : error C2371: 'bread' : redefinition; different basic types
c:\documents and settings\owner\my documents\visual studio 2008\projects\world of magic\world of magic\database.cpp(8) : see declaration of 'bread'

Share this post


Link to post
Share on other sites
It is confused because it doesn't know what HEAL is. Are you including the header file for the HEAL class in the cpp file where you Database function lies?

Share this post


Link to post
Share on other sites
I am including the library file in the database file, which the library file includes the item header file.

Share this post


Link to post
Share on other sites
Hmmmmm. Would you mind posting the Database() code again, as you now have it? Also, would you mind showing me what Library.h looks like?

Share this post


Link to post
Share on other sites
Alright, database() is this:

// Includes all the items, scrolls, weapons, enemies, etc.
#include "Library.h"

// Make copies for each type of healing item

HEAL bread;
bread.SetHeal(10);



And library is this:


#include <iostream>
#include <conio.h>

using namespace std;

#include "Item.h"

Share this post


Link to post
Share on other sites
I'm wondering if the problem is due to no include guards.

Try putting either "#pragma once" at the top of Library.h or wrap your header with the following:

#ifndef LIBRARY_H
#define LIBRARY_H

class Item
...
...

#endif

A problem occurs when two separate files include a shared file. This causes the same code to be processed twice. After the first file is processed, a redefinition error occurs with any other file including the same thing. By using "#pragma once" or the other method, it won't allow the same classes to be defined again. I see that you are including Library.h in two different cpp files, so this might be causing your problem.

Share this post


Link to post
Share on other sites
I'm sorry, I may have inadvertently misled you. When I said that those two lines are the only lines that should be in Database, I meant the Database function, not the Database file. The Database file should look something like this:


// Includes all the items, scrolls, weapons, enemies, etc.
#include "Library.h"

// Make copies for each type of healing item

void Database
{
HEAL bread;
bread.SetHeal(10);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!