This question already has answers here:
Unexpected Result in Macro
(2 answers)
Sequence points and partial order
(3 answers)
Closed 7 years ago.
The code below should return the value 216 but it returns 392 that is (7*7*8) . Can somebody please explain me how?
#include<iostream>
#define cube(x) (x*x*x)
using namespace std;
int main()
{
int x=5;
cout<<cube(++x);
cout<<endl;
return 0;
}
After macro expansion you will get:
using namespace std;
int main()
{
int x=5;
cout<<(++x*++x*++x);
cout<<endl;
return 0;
}
and it is undefined behavior.
Just use a function instead of the cube macro:
int cube(int x) {
return x * x * x;
}
You should really use a function and not a macro. The cube(++x) expands to
++x*++x*++x
which isn't at all what we want.
Related
This question already has answers here:
Double delete of data doesn't crash
(3 answers)
What happens in a double delete?
(8 answers)
Closed 9 months ago.
I have written the following program which should give runtime error of double free corruption.
#include <iostream>
#include <memory>
using namespace std;
int main()
{
shared_ptr<int> shared3(new int);
*shared3 = 9;
int *raw = shared3.get();
delete raw;
return 0;
}
A core dump should have come but it didn't. Please tell me how can I get double free runtime error in my environment?
This question already has answers here:
Can a declaration affect the std namespace?
(2 answers)
Why doesn't adding sqrt() cause a conflict in C++? [duplicate]
(1 answer)
Closed 2 years ago.
#include<iostream>
double sqrt(double);
int main()
{
double a = std::sqrt(4.0);
std::cout << a;
return 0;
}
double sqrt(double a)
{
return 1.0;
}
I know I declare the sqrt at first, but I use std::sqrt, it still call my own sqrt. why?
Search your code for a using namespace std; somewhere. Perhaps hidden in some other set of include files.
This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Why does this C++ snippet compile (non-void function does not return a value) [duplicate]
(7 answers)
Closed 2 years ago.
#include <iostream>
using namespace std;
int test(int numb) {
if (numb == 0) return 0;
test(numb - 1);
}
int main() {
cout<<test(10);
}
so i was solving some algorithm questions.
this code works in visual studio but not in other online shell.
what does test() function returning even though there is no return?
also
#include <iostream>
#include<stdio.h>
using namespace std;
int test(int numb) {
if (numb == 0) return 0;
cout<<test(numb - 1)<<endl;
}
int main() {
cout<<test(10);
}
//result
0
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
what does thoes number 1349009568 means??
Your function has undefined behavior when it doesn't return anything. The number 1349009568 is meaningless and probably random. For me, it shows a 0, and has the same output as this version which will never return anything.
int test(int numb) {
if (false) return -1;
}
This question already has answers here:
Why Template Function call is ambiguous?
(3 answers)
Closed 5 years ago.
Here's my code trying to simply add 2 numbers.
#include <iostream>
#include <string>
using namespace std;
template<class first, class second>
first plus(first x, second y) {
return x + y;
}
int main() {
int a = 123;
int b = 21;
plus(a, b);
return 0;
}
The plus() gives me an error stating that it's "ambiguous". This is basically copied code I've seen in tutorials(where it has worked!) on templates so I'm really confused now.
Remove the using namespace std, you are colliding with std::plus
http://en.cppreference.com/w/cpp/utility/functional/plus
I've solved the issue either removing std namespace or changing the function name is all it takes!
This question already has answers here:
call of overloaded 'min(int&, int&)' is ambiguous
(5 answers)
Closed 6 years ago.
#include<conio.h>
#include<iostream>
using namespace std;
template<class T>
T min(T a,T b)
{
return (a>b)?a:b;
}
int main()
{
int x,y;
cin>>x>>y;
cout<<"min. of integer value is="<<min(x,y); //error is call of overloaded function is ambiguous.
float p,q;
cin>>p>>q;
cout<<"min. of floating value is="<<min(p,q);//same error as above
char c1,c2;
cin>>c1>>c2;
cout<<"min. of c1 and c2(basis of ASCII values)="<<min(c1,c2);// same error as above
getch();
return 0; }
Is there any inbuilt feature of dev c++ that doesnt support templates or is there some other error?
The reason is there is a std::min, which gets imported into the global namespace (due to the using namespace std;).
So you have 2 different versions of min: Your version (which actually returns the maximum...), and the standard min.
Either rename your min-function, or remove it (and use std::min instead).