First time using classes

I wrote a simple function to draw a rectangle. Everything is fine except I have 2 small (i think) errors. I just can't figure out what I'm doing wrong...

Here's the code:
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
#include <iostream>

using namespace std;

class rechthoek
{
    private:
    int hoogte;
    int breedte;

    public:
    rechthoek (int b = 0, int h = 0) : breedte(b), hoogte(h){}
    void print();




    //getters & setters
    int getHoogte()
    {
        return hoogte;
    }
    int getBreedte()
    {
        return breedte;
    }
    void setHoogte(int h)
    {
        hoogte = h;
    }
    void setBreedte(int b)
    {
        breedte = b;
    }

}

int main()
{
    int h, b;
    rechthoek rechthoek1;

    //set hoogte
    cout<<"Hoe hoog moet de rechthoek zijn?\n";
    cin>>h;
    rechthoek1::setHoogte(h);

    //set breedte
    cout<<"Hoe breed moet de rechthoek zijn?\n";
    cin>>b;
    rechthoek1::setBreedte(b);

    rechthoek1::print();







    void  rechthoek::print()
    {
        //bovenste lijn
        for (int i = 0; i < rechthoek1::getBreedte(); ++i)
        {
            cout<<"x";
        }
        cout<<"\n";

        //middenstuk
        for (int j = 0; j < rechthoek1::getHoogte() - 2; ++j)
        {
            cout<<"x";

            for(int k = 0; k < rechthoek1::getBreedte() - 2; ++k)
            {
                cout<<" ";
            }

            cout<<"x";
            cout<<"\n";

        }

                //bovenste lijn
        for (int i = 0; i < rechthoek1::getBreedte(); ++i)
        {
            cout<<"x";
        }


    }

}


And here's the error I'm getting:
1
2
3
4
5
6
7
8
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp||In constructor 'rechthoek::rechthoek(int, int)':|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|9|warning: 'rechthoek::breedte' will be initialized after|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|8|warning:   'int rechthoek::hoogte'|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|12|warning:   when initialized here|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|5|error: new types may not be defined in a return type|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|5|note: (perhaps a semicolon is missing after the definition of 'rechthoek')|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|38|error: two or more data types in declaration of 'main'|
||=== Build finished: 2 errors, 3 warnings ===|


thanks!
your compiler wrote:
note: (perhaps a semicolon is missing after the definition of 'rechthoek'
Oh god, english is not my native language and i thought semicolon meant: { instead of ; :p

But now I have these other errors:
1
2
3
4
5
6
7
8
9
10
11
12
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp||In constructor 'rechthoek::rechthoek(int, int)':|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|9|warning: 'rechthoek::breedte' will be initialized after|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|8|warning:   'int rechthoek::hoogte'|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|12|warning:   when initialized here|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp||In function 'int main()':|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|46|error: 'rechthoek1' is not a class or namespace|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|51|error: 'rechthoek1' is not a class or namespace|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|53|error: 'rechthoek1' is not a class or namespace|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|61|error: 'rechthoek1' is not a class or namespace|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|62|error: a function-definition is not allowed here before '{' token|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 9.10 [1]\main.cpp|94|error: expected '}' at end of input|
||=== Build finished: 6 errors, 3 warnings ===|

Try using '.' instead of '::' (e.g. rechthoek1.print();) when you use an object.

EDIT: And don't define rechthoek::print inside main!

Last edited on
Oh, so stupid!
Thanks a load!
Topic archived. No new replies allowed.