Calling and using a constructor function containing vectors

Hi, I something similar to this before but realised I was asking slightly different question to the one I needed answering. Basically I have defined 4 variables in the class CRectangle as private, I have defined a constructor function, which I use to allow the user to input values. I am then trying to call the variables in the function fish and then show the final answer in int main. However, I am unsure how to call the variables in fish as I've tried using an object and it is saying they are not declared. Similarly in int main it says there is no matching call to 'CRectangle::CRectangle'. I think its the constructor that is the issue as I've managed similar with normal functions. If anyone has anything that could help me that would be very much appreciated as I'm struggling to see where to go from here. Thank you

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
/#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

class CRectangle {
private:
double S0;
double U;
double D;
double R;
public:
CRectangle (vector<double> _S0,double _U,double _D,double _R);
};

CRectangle::CRectangle (vector<double> _S0, double _U, double _D, double _R)
{
S0[i]=_S0;
U=_U;
D=_D;
R=_R;

for (vector<double>::size_type i = 0; i < 2; i++)
{
cout << "Enter s0 " << i+1
<< ": " << flush;
cin >> S0[i];
cout << endl;
}

cout << "Enter U: "; cin >> U;
cout << "Enter D: "; cin >> D;
cout << "Enter R: "; cin >> R;
cout << endl;

};

double Fish(CRectangle& Model)
{
return (S0[0]-R)/(U-D);
}
int main () {
CRectangle Model;
cout << "rect area: " << Fish(Model)<< endl;

return 0;
}/
Last edited on
Please put your code inside code tags next time [code] /* code here */ [/code]. It makes it much easier to read your code.

When you pass Model to Fish you should leave out the type. Fish(Model)

Yes it is complaining about your constructor. CRectangle constructor takes 4 arguments but you give it none.
Hi, I have edited it so the code is more readable, sorry I didn't know you could do that. I have edited it above as suggested. However, I'm having 2 main problems. Firstly, how to use the vector above in the constructor, my current knowledge of vectors is hazy. Secondly, I am unsure how to pass the variables of CRectangle to Fish?
I'm doing this for a project that I need to do by the way where I need use lots of vectors and constructors. I thought if I had a base example I would understand it better. I have pieced this together from previous equation we have made so far.
Topic archived. No new replies allowed.