Error C2664 while using classes.

I'm trying to write a small code in order to learn classes. I used one of the examples posted in the reference section of this site as a template. However, when I compile, I get a C2663 error with the below code.

If anyone could give some direction to what should be changed I would be much obliged.
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
#include <iostream>
/* This class is to calculate the adjusted, weighting denominator for use in the calculation of the
individual gas presence percentages.*/
class Weighted_Gases{
private:
	int C2H2, C2H4, CH4;
public:
	Weighted_Gases(int Act, int Eth, int Meth) : C2H2(Act), C2H4(Eth), CH4(Meth) {}//Member initialization
	double denominator() { return C2H2 + C2H4 + CH4; }
};
/* This class is to calculate the percentage of Acetylene present int the Transformer oil test sample.*/
class Duval_Triangle_C2H2{
private:
	int iC2H2;
	Weighted_Gases Added_Fault_Gases;
public:
	Duval_Triangle_C2H2::Duval_Triangle_C2H2(int Acetylene, double added) : iC2H2{ Acetylene }, Added_Fault_Gases{ added } {}//Memeber initialization
	double Percent_of_C2H2() { return iC2H2 / Added_Fault_Gases.denominator() * 100; }
};
int main(void)
{
	Duval_Triangle_C2H2 Acetylene(45, 350);
	std::cout << "The Percentage of Acetylene is: " << Acetylene.Percent_of_C2H2 << "% \n";
	std::cin.get();
	return 0;
}
Last edited on
A few things going on here:


17:2: error: extra qualification 'Duval_Triangle_C2H2::' on member 'Duval_Triangle_C2H2' [-fpermissive]
 In constructor 'Duval_Triangle_C2H2::Duval_Triangle_C2H2(int, double)': 
17:119: error: no matching function for call to 'Weighted_Gases::Weighted_Gases(<brace-enclosed initializer list>)' 
17:119: note: candidates are: 
8:2: note: Weighted_Gases::Weighted_Gases(int, int, int) 
8:2: note: candidate expects 3 arguments, 1 provided 
4:7: note: constexpr Weighted_Gases::Weighted_Gases(const Weighted_Gases&) 
4:7: note: no known conversion for argument 1 from 'double' to 'const Weighted_Gases&' 
4:7: note: constexpr Weighted_Gases::Weighted_Gases(Weighted_Gases&&) 
4:7: note: no known conversion for argument 1 from 'double' to 'Weighted_Gases&&' 
In function 'int main()': 
23:63: error: invalid use of non-static member function


So it looks like you need to provide the right number of arguments, of the right type. That would be a good start.

With your formatting, you can split up lines with lots of parameters, 1 per line - it's easier to read that way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Weighted_Gases{
private:
	int C2H2;     // one variable per line
        int C2H4;     // consider making these unsigned int
        int CH4;
public:
	Weighted_Gases(const int Act,  // parameters normally should be const
                       const  int Eth, // unless passing by ref or pointer
                       const int Meth) 
            :  //Member initialization
            C2H2(Act), 
            C2H4(Eth), 
            CH4(Meth) 
         {
                    // do validation here, throw if not valid
         }

	double denominator() const { return C2H2 + C2H4 + CH4; }
};



Consider separating the class definitions into a *.hpp header files, with class function definitions into *.cpp files, name the files the exactly the same as the class name.

Good Luck !!
Last edited on
Thanks! This helps a lot. I need the parameters to be dynamic so I should probably pass by reference (pointers still make my head hurt, but I'm working on it ;) ). I'm going to play around with it more, but this works...thanks much.
Topic archived. No new replies allowed.