Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <iostream>
using namespace std;
void printValues(________________________) //missing line of code
{
cout << val1 << " " << val2 << endl;
}
int main()
{
printValues(10, 20);
printValues(30, "Hello");
printValues(2.5, 7.5);
return 1;
}

What I have tried:

I Have tried using
(int val1,int val2,string val2 = "Hello", float val1 = 2.5,float val2 = 7.5)
Posted
Updated 9-Oct-22 5:40am

You can tempatize the printValues function like this:

C++
template<class T1, T2> void printValues(T1 val1, T2 val2)
{
    cout << val1 << " " << val2 << endl;
}


or use shorthand version of the above:

C++
void printValues(auto val1, auto val2)
{
    cout << val1 << " " << val2 << endl;
}


This will allow the compiler to deduce your types and generate all the overloads.
 
Share this answer
 
v2
Comments
merano99 9-Oct-22 10:38am    
With which compiler and C++ standard should the shorthandversion work?
I get the error message that a function parameter must not have a type that contains "auto". But I would generally assume that you could declare the parameter as a class, which can then contain several data types and can also output it again via corresponding ostream overloads.
The template version works with
template<class T1, class T2> void printValues(T1 val1, T2 val2) ...

Have my 5!
steveb 9-Oct-22 13:57pm    
auto specifier is c++11 standard.
Greg Utas 10-Oct-22 9:47am    
It doesn't compile for me in C++17, for the same reason given by merano99 ("auto not allowed here"). It's actually C++20. C++11 only had auto variables and return types.
steveb 11-Oct-22 8:52am    
Ooops. I think I built that in VS 2022 with the C++20 standard enabled. The coolest feature ever I must admit
Greg Utas 11-Oct-22 8:57am    
A 5 for you regardless! I hadn't heard of it. It appears to simply reduce boilerplate (the more verbose template function version) without actually providing anything new.
You can't pass "variable types" as parameters - they could be significantly different lengths and the system can't cope with that.
Instead, you would have to pass a pointer to the value as a void *, and then work out what type it was before printing it.

A better solution would be to use C++ function overloading[^] to create functions that have different signatures, and thus accept different types.
 
Share this answer
 
Comments
54 EE319 VIGNESH B 9-Oct-22 5:35am    
I have to do within this one line of code and it is thus possible as few have solved it
These lines of code is preset and I cannot do any change to other lines of code.
Could you type me that line of code to be entered.
Dave Kreskowiak 9-Oct-22 10:12am    
Uhhh, if you know other people have "solved it", why don't you use their line of code?
merano99 9-Oct-22 11:45am    
Yes, no problem. See my solution 3
As Richard had already pointed out, a data type is needed that fits all passed parameters. As usual you can define such a type yourself, if nothing fits in the standard. My suggestion here would be a class multitype, which provides an overload for all needed parameter data types and can also restore these data types as ostream. The call would then look as desired:
C++
void printValues(multitype val1, multitype val2)
{
	cout << val1 << " " << val2 << endl;
}

int main()
{
	printValues(10, 20);
	printValues(30, "Hello");
	printValues(2.5, 7.5);
	return 0;
}
Since the multitype data type does not yet exist, it must first be defined. This could look like this, for example:
C++
class multitype {
public:
	multitype(int d);
	multitype(double d);
	multitype(std::string mts) :_s(mts) { _typ = T_STRING; };
	multitype(const char* mts):_s(mts) { _typ = T_STRING; };
	enum DDTYPE { T_NONE, T_INT, T_DOUBLE, T_STRING };
	DDTYPE getype() { return _typ; };
	std::string getdata_s() { return _s; };
	friend ostream& operator<<(ostream&, multitype&);
private:
	DDTYPE _typ;
	int  _d;
	double _dd;
	string  _s;
};
The rest is housework and I like to leave that to the questioner. I still briefly give some outlines here:
C++
multitype::multitype(int d) { /* TODO */  };
multitype::multitype(double d) { /* TODO */ };
ostream& operator<<(ostream& os, multitype& mt) { /* TODO */ return os; };
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900