Sign in to follow this  
Attala101

Class problem (again)

Recommended Posts

Hi I have made av simple class calles "nationclass". In the header file I define it like this:
#ifndef NATIONCLASS_H
#define NATIONCLASS_H

class nationclass
{
public:

	nationclass();
	~nationclass();
	
	void init( );

	int ICBMbigCount, ICBMmediumCount, ICBMsmallCount;

private:

};


#endif

My .cpp file looks like this:
#include "nationclass.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

nationclass::nationclass( )
{
	ICBMsmallCount = 0;
	ICBMmediumCount = 0;
	ICBMbigCount = 0;

}

nationclass::~nationclass()
{
   
}

void nationclass::init ( )
{


	int i;
	for (i=0; i < 3; i++)
	{
	int r;
	r = (rand() % 3) + 1;
	switch (r)
	{
	case 1:
		{
		ICBMsmallCount++;
		break;
		}
	case 2:
		{
		ICBMmediumCount++;
		break;
		}
	case 3:
		{
		ICBMbigCount++;
		break;
		}
	}
	}
}

It all compiles fine but when I try to call the memberfunction init() it says that the public ints ICBMbigCount, ICBMmediumCount and ICBMsmallCount cannot be evaluated. (Access violation reading location 0x00000000). Can anybody see what I'm doing wrong?

Share this post


Link to post
Share on other sites
Sounds like a null pointer error. What does the calling code look like.

Also:
Quote:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#include <ctime>
#include <cstdio>
#include <cstdlib>


Or even just

#include <cstdlib>

since rand is the only standard function you use. This is C++, not C.

Σnigma

Share this post


Link to post
Share on other sites
I guess you are calling the init something like this:

nationclass_pointer->init();

If thats the case, make sure the nationclass_pointer is valid (not pointing at address NULL)

Share this post


Link to post
Share on other sites
Well In my game.h file I do this:

class nationclass;
extern nationclass * USA;

In my game.cpp file:

nationclass * USA;

And in a function calles game_init() I go:

USA->init();

Share this post


Link to post
Share on other sites
USA is a pointer, but it doesn't actually point to anything. You need to allocate an object of type nationclass and point USA at it somewhere:

USA = new nationclass();

and don't forget to delete it later!

Σnigma

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this