Two classes, one struct

Hey! I was hoping to get some help with classes and structs. I've already read the forums relating to my problem, and they've helped so far, but now I'm stuck.

Below, I have two classes and a struct within a class. Right now it compiles as is, but when I make the array declaration visible in the 'add' function, I get an error pointing to line 38 saying:

there is no matching function for call to 'Finish::Finish()'

Within the struct, i can change the type 'Finish' to type 'int', which allows me to make the array declaration visible, but when I go to assign

'good[0].high = k.getB'

an error pops up saying:

cannot convert 'Finish::getB' from type 'int (Finish::)()' to type 'int'|

which is understandable, but I don't know how to fix it. There's also a vector declaration, but I'd much rather use a struct. Anyone know what to do or where else to look?


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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>


using namespace std;


class Finish{
    int a, b;

public:

    int getA(){return a;}
    int getB(){return b;}

    Finish (int h, int j);

    int other(){
        int f = b-a;
        return f;
    }


};

class Start{

    int num, jar;

std::vector<Finish> soo;
///struct instead?

struct Struct{      ///Trying to make this struct
    Finish low;     ///work with 'Finish' type variables
    Finish high;    ///
};

public:

    Start (int,int);
    int answer() {
        int d = num+jar;
        return d;
    }

    void test(Finish m){
        cout<<m.getA()<<endl;
    cout<<endl<<m.getA()+m.getB()<<endl<<endl;
    cout<<jar-num<<endl<<endl;

    ///add elements to vector 'soo'?
    ///or
    ///add elements to Struct?

    }

    void add(Struct, Finish k){                 ///pass through class 'Finish' and struct 'Struct'
        ///Struct * good = new Struct[1000];    <---trying to set up an array

        ///good[0].high = k.getB;               <---trying to take in values from class 'Finish'

    }

};


Start::Start (int c, int d){
    num = c;
    jar = d;
}

Finish::Finish (int h, int j)
    : a(h), b(j)
{}



P.S. This problem relates to a project I'm working on now, this code is just used to play around and find out how things work (in this case, classes and structs). The architecture is very similar, so there's little I can do to move things around, if that makes sense.
I'm seeing some errors in there, but I'll let you play around with those. The error you get is because Finish only has a ctor that takes two arguments. Make the Struct ctor pass some values to its members' ctors or write a Finish ctor that takes zero arguments.
1
2
3
4
5
6
7
8
9
10
11
12
struct Struct{     
    int low;     
    int high;    

    Struct(Finish m)
    {
        low = m.getA();
        high = m.getB();

    }

};


I think this is what you're talking about, right? It compiles fine at least. I understand what this is, but how do I use it?
Thats not quite the problem.

At lines 37-38 your have two instances of Finsh. When Start is constructed, those two instances of Finish must be constructed, however you have no default constructor for Finish.

As previously pointed out by maeriden, you have two choices.
1) Provide a default constructor for Finish
1
2
  Finish::Finish ()
  { a = 0; b = 0; } 


2) Invoke your existing 2 argument constructor for Finish when constructing Start:
1
2
  Start::Start () : low(0,0), high(0,0)
  {}


Ohhhh. Ok, I want the second one for sure but I don't understand it.

1
2
Start::Start () : low(0,0), high(0,0)
{}
This tells me there's no such thing as low or high in Start

1
2
Start::Start::Struct (Finish) : low(0,0), high(0,0)
{}
This would make more sense to me, but again I'm not sure. And it says it needs an initializer before ':'

What am I saying with low(0,0) and high(0,0)? Or rather what does (0,0) set?

And then once I do set this struct up, could I just make a struct array in the public portion of Start like I normally would?




For reference:

(I made a new file and just cleaned up everything that didnt need to be there)

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>


using namespace std;


class Finish{
    int a, b;

public:

    int getA(){return a;}
    int getB(){return b;}

    Finish (int h, int j);

    int other(){
        int f = b-a;
        return f;
    }


};

class Start{

    int num, jar;

struct Struct{      ///Trying to make this struct
    Finish low;     ///work with 'Finish' type variables
    Finish high;    ///
};


public:

