Class Help

Started by
5 comments, last by Jammer99 23 years, 1 month ago
I''m getting this weird error about how my constructos can''t return values. I KNOW this, and I have no returns. But it gives me a odd error, and points to my { of the function. Can someone help? Here is the code. NOTE: This is just something I threw together to practice. It was designed to work in a text RPG.
  

#include "troll.h"



troll::troll(int power, apstring description)
{
	apstring	describe=description;
	strength=(rand()%power+3)+1;
	agility=(rand()%power+3)+1;
	health=(rand()%power+2)+1;
	brains=(rand()%power+1)+1;
	currentHealth=health*health;
}

int troll::attack()
{
	return strength*6;
}


void troll::veiwStats()
{
	cout << "Strength:   " << strength << endl;
	cout << "Agility:    " << agility  << endl;
	cout << "Resistance: " << health   << endl;
	cout << "Brains:     " << brains   << endl;
}

int troll::defend()
{
	return health*6;
}


void troll::takeDamage(int damageRecived)
{
	if((damageRecived-defend())>0)
	{
		currentHealth=damageRecived-defend();
		die();
	}
	
}

void troll::die()
{
	if(currentHealth<=0)
	{
		cout << "You have killed the vile beast! Congrats!\n";
	}
}


void troll::look()
{
	cout << describe << endl;
}
  
thanks
Advertisement
Does your class declaration have a semi colon at the end?

Also, what is apstring? is it a macro? is it a type def? if so, to what?

Why do you declare apstring describe = description, but then, not use it anywhere in your constructor?

You should always add a default constructor as well.
The problem is most likely in troll.h.
AlekM: apstring is one of the 5 "AP Classes," designed to make your life with a couple of data structures easier.

The five are:

apstring
apvector
apmatrix
apstack
apqueue

There are all templated (except for, of course, apstring) so you can create an array, matrix, stack. or queue of any data type you pass to the constructor.
"Be that word our sign in parting, bird or fiend!" I shrieked, upstarting —"Get thee back into the tempest and the Night''s Plutonian shore!-just 2 of 96 lines from E.A.P.'s "the Raven"
Here is the source for troll.h
The cplus2nb.h is just a small file with common files i use (inputs, outputs, etc).
  #ifndef _TROLL_H#include <iostream.h>#include "cplus2nb.h"class troll{public:	troll(int, apstring);	~troll();			apstring description,describe;	int power;		int attack();	int defend();	void veiwStats();	void takeDamage(int);	void die();	void look();	private:	int strength;	int brains;	int health;	int agility;	int currentHealth;}#define _TROLL_H#endif[source]  
I''ve never heard of the ap* classes, but C++ already has standard class templates: string, vector, list, map, deque, and a wide variety of algorithms:



#include &ltiostream>
#include &ltvector>
#include &ltstring>
#include &ltlist>

struct mydata { int unused[4]; };

int main(int argc, char* argv)
{
std::string s1 = "Hello";
std::string s2 = "World";
std::string s3 = s1 + " " + s2 + "!"; // make "Hello World!"

// output it
std::cout << s3 << std::endl;

std::vector&ltmydata> v1(256); // array of 256 mydata objects
std::vector&ltint> v2(10); // array of 10 ints

for( int x=0; x < v2.size(); x++ ) v2[x] = 0; // set each one to zero

v2.push_back(5); // add another int, handles resizing/copying

std::list&ltint> l1;

// add three ints
l1.insert(5);
l1.insert(10);
l1.insert(7);

l1.sort(); // sort them to: 5, 7, 10

// automatic cleanup
}



These template classes are part of a template library called the STL. I would advise using this instead unless you have a real need to write your own low-level data structures. The STL is usually very highly optimized by the person who wrote the compiler, and as the interface is specified in the C++ language it makes your code a bit more portable and other C++ programmers will understand your code more easily.

Now, as to the original topic of discussion, I received that error recently, but I can''t remember what caused it... hmm... There seems to be nothing wrong with your code. Did you just copy and past portions of it? Perhaps the problem is one of the headers you included from troll.h (iostream.h - use iostream instead) or (cplus2eh.h - use STL).

Try commenting out the bodies of the functions using /* */ and remove the header inclusions from troll.h. Did this fix the problem?
AlekM said it right. You need a semicolon at the end of the class declaration.

  class troll{...};  




-------
Andrew

This topic is closed to new replies.

Advertisement