Problem with using class files

I'm just starting to learn to use classes in CPP. The idea seems ok but I'm having real problems when the class is put into two different files (.h and .cpp).

The code below is just meant to use a constructor to call a function to set the value of a private variable (I'm aware this could be done in the constructor itself).

The code of the three files:

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
//main.cpp

#include <fstream>
#include "Tom_class.h"


using namespace std;



int main ()

{

Tom_class tom(132);



}


//Tom_class.h
#ifndef TOM_CLASS_H
#define TOM_CLASS_H


class Tom_class
{
    int dog;
    public:

    Tom_class(int z);
    void setV(int a);

};

#endif // TOM_CLASS_H

//Tom_class.cpp

#include "Tom_class.h"

Tom_class::Tom_class(int f)
{

setV (f);

}

Tom_class:: void setV(int j)

{

    dog=j;
}


Keeps giving me expected unqualified id before void when declaring the setV function in the Tom_class cpp file.
The return type must be before the name:
void Tom_class::setV...
Suppose you have a program spread across a number of files and more than one file has an include directive for a class interface file such as yours: #include "Tom_class.h"

Under these circumstances, you can have files that include other files, and these other files may turn include yet other files. This can easily lead to a situation in which a file, in effect, contains the definitions in #include "Tom_class.h" more than once. C++ does not allow you to define a class more than once, even if the repeated definitions are identical.
That's probably why he has the include guards...
As Athar said, you need to declare what the program returns before you specify it.
Topic archived. No new replies allowed.