Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

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

This topic is 4748 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

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 = rand()%2;
}

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

int creature::calculateTotal() {
	// holder for the total number
	int total = 0;
	for(int i = 0; i < CELL_SIZE; i++)
		total += cell;
	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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!