Sign in to follow this  
Mizipzor

I break the project into smaller files and the program falls apart

Recommended Posts

My project have grewn a bit now and I decided to split it up in several files. Although its still just one class its good practice. The program works when its a single file but it doesnt when I break it up. Need help to see why. creature.h
#ifndef CREATURE_INCLUDED
#define CREATURE_INCLUDED

const int TARGET_NUMBER = 20;
const int CELL_SIZE = 20;
const int CREATURES = 10;

class creature {
public:
	creature();	//constructor
	void randomizeCells();
	void printCells();
	
	int calculateTotal();
	int calculateOffset();
	
	int abs(int i);	// makes sure a number if positive
	
private:
	int cell[CELL_SIZE];
};

#endif

creature.cpp
#include "creature.h"
#include <iostream>

creature::creature() {
	randomizeCells();
}

void creature::randomizeCells() {
	// fill them with junk	
	for(int i = 0; i < CELL_SIZE; i++)
		cell[i] = rand()%2;
}

void creature::printCells() {
	for(int i = 0; i < CELL_SIZE; i++)
		cout << cell[i];
}

int creature::calculateTotal() {
	// holder for the total number
	int total = 0;
	for(int i = 0; i < CELL_SIZE; i++)
		total += cell[i];
	return total;
}

int creature::calculateOffset() {
	int worth = this->calculateTotal() - TARGET_NUMBER;
	return abs(worth);
}	

int creature::abs(int i) {
	if(i >= 0)
		return i;
		
	if(i < 0) {
		i *= -1;
		return i;
	}
}


When I try to run it now it complains about cout is undecleared, but iostream is included just a couple of lines above. What did I do wrong when I split up the project into smaller files?

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