**
why the output of code is
x = 1 count = 0
x = 1 count = 1
x = 1.1 count = 0
**
//code for template
#include <iostream>
using namespace std;
template <typename T>
void fun(const T&x)
{
static int count = 0;
cout << "x = " << x << " count = " << count << endl;
++count;
return;
}
int main()
{
fun<int> (1);//for int
cout << endl;
fun<int>(1);//for int
cout << endl;
fun<double>(1.1);//for int
cout << endl;
return 0;
}
Is Compiler creates a new instance of a template function for every data type in c++ in above code and also how can we assign rvalue to reference variable while calling function fun() ?
In your code, you have used the stencil to create two functions, one function uses the int type, the other function uses the double type:
void fun(const int &x)
{
static int count = 0;
cout << "x = " << x << " count = " << count << endl;
++count;
return;
}
void fun(const double &x)
{
static int count = 0;
cout << "x = " << x << " count = " << count << endl;
++count;
return;
}
The compiler can recognize the second fun<int>(1) as a call to the above integer function, thus not needing to generate a third function.
Passing by reference or const reference is the same with template functions as it is with normal functions; the template only affects the data type, not how parameters are passed.
Related
If static variables has only one copy for the program. So why is it not possible to swap 2 numbers using another function?
Code:
#include <iostream>
void swap(int, int);
int main()
{
static int a = 1;
static int b = 2;
swap(a, b);
std::cout << "a = " << a << std::endl << "b = " << b << std::endl;
std::cin.get();
}
void swap(int a,int b)
{
int temp = a;
a = b;
b = temp;
std::cout << "a = " << a << std::endl << "b = " << b << std::endl;
}
As the 'swap' function is taking parameters as pass by value, copies of the variables are passed to the swap function which will only swap its local variables 'a' and 'b' (passed as parameter) not the static ones passed from main.
Swap should be taking parameters as references like below.
#include <iostream>
void swap(int&, int&);
int main()
{
static int a = 1;
static int b = 2;
swap(a, b);
std::cout << "a = " << a << std::endl << "b = " << b << std::endl;
std::cin.get();
}
void swap(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
std::cout << "a = " << a << std::endl << "b = " << b << std::endl;
}
Please note that static variable defined in a function pertains its value in the subsequent calls of the function in which it is declared.
This is because you are passing arguments by value and not by address(reference). Your function is working on a copy of a and a copy of b - not the original values. You could try this(notice the & in the function prototype and function definition arguments)
void swap(int &, int &);
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
std::cout << "a = " << a << std::endl << "b = " << b << std::endl;
}
Good evening, I'm attempting to call my void function "getProblems" in main, but get an extraneous value when outputting "getProblems" with no parameters. Similarly, when passing arguments, such as "getProblems(list, i)", I get the error "no operator '<<' matches these operands". The goal is to output the number of problems my text file contains without using a function that returns a value or using pointers.
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
int const MAX_PROBLEMS = 50;
void getProblems(string problem[], int& count);
int main()
{
string list[MAX_PROBLEMS] = {};
int i = 0;
cout << "There are " << getProblems << " problems. " << endl;
// I have also tried calling the void function with parameters
// cout << "There are " << getProblems(list, i) << "problems. " << endl;
return 0;
}
void getProblems(string problem[], int& count)
{
ifstream mathProblems;
mathProblems.open("P4Problems.txt");
if (!mathProblems)
{
cout <<"No file was found."<< endl;
}
count = 0;
string data;
getline(mathProblems, data);
while (!mathProblems.eof())
{
problem[count] = data;
count ++;
mathProblems >> data;
}
mathProblems.close();
}
Your function getProblems() is of type void, so what are you trying to display with cout?
If you need to display the count, which is an argument,
int main()
{
int count;
getProblems(listt,count); //assuming listt, has been declared before
cout << "There are " << count << " problems. " << endl;
return 0;
}
I'm trying to make something in C++ and I have a problem. I have this code:
#include <iostream>
#include <string>
//---MAIN---
using namespace std;
int af1 = 1;
int af2 = 1;
void lettersort(int cnt1) {
cout << "RESULT:" << cnt1 << endl;
cnt1++;
cout << "RESULT WHEN+:" << cnt1 << endl;
cout << "RESULT IN GLOBAL INT:" << af2 << endl;
}
int main()
{
lettersort(af2);
return 0;
}
So is there any way so that cnt1++ affects af2 too, to make it bigger ? I don't want to use af2++ directly because I want to sometimes use af1.
At the moment you are just passing af2 to cnt1 by value, so any changes to cnt1 are strictly local to the function lettersort. In order to get the behaviour you want you need to pass your cnt1 parameter by reference. Change:
void lettersort(int cnt1)
to:
void lettersort(int &cnt1)
You are passing the argument by value. I.e., you are copying the value of af1 to a local variable in lettersort. This integer is then incremented, and disposed of when the function ends, without affecting the original af1. If you want the function to be able to affect af1, you should pass the argument by reference:
void lettersort(int& cnt1) { // Note the "&"
if i understood your question:
there are 2 ways you can do that.
make lettersort function return the new value, and put it in af2
int lettersort(int cnt1) {
cout << "RESULT:" << cnt1 << endl;
cnt1++;
cout << "RESULT WHEN+:" << cnt1 << endl;
cout << "RESULT IN GLOBAL INT:" << af2 << endl;
return cnt1;
}
int main()
{
af2 = lettersort(af2);
return 0;
}
pass the value by reference. you can read about it here, but generally its about passing a pointer to that value. meaning whatever you do on the argument you are passing, will happen on the original var.
example:
void foo(int &y) // y is now a reference
{
using namespace std;
cout << "y = " << y << endl;
y = 6;
cout << "y = " << y << endl;
} // y is destroyed here
int main()
{
int x = 5;
cout << "x = " << x << endl;
foo(x);
cout << "x = " << x << endl;
return 0;
}
here you have to just modified the argument pass to lettersort
function as passed by reference.
for example if you declare and initialize any variable like:
int a=10; int &b = a;
now a and b refer to the same value.if you change a then the changes
also reflect in b also.
so,
cout << a; cout << b;
both statement produce the same result across the program. so using
this concept i modified the function argument and made it as by
reference.
your correct code is :
#include <iostream>
#include <string>
using namespace std;
int af1 = 1;
int af2 = 1;
void lettersort(int &cnt1) {
cout << "RESULT:" << cnt1 << endl;
cnt1++;
cout << "RESULT WHEN+:" << cnt1 << endl;
cout << "RESULT IN GLOBAL INT:" << af2 << endl;
}
int main()
{
lettersort(af2);
return 0;
}
I'm using Template keyword to run simple swap program, Kindly help me why my program is not working out?
#include<iostream>
using namespace std;
template<typename T>
void Swap(T m, T n)
{
T temp;
temp = m;
m = n;
n = temp;
}
int main()
{
int i = 5, j = 6;
cout << "Before swapping:" << endl;
cout << i << " and " << j << endl;
Swap(i, j);
cout << "After Swapping:" << endl;
cout << i << " and " << j << endl;
return 0;
}
Output:
You are creating copies of your arguments because you are taking them by value. If you want to update them within the function you should take them by reference:
void Swap(T& m, T& n)
// ^ ^
Additionally, your implementation has an implicit constraint that T must be default-constructible, which is not necessary for swapping. You should construct your temp variable directly:
T temp = m;
You'd also be better off using move semantics to avoid making copies:
T temp(std::move(m)); // or T temp = std::move(m);
m = std::move(n);
n = std::move(temp);
How do I make the following overload work
#include <iostream>
using namespace std;
int subtractFive (int a)
{
a = a - 5;
return a;
}
int subtractFive (int &a)
{
a = a -5;
return a -5;
}
int main()
{
int A = 10;
cout << "Answer: " << subtractFive(*A) << endl;
cout << "A Value "<< A << endl;
cout << "Answer: " << subtractFive(A) << endl;
cout << "A Value "<< A << endl;
return 0;
}
Tried but doesnt compile
#include <iostream>
using namespace std;
int subtractFive (int a)
{
a = a - 5;
return a;
}
void subtractFive (int* a)
{
*a = *a -5;
}
int main()
{
int A = 10;
cout << "Answer: " << subtractFive(A) << endl;
cout << "A Value "<< A << endl;
subtractFive(A);
cout << "A Value "<< A << endl;
return 0;
}
You might try specifying an overload that takes an address as an argument:
int subtractFive (int *a)
{
*a = *a -5;
return *a -5;
}
Declare one function as pass by address the other by value or reference:
void subtractByFive(int * p_value)
{
if (p_value != NULL)
{
*p_value -= 5;
}
return;
}
A value and a reference have the same type so you can't overload on it. If you want two functions one of which modifies its parameter and one that returns the new value then you either have to give them different names or different types (e.g. make the latter function use a pointer type).