Cannot Access Class Header File

I've written a class that defines a Bow object with it's colour, number of arrows etc. and two actions, draw and fire. The actions draw the Bow back and fire it if it's drawn then assign a random number of points for the shot.

I put the declaration and definition into different files (declaration in header file, definition in .cpp) and am trying to use them with the #include directive in a test chassis to test all the functions but I'm recieving an error stating the file can't be accessed. Am I supposed to add the .h and .cpp files into the program? Can someone explain my error/point me towards a good guide for splitting classes into files for later use?

Also, in the definition I'm getting this error:

MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup


Sorry for the length but wasn't sure what was necessary to put in so I put it all in.

Bow.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <ctime>
#include "Bow.h"

using std::string;

Bow::Bow(string aColour)
{
	using std::srand;
	using std::time;
	numOfArrows = 10;
	drawn = false;
	colour = aColour;

	srand((unsigned int)time(0));
}

Bow::~Bow()
{
}

void Bow::draw()
{
	using std::cout;
	drawn = true;
	cout << "The " << colour << " Bow has been drawn.\n";
}

int Bow::fire()
{
	using std::cout;

	if (!drawn)
	{
		cout << colour << " Bow has not been drawn and therefore could not fire.\n";
		return 0;
	}

	int score;
	score = rand() % (10 - 0 + 1) + 0;
	if (score == 0)
	{
		cout << colour << " Bow missed the target!\n";
	}
	else
	{
		cout << colour << " Bow scored " << score << " points!\n";
	}
	return score;
}


Bow.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>

using std::string;

class Bow
{
	string colour;
	bool drawn;
	int numOfArrows;
public:
	Bow(string aColour);
	~Bow();

	void draw();
	int fire();
};


Test Chassis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include "Bow.h"

void bowTest(void);

int main(void)
{
	bowTest();

	return 0;
}

void bowTest(void)
{
	using std::cout;

	cout << "Yellow Bow created.\n";
	Bow yellow("Yellow");
	cout << "Attempting to fire Yellow Bow.\n";
	yellow.fire();
	cout << "Drawing the Bow.\n";
	yellow.draw();
	cout << "Attempting to fire Yellow Bow.\n";
	yellow.fire();
}
Yes, the bow.cpp/bow.h files need to be part of the project as well.
You also need to compile the two .cpp files together, i.e. command [options] Bow.cpp test.cpp, or add the missing one to the project's list of source files... this is very different in all IDE's so that's up to you to figure out.

But if you have any classses with .cpp "definition" files, they needs to be linked against, usually, in the way that I specified above..
On a side note adding #include <iostream> to both class definition and header is not necessary. once the preprocessor includes the header, it includes all headers defined within.
So typically I have all my #includes in my header file.
Then in the .cpp class definition simply:
1
2
3
4
5
6
#include "header.h"

// and namespaces
using std::cout;
using std::string;
using std::endl;

Last edited on
Indeed. This is a very good approach, IMO.
Last edited on
Topic archived. No new replies allowed.