error: no match for call to ‘(Pair<std::valarray<int>, std::valarray<int> >)

Hello,

I am solving the first exercise in fourteenth chapter from the C++ primer plus book and I get an g++(v 4.3.2/OpenSUSE 11.1) error annoucement
"43: error: no match for call to ‘(Pair<std::valarray<int>, std::valarray<int> >) (std::valarray<int>, std::valarray<int>)’"
when trying to compile following 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
#include <iostream>
#include <string>
#include <valarray>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::valarray;
template <class T1, class T2>
class Pair {
	private:
		T1 a;
		T2 b;
	public:
		T1 & first();
		T2 & second();
		T1 first() const {return a;}
		T2 second() const {return b;}
		Pair(const T1 & aval, const T2 & bval) : a(aval), b(bval) {}
		Pair() {}
};
class Wine {
	private:
		string name;
		Pair<valarray<int>, valarray<int> > ys;
		int arrsize;
	public:
// 		Wine(int as = 20, valarray<int> a = valarray<int> (0,20), valarray<int> b = valarray<int> (0,20));
		Wine(int as = 20);
		Wine(const char * l, int y, const int yr[], const int bot[]);
		Wine(const char * l, int y);
		void getbottles();
		string label();
		int sum();
};
// Wine::Wine(int as, valarray<int> a, valarray<int> b) : ys(a, b) {
// 	name = "";
// 	arrsize = 0;
// }
Wine::Wine(int as) {
	name = "";
	arrsize = 0;
	ys(valarray<int> (0,20), valarray<int> (0,20)); //On this line g++ finds the problem
}
int main(void) {
	return 0;
}


If the "commented" version of Wine constructor is used, g++ compile the code faultlessly. But in such a case I can not use argument "as" to set array size of valarray members in Pair class, which is what I would like.
Could anybody help me?

Thank you

talx
What is "ys"?
ys is an object of class Pair<valarray<int>, valarray<int> > that contains two objects of class valarray. These two valarray class objects are arrays of type "int" and contain arrays of vintages and number of bottles in storage. (ys is an abbreviation of "years and stocks".)
There is a problem with line 43 as the compiler says.
identifier_name followed by values in brackets is actually a function call.

What you really wanted to do was to initialise the ys member of the Wine class.
You initialise embedded class member variables using an initialiser list.
If you don't use an initialiser list, then you have to use assign them in the
body of the constructor (like you did with name in line 41) which
is less efficient than initialiser list.

So your constructor should look something like this:

1
2
3
4
5
6
//using initialiser list
Wine::Wine(int as) 
:name(""), arrsize(0), ys(valarray<int> (0,20), valarray<int> (0,20)) 
{

}


**Edit - I don't think you have to explicitly initialse a string to be empty because that is the default for string**
Last edited on
Thank you very much, it works now.
Topic archived. No new replies allowed.