Can't figure it out...

Hey, i get a couple of errors in my code here and i can't seem to figure it out how to solve them.




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
#include <iostream>

using namespace std;

class Person
{
public:

    string namn;

    int alder;

    void SkrivUt()
    {
        cout<<namn<<endl;
        cout<<alder<<endl;
    }

    void SetInfo(string _namn, int _alder)
    {
        namn = _namn;
        alder = _alder;
    }
};

int linsok(Person p[], int n, int a)     //algorithm for linear search.
{
    for (int i = 0; i < n; i++)
    {
        if (p[i].alder == a)
            return i;
    }
    return -1;
}

int tot = 4;

void bubblesort(Person p[], int n)    // algorithm for bubblesort function
{
    for (int i = 0; i < tot; i++)
    {
        int nrLeft = tot - i;

        for (int j = 0; j < nrLeft; j++)
        {
            if (p[j] > p[j + 1])
            {
                int temp = p[j];
                p[j] = p[j + 1];
                p[j + 1] = temp;
            }
        }
    }

    for (int i = 0; i < n; i++)
    {
        cout<<p[i]<<endl;
    }
}


int main()
{
    Person familj[4];
    familj[0].SetInfo("Elliot", 21);
    familj[1].SetInfo("Kenneth", 51);
    familj[2].SetInfo("Vicky", 48);
    familj[3].SetInfo("Oliver", 18);
    bubblesort(familj[], 4);
    linsok(familj[], 4, 21);

}













Last edited on
All help is appreciated. Thanks!:)
Hi,

To get more replies, always use code tags, and make sure your code is indented. When you say errors, do you mean compile errors/warnings? If so paste the full text of them here, verbatim.

http://www.cplusplus.com/articles/z13hAqkS/
Editted the code as you said i should.
I will post the errors i get when i get home from work.
The errors i get are compile errors.

Thanks for the fast replies.
For lines 69 and 70, read "Arrays as parameters" from http://www.cplusplus.com/doc/tutorial/arrays/

Line 48 (and 50), what is wrong (or what should happen) in:
1
2
Person foo;
int bar = foo;


Line 46, what determines whether one person is larger than other?


Line 57, what is the difference in these:
1
2
3
4
Person foo;

cout << foo;   // 1
foo.SkrivUt(); // 2 
46|note: 'Person' is not derived from 'const std::reverse_iterator<_Iterator>'|

48|error: cannot convert 'Person' to 'int' in initialization|

50|error: no match for 'operator=' (operand types are 'Person' and 'int')|

57|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Person')|

69|error: cannot convert 'Person' to 'Person*' for argument '1' to 'void bubblesort(Person*, int)'|

70|error: cannot convert 'Person' to 'Person*' for argument '1' to 'int linsok(Person*, int, int)'|

Those are the errors i get. I know how to solve some of them like line 69 and 70. The others are mysteries to me:/
Hello elliotawerstedt,

The error on line 46 is because you are trying to compare a class to a class when you need to be comparing an int to an int. if (p[j].alder > p[j + 1].alder) would be the correct way.

On line 48 you define temp as an int and then try to set it equal to a class. This will not work. should be defined as Person temp = p[j] this will also correct the problem on line 50.

Line 57 is trying to output the entire class when it should be outputting the namn or alder i.e., cout << p[i].namn << endl;.

Lines 69 and 70 you need to remove the [] from the function calls.

Hope that helps,

Andy

P.S. line 42 should be "tot -1" not "- "i.
Last edited on
Topic archived. No new replies allowed.