    Start (int,int);
    Start ();


};

Start::Start (int c, int d){
    num = c;
    jar = d;
}

Finish::Finish (int h, int j)
    : a(h), b(j)
{}

Start::Start::Struct (Finish) : low(0,0), high(0,0)
{}

int main(){

return 0;
}


I really appreciate the help
This tells me there's no such thing as low or high in Start

Sorry, my mistake. Lines 34-34 only declare a type. They do not instantiate it. Not clear if you meant to instaniate it there.

You have a syntax problem in line 56. You appear to be implementing a constructor for Struct, but no such constructor is declared. Also, your reference to Finish as an argument needs to be named.
When I add

Struct (Finish);

to Struct, and

1
2
Start::Struct::Struct (Finish) : low(0,0), high(0,0)
{}


on the outside, the program compiles. For reference:

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>


using namespace std;

class Finish{
    int a, b;

public:

    int getA(){return a;}
    int getB(){return b;}

    Finish (int h, int j);

    int other(){
        int f = b-a;
        return f;
    }
};

class Start{

    int num, jar;

struct Struct{     ///Trying to make this struct
    Finish low;     ///work with 'Finish' type variables
    Finish high;    ///
    Struct (Finish);

};

public:

    Start (int,int);
};

Start::Start (int c, int d){
    num = c;
    jar = d;
}

Finish::Finish (int h, int j)
    : a(h), b(j)
{}

Start::Struct::Struct (Finish) : low(0,0), high(0,0)
{}

int main(){

return 0;
}


If it is, and I'm on the right track, I still don't know what low(0,0) and high(0,0) do. I mean, it looks like they set low and high equal to 0.

Also, what would the struct array initiation look like? Would I just play around with it like normal, but only passing and assigning Finish types?
Lines 33 and 51 aren't right yet.

You've declared and implemented a constructor for Struct that takes one argument of type Finish, but then don't use it.

Perhaps you meant something like this:
1
2
Start::Struct::Struct (Finish l, Finish h) : low(l), high(h)
{}

In this constructor, we're passing two instances of Finish. We then use l and h to initialize low and high respectively.



Sweet! That makes sense.

So here's what I've been trying to do:

Given these
1
2
    Start foo (2, 8);
    Finish var (2, 4);

I want to pass var through a function in Start
1
2
3
4
5
6
7
    void add(Finish k)
    {
        int answer = k.getA() + k.getB();

        cout << answer <<endl<<endl;

    }


to look like this
1
2
3
4
5
6
7
8
9
int main(){

    Start foo (2, 8);
    Finish var (2, 4);

    foo.add(var);

return 0;
}

but I need the function to have an array of structs. I understand that initializing low and high will look something similar to foo and var. But the only way I can think of is
Start.Struct.Struct()

or
Start.Struct = ...

or
Struct.Struct = ...

I thought that since Structs and classes were similar, you could access them the same way?

How would I go about setting this up?
Ok, so after reading around I found that I'm supposed to declare the array like
Struct * foo[100];

instead of
Struct * foo = new Struct[100];

Is there any way I can declare this inside a function? I'm trying to avoid passing it through (see below). Is there any way I can create a function such as add:

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
class Start{

    int num, jar;

struct Struct{     ///Trying to make this struct
    Finish low;     ///work with 'Finish' type variables
    Finish high;    ///
    Struct (Finish l, Finish h);

};

public:

    Start (int,int);

    void add(Finish k)
    {
        Struct * bam[10];
        ///assign bam values using k.getA() and k.getB()

        int answer = k.getA() + k.getB();
        cout << answer <<endl<<endl;
    }

};


without having to pass bam through as a parameter? The reason I don't want to is because the main must stay the same, only passing through var:

1
2
3
4
5
6
7
8
9
int main(){

    Start foo (2, 8);
    Finish var (2, 4);

    foo.add(var);

return 0;
}


If this can't be done, how would I go about storing var, again, keeping everything in main the same?
Topic archived. No new replies allowed.