using vectors

Hi,

I am trying to understand how vectors work in particular how to pass them from a constructor function to int main. This is my current code. Its pretty basic but I'm trying to create a program to allow the user to input all the values for the variables, later to be used in more complex functions. However when I run it, it says that fish in rect, which is of non-class type 'CRectangle()'. When building this I originally got it to work without vectors but it still needed to have CRectangle rect(1,1,1,1) or something similar in main even though the 1's weren't being used. Has anyone got any ideas?

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

class CRectangle {
private:
double S0;
double U;
double D;
double R;
public:
CRectangle (vector<double>,double,double,double);
double RiskNeutProb();
};

CRectangle::CRectangle (vector<double> S0, double b, double c, double d)
{

U = b;
D=c;
R=d;

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 CRectangle::Fish(void)
{
return (R-D)/(U-D);
}
int main () {

CRectangle rect();
cout << "rect area: " << rect.Fish()<< endl;

return 0;
}
The function Fish has not been declared in the class definition.

CRectangle rect(); This is a function declaration. If you want to call the default constructor you have to remove the parenthesis CRectangle rect;. CRectangle has no default constructor so you can either declare a default constructor or use another constructor.
Thank you for your help, however if I do this it comes up with the error: "no matching function for call to 'CRectangle::CRectangle()'and then note:candidates are: CRectangle(std::vector<double,... or CRectangle(const CRectangle&). I am attempting to call the constructing defined above in which the user defines the variables and then input these values into the formula of Fish.

I also noticed above that I needed to change RiskNeutProb to Fish in the class declarations and remove the void after Fish in the definition. However this does not solve the problem. What could be going wrong?
If you want to use the constructor CRectangle (vector<double>,double,double,double); you have to pass the arguments to the constructor.
1
2
vector<double> v;
CRectangle rect(v, 1.2, 1.0, 7.5);
Last edited on
Yes in main. The numbers are just arbitrary numbers.

It probably crashes because you try to access elements in an empty vector.

It looks almost as if you don't actually want to have any arguments to the constructor. If you don't want to pass in the values of S0, b, c and d to the constructor, just make them local variables in the constructor instead.
Sorry I deleted my response as I changed it and it came out with the answer I wanted.

int main () {
double b,c,d;
vector<double> S0(2);
CRectangle rect(S0,b,c,d);
cout << "rect area: " << rect.Fish()<< endl;

return 0;
}

I have changed int main to this and it gives me the value of Fish that should come out. Just to clarify, have I actually used the variables initially defined in the CRectangle class? I need for the variables to be able to be defined in the constructor function and then I would hopefully be able to use these values in later equations. So would use say this strategy would work? Thank you for all your help by the way it will help me greatly.
This method didn't work when I tried to change the formula for Fish, it also warns me that b,c,d may be unitialised. When I tried to change the fish formula, I tried (S0[1]-D)/(S0[2]) and when you input the values it comes up with a random number. Obviously there is something wrong but I can't quite see it at the moment.

By the way in reply to above, I definitely want to pass the values in to the constructor, I'm just a little unsure how.
Last edited on
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
/#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

class CRectangle {
private:
double S0;
double U;
double D;
double R;
public:
CRectangle (vector<double>,double,double,double);
double Fish();
};

CRectangle::CRectangle (vector<double> _S0,double _U,double _D,double _R)
{

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 CRectangle::Fish()
{
return (S0[1]-D)/(U-D);
}
int main () {
double b,c,d;
vector<double> S0(2);
CRectangle rect(S0,b,c,d);
cout << "rect area: " << rect.Fish()<< endl;

return 0;
}/
Last edited on
Hello everyone I have posted the code above and it coming up with two errors, invalid types 'double[size_t]' for array subscript on the line ""cin >> s0[i];" and invalid types 'double[int]' for array subscript on the line. Also if I do manage to get a value it comes out with an arbitrary value. Can anyone see why this is?
Topic archived. No new replies allowed.