Constructor and destructor problem

This is how it is put in my book "Now add an assiocated destructor prototype
~Dog();

After that add the class declarions etc but it just doesn't compile and throws me these errors:

1
2
3
4
5
6
7
1
1>  Source.cpp
1>c:\users\NewComer\documents\visual studio 2012\projects\constuctor\constuctor\source.cpp(25): error C2535: 'Dog::Dog(int,int,std::string)' : member function already defined or declared
1>          c:\users\NewComer\documents\visual studio 2012\projects\constuctor\constuctor\source.cpp(15) : see declaration of 'Dog::Dog'
1>c:\users\NewComer\documents\visual studio 2012\projects\constuctor\constuctor\source.cpp(32): error C2535: 'Dog::~Dog(void)' : member function already defined or declared
1>          c:\users\NewComer\documents\visual studio 2012\projects\constuctor\constuctor\source.cpp(11) : see declaration of 'Dog::~Dog'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
	


Here is the full code, can anyone help me before I smash up my laptop lol
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
#include <iostream>
#include <string>
using namespace std;

class Dog
{
	int age, weight;
	string colour;
public:

	void bark() { cout << "WOOF!" << endl ; }
	//void setValues (int, int, string); // this is incorrect

	Dog (int ,int , string) 
		
	{
		this -> age = age;
		this -> weight = weight;
		this -> colour = colour;
	}

	~Dog();
	Dog::Dog (int age, int weight, string colour)
	{
		this -> age = age;
		this -> weight = weight;
		this -> colour = colour;
	}

	Dog::~Dog()
	{
		cout << "Objected destoryed." << endl;
	}

	int getAge() { return age ; }
	int getWeight() { return weight ; }
	string getColour() { return colour ; }

};

int main()
{
	Dog fido(3,15,"brown");

	cout << "Fido is a " << fido.getColour() <<
								" dog" << endl;
	cout << "Fido is " << fido.getAge() <<
								" dog" << endl;
	cout << "Fido weighs "	<< fido.getWeight() <<
								" pounds" << endl;
	fido.bark();

	Dog pooch(4,18,"grey");

	cout << "Pooch is a " << pooch.getAge();
	cout << " year old " << pooch.getColour();
	cout << " dog who weighs " << pooch.getWeight();
	cout << " pounds.";

	pooch.bark(); 
	
	return 0;
}
Last edited on
You have duplicate definitions in your class. Also - if you're definining a member function within the class scope, you don't need the Dog:: prefix on the function.

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
class Dog
{
	int age, weight;
	string colour;
public:

	void bark() { cout << "WOOF!" << endl ; }
	//void setValues (int, int, string); // this is incorrect

	Dog (int ,int , string) 
		
	{
		this -> age = age;
		this -> weight = weight;
		this -> colour = colour;
	}

	/*~Dog();
	Dog::Dog (int age, int weight, string colour)
	{
		this -> age = age;
		this -> weight = weight;
		this -> colour = colour;
	}*/

	Dog::~Dog() 
	{
		cout << "Objected destoryed." << endl;
	}

	int getAge() { return age ; }
	int getWeight() { return weight ; }
	string getColour() { return colour ; }

};



or leave prototypes in the class, and define the functions outside

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
class Dog
{
	int age, weight;
	string colour;
public:

	void bark() { cout << "WOOF!" << endl ; }
	//void setValues (int, int, string); // this is incorrect

	Dog (int ,int , string);
		
	/*{
		this -> age = age;
		this -> weight = weight;
		this -> colour = colour;
	}*/

	~Dog();


	int getAge() { return age ; }
	int getWeight() { return weight ; }
	string getColour() { return colour ; }

};

Dog::Dog (int age, int weight, string colour)
{
	this -> age = age;
	this -> weight = weight;
	this -> colour = colour;
}

Dog::~Dog()
{
	cout << "Objected destoryed." << endl;
}
Last edited on
I fixed the errors in your first post (cheers btw) but now I get weird output
http://i.imgur.com/ytiRYlq.png

This book is so poorly written, could you reccomend a good C++ book? I've looked around but C++ Primer seems to advanced for me...
Presumably you went with the first snippet, then, which contains a serious bug which is reflected in your original code.

1
2
3
4
5
6
7
	Dog (int ,int , string) 
		
	{
		this -> age = age;
		this -> weight = weight;
		this -> colour = colour;
	}


Here, none of the three parameters are named, so you cannot access them and this code is completely equivalent to:

1
2
3
	Dog (int ,int , string) 		
	{
	}


Self-assigning uninitialized variables leaves you with junk values, which is is what you're seeing in the output.

[edit: Please post in a relevant forum next time. This has nothing to do with Windows Programming.]
Last edited on
Thank you :)
Topic archived. No new replies allowed.