Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Jammer99

Class Help

This topic is 6484 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''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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 <iostream>
#include <vector>
#include <string>
#include <list>

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<mydata> v1(256); // array of 256 mydata objects
std::vector<int> 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<int> 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?

Share this post


Link to post
Share on other sites
AlekM said it right. You need a semicolon at the end of the class declaration.

  
class troll
{
...
};




-------
Andrew

Share this post


Link to post
Share on other sites

  • 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!