Learning headers and .cpp

This question might be slightly difficult to explain but i will try my best. I am learning to place different parts of code into header files and .cpp files. I have a piece of code which includes an if and an else statement which does not work in the header file or the .cpp but i cannot work out why or how i include the if or else statement in the code. All the errors i am getting revolve around the if and else piece of code (syntax error etc). So the first part of the code i have given is the .cpp then the header. Can someone please help me out, thanks


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
 #include <iostream>
using namespace std;
#include "Mammal.h"
#include "Human.h"


Human:: Human()
{
	cout << "A new human is born" << endl;
	speed = 2.0;
	civilized = true;
}
Human::~Human()
{
	cout << "The human has died" << endl;
}
void Human::talk() 
{
	cout << "I'm good looking for a ...Human" << endl;
}
void Human::walk()
{
	cout << "Left, right, left, right at the speed of " << speed << endl;
}
void Human::attack(Human & other)
{
	Human::if (civilized)
		cout << "Why would a human attack another? Je refuse" << endl;
	Human::else
		cout << "A human attacks another!" << endl;
}









#include <iostream>
#include "Mammal.h"


using namespace std;

class Human : public Mammal
{
	bool civilized;
public:
	Human();
	~Human();
	virtual void talk();
	virtual void walk();
	void attack(Human & other);
	if (civilized);
	else
};
ok i have changed the code to this, and there is no errors but the line of code for if or else does not run


#include <iostream>
using namespace std;
#include "Mammal.h"
#include "Human.h"


Human:: Human()
{
cout << "A new human is born" << endl;
speed = 2.0;
civilized = true;
}
Human::~Human()
{
cout << "The human has died" << endl;
}
void Human::talk()
{
cout << "I'm good looing for a ...Human" << endl;
}
void Human::walk()
{
cout << "Left, right, left, right at the speed of " << speed << endl;
}
void Human::attack(Human & other)
{
if (civilized)
cout << "Why would a human attack another? Je refuse" << endl;
else
cout << "A human attacks another!" << endl;
}
1
2
3
4
5
6
7
8
9
public:
	Human();
	~Human();
	virtual void talk();
	virtual void walk();
	void attack(Human & other);
	if (civilized);
	else
};


Your issue seems to be how you are interpreting this class, if(civilised) and else are not proper functions. All member functions need a return type and a name. What you need to do is create a function/operation that can implement the statement you are trying to apply. Assuming that your function is attack
Last edited on
I realised i never had the human.attack part in the main source code, but now that i have placed it in i get a error C2660: 'Human::attack' : function does not take 0 arguments and a too few arguments in function call error message.

This is the main source file. I dont know if this will provide much info without the rest of it

#include <iostream>
#include "Mammal.h"
#include "Dog.h"
#include "Cat.h"
#include "Human.h"
using namespace std;

int main()
{
Human human;
human.breathe();
human.talk();
human.walk();
human.attack();

Cat cat;
cat.breathe();
cat.talk();
cat.walk();

Dog dog;
dog.breathe();
dog.talk();
dog.walk();


system("PAUSE");
}
Last edited on
Yes again reason for that error is that you declared the attack function to take a Human class object as an argument.

void attack(Human & other);


human.attack(); <-------- Where is your argument here


I think it would benefit you greatly to really understand functions before moving onto working with classes and objects.
Im learning c++ from a book so I get lost with some of it. Especially parts the book does not properly explain. I will try go through it all again and figure it out
so i am still having problems with this, i dont understand why i need to have an argument in the source file or what the argument needs to be. I though that the function in the source.cpp would take the info it needs to run from the human.cpp and human.h

I am really new to learning c++ so using the correct terminology is a bit difficult just now so i apologise for that but any help would be appreciated.
i dont understand why i need to have an argument in the source file or what the argument needs to be.

Can you be more specific. What do you mean by "an argument" here? If you don't know the correct terminology, then give an example in your code of what you're talking about.

Also, please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Ok I will try explain the best I can, so i am learning to put different code in header files and .cpp files. I have split them all in to the relevant files, but in the source file I get a C2660: 'Human::attack' : function does not take 0 arguments and a too few arguments in function call error message.

When I asked yesterday someone said there needs to be an argument in the human.();
"
human.attack(); <-------- Where is your argument here"


The code below is the source.cpp the line human.attack(); is where I am getting the error. I have also supplied the human.cpp and the human.h code if that helps. Again I am sorry if this is difficult to understand.

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
#include <iostream>
#include "Mammal.h"
#include "Dog.h"
#include "Cat.h"
#include "Human.h"
using namespace std;

int main()
{
	Human human;
	human.breathe();
	human.talk();
	human.walk();
	human.attack();

	Cat cat;
	cat.breathe();
	cat.talk();
	cat.walk();

	Dog dog;
	dog.breathe();
	dog.talk();
	dog.walk();
	

	system("PAUSE");
}



human.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
#include <iostream>
using namespace std;
#include "Mammal.h"
#include "Human.h"


Human:: Human()
{
	cout << "A new human is born" << endl;
	speed = 2.0;
	civilized = true;
}
Human::~Human()
{
	cout << "The human has died" << endl;
}
void Human::talk() 
{
	cout << "I'm good looing for a ...Human" << endl;
}
void Human::walk()
{
	cout << "Left, right, left, right at the speed of " << speed << endl;
}
void Human::attack (Human & other)
{
	if(civilized)
		cout << "Why would a human attack another? Je refuse" << endl;
else
		cout << "A human attacks another!" << endl;
}




human.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "Mammal.h"


using namespace std;

class Human : public Mammal
{
	bool civilized;
public:
	Human();
	~Human();
	virtual void talk();
	virtual void walk();
	void attack(Human & other);
};




main.cpp line 14: You have no argument for human.attack().

human.h line 15, human.cpp line 25: human.atack() requires one argument (another human).

When you call a function that requires one argument, you must supply that argument in the call.
> i dont understand why i need to have an argument
¿who are you attacking?
Look at it from the compilers point of view:
When you call the attack() function in the main.cpp file, the compiler jumps over to the definition of that function. Since that function has been declared/defined with an argument,
human.h line 15
 
void attack(Human & other); //Why is (Human & other) here?  other is not used in this function. 

the compiler will not find a definition for an attack() function, rather an attack(Human & other) function. To the compiler, these are two totally separate functions. Look into overloaded functions if you still do not understand.

On that note, I would HIGHLY recommend learning function (and operator) overloads before moving onto inheritance and polymorphism. Below is a link to an overview of overloaded functions:
http://www.cplusplus.com/doc/tutorial/functions2/
Last edited on
Topic archived. No new replies allowed.