Header Files Issues

Using header files not working

Page 1 of 1

4 Replies - 972 Views - Last Post: 17 June 2010 - 09:10 AM Rate Topic: -----

#1 DeucEy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 11-June 10

Header Files Issues

Posted 17 June 2010 - 01:48 AM

The MAIN problem i am having with using these header files is that i cannot get the compiler to recognize the 'string' type and brings up a TON of errors. Here is my code for the main .cpp file and the header files. Can anyone help me run my program?
(P.S. It DOES work if everything is in the main .cpp problem but when i split them up into headers it doesn't build) Any help GREATLY appreciated.

Bug Class.cpp
#include <iostream>;
#include <string>;
#include "bug.h";
#include "swimming.h";
#include "flying.h";
#include "crawling.h";
using namespace std;

int main ()
{
	string bugName;
	Bug myBug;
	Flying flyingBug;
	Crawling crawlingBug;
	Swimming swimmingBug;
	int end;
	char classChoice;
	
	cout << "Welcome To *Creat Your Own Bug*" << endl;
	cout << "You will be selecting different traits throughout\nthe program to create a custom bug of your choice" << endl;
	cout << "-------------------------------" << endl << endl;
	cout << "To begin, lets give you bug a name: ";
	getline(cin, bugName);
	myBug.getTraits(bugName);
	cout << endl << "Now Lets Further Develop Your Bug" << endl;
	cout << "Select which class of Bug you want to develop:" << endl;
	cout << "F : Flying\nC : Crawling\nS : Swimming" << endl;
	cin >> classChoice;
	switch (classChoice)
	{
	case 'F':
	case 'f':
		flyingBug.getFlyTraits(bugName, myBug.numEyes, myBug.numLegs, myBug.color);
		break;
	case 'C':
	case 'c':
		crawlingBug.getCrawlTraits(bugName, myBug.numEyes, myBug.numLegs, myBug.color);
		break;
	case 's':
	case 'S':
		swimmingBug.getSwimTraits(bugName, myBug.numEyes, myBug.numLegs, myBug.color);
		break;
	}

	cin >> end;
	return 0;
}


swimming.h
#ifndef SWIMMING_H
#define SWIMMING_H
#include <iostream>;
#include <string>;

class Swimming : public Bug
{
private:
	int lifespan;
	int breathingTime;
public:
	void getSwimTraits(string, int, int, char);
	void printTraits(string, int, int, char, int, int);
};

void Swimming::getSwimTraits(string bName, int numEyes, int numLegs, char color)
{
	Bug myBug;

	cout << endl << "How long (in years) would you like you bug to live? ";
	cin >> lifespan;
	cout << endl << "How long (in minutes) will your bug be able to breathe underwater? ";
	cin >> breathingTime;
	cout << endl;
	cout << "-------------------------------" << endl;
	printTraits(bName, numEyes, numLegs, color, lifespan, breathingTime);
}

void Swimming::printTraits(string bName, int numEyes, int numLegs, char color, int lifespan, int flyingTime)
{
	Bug myBug;

	myBug.printTraits(bName, numEyes, numLegs, color);
	cout << "Life Span: " << lifespan << " Years" << endl;
	cout << "Flying Time: " << breathingTime << " Minutes" << endl;
	
}
#endif


crawling.h
#ifndef CRAWLING_H
#define CRAWLING_H
#include <iostream>;
#include <string>;

class Crawling : public Bug
{
private:
	int lifespan;
	int websize;
public:
	void getCrawlTraits(string, int, int, char);
	void printTraits(string, int, int, char, int, int);
};

void Crawling::getCrawlTraits(string bName, int numEyes, int numLegs, char color)
{
	Bug myBug;

	cout << endl << "How long (in years) would you like you bug to live? ";
	cin >> lifespan;
	cout << endl << "How big (inches) of a web would you bug be able to make? ";
	cin >> websize;
	cout << endl;
	cout << "-------------------------------" << endl;
	printTraits(bName, numEyes, numLegs, color, lifespan, websize);
}

void Crawling::printTraits(string bName, int numEyes, int numLegs, char color, int lifespan, int flyingTime)
{
	Bug myBug;

	myBug.printTraits(bName, numEyes, numLegs, color);
	cout << "Life Span: " << lifespan << " Years" << endl;
	cout << "Web Size: " << websize << " Inches Long" << endl;	
}
#endif


flying.h
#ifndef FLYING_H
#define FLYING_H

#include <iostream>;
#include <string>;
using namespace std;

class Flying : public Bug
{
private:
	int lifespan;
	int flyingTime;
public:
	void getFlyTraits(string, int, int, char);
	void printTraits(string, int, int, char, int, int);
};

void Flying::getFlyTraits(string bName, int numEyes, int numLegs, char color)
{
	Bug myBug;

	cout << endl << "How long (in years) would you like you bug to live? ";
	cin >> lifespan;
	cout << endl << "How long (in minutes) will your bug be able to Fly? ";
	cin >> flyingTime;
	cout << endl;
	cout << "-------------------------------" << endl;
	printTraits(bName, numEyes, numLegs, color, lifespan, flyingTime);
}

void Flying::printTraits(string bName, int numEyes, int numLegs, char color, int lifespan, int flyingTime)
{
	Bug myBug;

	myBug.printTraits(bName, numEyes, numLegs, color);
	cout << "Life Span: " << lifespan << " Years" << endl;
	cout << "Flying Time: " << flyingTime << " Minutes" << endl;


}

#endif


bug.h
#ifndef BUG_H
#define BUG_H
#include <iostream>;
#include <string>;

class Bug
{
public:
	string bName;
	int numEyes;
	int numLegs;
	char color;

	int getTraits(string);
	void printTraits(string, int, int, char);

	Bug();
	Bug(string, int, int, char);
};

Bug::Bug()
{
	bName;
	numEyes = 0;
	numLegs = 0;
	color = '*';
}

Bug::Bug(string, int nE, int nL, char col)
{
	bName;
	numEyes = nE;
	numLegs = nL;
	color = col;
}

int Bug::getTraits(string bName)
{
	cout << endl << "Now, your bug gets a choice of a number of\nEyes, Legs, and A color of your choice\n";
	cout << "How many Eyes would you like your Bug to have(at Least 2)? ";
	cin >> numEyes;
	if (numEyes < 2)
	{
		numEyes = 2;
	}
	else {
		numEyes = numEyes;
	}
	cout << endl << "How many legs would you like you bug to have(at Least 2)?" ;
	cin >> numLegs;
	if (numLegs < 2)
	{
		numLegs = 2;
	}
	else {
		numLegs = numLegs;
	}
	cout << endl << "Lastly, what color would you like your bug to be: " << endl;
	cout << "w : White\nb : Blue\ng : Green\ny : Yellow\n";
	cin >> color;
	cout << endl;
	cout << "-------------------------------" << endl;
	printTraits(bName, numEyes, numLegs, color);

	return numEyes, numLegs;
}

void Bug::printTraits(string bName, int numEyes, int numLegs, char color)
{
	cout << "Name: " << bName << endl;
	cout << "Number of Eyes: " << numEyes << endl;
	cout << "Number of Legs: " << numLegs << endl;
	switch (color)
	{
	case 'W':
	case 'w':
		cout << "Color : White" << endl;
		break;
	case 'B':
	case 'b':
		cout << "Color : Blue" << endl;
		break;
	case 'G':
	case 'g':
		cout << "Color : Green" << endl;
		break;
	case 'Y':
	case 'y':
		cout << "Color : Yellow" << endl;
		break;
	}

}


#endif


Is This A Good Question/Topic? 0
  • +

Replies To: Header Files Issues

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Header Files Issues

Posted 17 June 2010 - 02:09 AM

What are the errors you're seeing? For one, you shouldn't be putting a semicolon at the end of an include statement
#include <iostream>;

Also, string exists within the std namespace. within headers, you should prefix it with std:: (prefer doing this to 'using namespace')

This post has been edited by Bench: 17 June 2010 - 02:11 AM

Was This Post Helpful? 1
  • +
  • -

#3 DeucEy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 11-June 10

Re: Header Files Issues

Posted 17 June 2010 - 02:21 AM

View PostBench, on 17 June 2010 - 01:09 AM, said:

What are the errors you're seeing? For one, you shouldn't be putting a semicolon at the end of an include statement
#include <iostream>;

Also, string exists within the std namespace. within headers, you should prefix it with std:: (prefer doing this to 'using namespace')

Thank you post very helpful i did the std:: for everything and it pretty much fixed everything..
Now its just ONE more problem i've never seen. Now all im getting is a bunch of these errors
1>c:\users\christopher\documents\swimming.h(24): error C2065: 'endl' : undeclared identifier
theres about 40 of these and is the ONLY error i have.
Your suggestion cleared up about 100 so thank you lol :bigsmile:

nvm got it thank you so much
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Header Files Issues

Posted 17 June 2010 - 08:27 AM

endl is ALSO in the std:: namespace and needs to be prefixed with "std::"

(hint: this is a good place to use a search/replace)
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Header Files Issues

Posted 17 June 2010 - 09:10 AM

*Moved to C/C++*
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1