Operator >> overload with struct? [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
overload operator<< within a class in c++
Operator overloading
Is there any way this is possible?
#include <iostream>
using namespace std;
struct test{
int n;
};
int main(){
test t1;
cin >> t1;
return 0;
}
For all I know, it is not possible, but I had an exam yesterday and that question came in it, it asked me to write the functions missing.

Add the include:
#include <stdlib.h>
You should include both namespaces:
using namespace System; // ie System::Console
using namespace std;
You'll need to use:
std::cin >> t1.n;

In C++, structs are the same as classes. So yes, you can do the same thing as you do with classes.

Related

Can't compare strings/chars in insensitive cases (error - methods not found) [duplicate]

This question already has answers here:
‘strcasecmp’ was not declared in this scope
(4 answers)
strncasecmp and strcasecmp has not been declared
(3 answers)
Closed 1 year ago.
I'm having problems with comparing two strings in case-insesitive way.
I've tried lots of methods(functions) like strcasecmp, stricmp, strcmpi, etc. I had imported the libraries where those functions belong but still it can't seem to detect them.
Here is my code:
#include <bits/stdc++.h>
#include <string.h>
#include <cstring>
#include <mem.h>
using namespace std;
int main() {
char s1[100], s2[100];
cin >> s1;
cin >> s2;
cout << strcasecmp(s1, s2) << endl;
return 0;
}
ERROR: project.cpp|13|error: 'strcasecmp' was not declared in this scope
Does anybody have suggestions on how to fix it?
Thank you.

C++: How to call a function (lets say funA()) from a header file inside my own function whose name is also funA()? [duplicate]

This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 2 years ago.
I want to call a function reverse(BidirectionalIterator first, BidirectionalIterator last) from <algorithm> header file inside my function, whose name is also reverse(int).
code:
#include<iostream>
#include<algorithm>
using namespace std;
class Solution{
public:
int reverse(int x){
string num = to_string(x);
reverse(num.begin(), num.end());
}
};
I thought it would automatically call the appropriate function based on the parameters passed just like function overloading. But, it doesn't.
I tried:
namespace algo{
#include<algorithm>
}
But it is giving a lot of errors.
Ahh, now you're experiencing the reason people on StackOverflow are always yelling about not using using namespace std;. The issue is that you're bringing the whole namespace into the global namespace, which'll lead to clashes like this.
If you delete that line, however, now all of your imported functions stay in the std namespace, so you could do:
#include<iostream>
#include<algorithm>
// BAD
// using namespace std;
class Solution{
public:
int reverse(int x){
std::string num = std::to_string(x);
std::reverse(num.begin(), num.end());
return std::stoi(num); // Don't forget to return!
}
};

I am new to C++ Programming , What is the difference between these codes , and Which one I should use? [duplicate]

This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 2 years ago.
I got both the codes from Books from an Online PDF
First -
#include <iostream>
int main()
{
std::cout << "Hello World!";
return 0;
}
Second -
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" ;
return 0;
}
No difference, using namespace std; simply means everything that is otherwise available via std namespace no loner needs the std:: prefix. In a cpp file its a personal preference. In an h file - don't use using namespace std;, this is because std namespace is huge, and you may be not the only one including that h. For a beginner, or 'academic' code in general it doesn't really matter, but believe me, when you are on the receiving end of someone pulling the entire std namespace in on you in a big project, you aren't gonna like it.

c++ template ambiguous error [duplicate]

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!

Using Namespaces vs Using Classes [duplicate]

This question already has answers here:
Namespace + functions versus static methods on a class
(9 answers)
Closed 8 years ago.
in c++ what is the difference between using a namespace and a class??
like::
in this example i added namespace
#include <iostream>
using namespace std;
namespace ns{
void print(){
cout<<"Hello, World!";
}
}
int main(){
ns::print();
return 0;
}
vs:
and in this one i added a class
#include<iostream>
using namespace std;
class cs{
void print(){
cout<<"Hello World!";
}
}
int main(){
cs classOject;
classObject.print();
return 0;
}
but both got me the same result;;;
that question made me keep thinking for a week
thanks for any replies guys and all repliers are much appreciated...
they are too different to describe here in detail. i recommend you to read something about oop.
classes are definition of objects, and namespaces can be used to build logical groups of code.