class help/static variable problem

Write your question here.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  #include<iostream>
#include<string>
using namespace std;

#ifndef TOY_H
#define TOY_H

class Toy 
{
protected:
	string _mfgr;
	double _wholesale;
	static int _count;
public:
	Toy(string mfgr, double wholesale);
	~Toy();
	double getWholesale();
	int getCount();
	virtual void showAge() = 0;
	friend void calcRetail(Toy & toy);
};
#endif
// ====================================================================
// Toy.cpp
#include"Toy.h"
Toy::Toy(string mfgr, double wholesale)
{
	_mfgr = mfgr;
	_wholesale = wholesale;
	_count ++;
}
Toy::~Toy(){}
double Toy::getWholesale()
{
	return _wholesale; 
}
int Toy::getCount()
{ 
	return _count;
}
// ====================================================================
// ActionToy.h
#include"Toy.h"
#ifndef ACTIONTOY_H
#define ACTIONTOY_H

class ActionToy : public Toy
{
private:
	char _gender;
public:
	ActionToy(string mfgr, double wholesale, char gender);
	~ActionToy();
	void displayToy();
	virtual void showAge();
};
#endif
// ====================================================================
// ActionToy.cpp
#include"ActionToy.h"

ActionToy::ActionToy(string mfgr, double wholesale, char gender) :Toy(mfgr,wholesale)
{
	_gender = gender;
}
ActionToy::~ActionToy(){}
void ActionToy::displayToy()
{
	cout << "GENDER: " << _gender << endl
		 << "MFGR: " << _mfgr << "." << endl
		 << "WHOLESALE: $" << _wholesale << "." << endl;
}
void ActionToy::showAge()
{
	cout << "The appropriate age for this toy is 6 to 10 years old.\n";
}
// ====================================================================
// Game.h
#include"Toy.h"
#ifndef GAME_H
#define GAME_H

class Game : public Toy
{
private:
	string _type;
public:
	Game(string mfgr, double wholesale, string type);
	~Game();
	void displayToy();
	virtual void showAge();
};
#endif
// ====================================================================
// Game.cpp
#include"Game.h"

Game::Game(string mfgr, double wholesale, string type) : Toy(mfgr, wholesale)
{
	_type = type;
}
Game::~Game(){}
void Game::displayToy()
{
	cout << "TYPE: " << _type << endl
		 << "MFGR: " << _mfgr << endl
		 << "WHOLESALE: $" << _wholesale << endl;
}
void Game::showAge()
{
	cout << "The appropriate for this toy is 12 years and older.\n\n" ;

}
// ====================================================================
// driver/main.cpp
#include"Game.h"
#include"ActionToy.h"

int main()
{
	ActionToy actionToy("China", 4,'M');
	Game game("Taiwan", 5, "Chess Set");

	cout << "Action toy information: \n";
	actionToy.displayToy();
	calcRetail(actionToy);
	actionToy.showAge();
	cout << endl << endl;

	cout << "Game toy information: " << endl << endl;
	game.displayToy();
	calcRetail(game);
	game.showAge();
	

	cout << game.getCount() << "toys are shown.\n";
	
	return 0;
}
void calcRetail(Toy & toy)
{
	cout << "RETAIL: $" << (toy.getWholesale() *2.0)
		 << "." << endl;
}
// ==================================================================== 

This is the error that shows up:
------ Build started: Project: hw 11a, Configuration: Debug Win32 ------
driver.cpp
Toy.obj : error LNK2001: unresolved external symbol "protected: static int Toy::_count" (?_count@Toy@@1HA)
c:\users\documents\visual studio 2010\Projects\hw 11a\Debug\hw 11a.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I know I'm doing everything else right except for the code that has to do with the static variable. In the end of driver.cpp the code is supposed to output something like "2 toys are shown" Im not sure what I'm doing wrong.
I checked all possible linker errors, I cant figure it out, and that article didn't help me, is there anything else?
remove static from static int _count; and it will compile.
Last edited on
Or if you need _count to remain static add int Toy::_count = 0; to Toy.cpp
thank you! adding the" int Toy:: _count = 0;" worked!
Topic archived. No new replies allowed.