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.
Related
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!
}
};
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.
I started learning C++ a few weeks ago. Now I'm trying to program a kind of shop as a challenge. I've made it 2 or 3 times before, but always in one program. This time I tried to put some functions I wrote in it, so the main file wouldn't be that messed up again.
The problem I'm having is, when I'm trying to import a function, I get this error message:
E0413 There is no suitable conversion function from 'std::basic_ostream<char, std::char_traits<char>>' to 'int'.
Here's the code:
Mainfile:
#include <iostream>
#include "Benutzer.h"
using namespace std;
int main()
{
user;
}
Function:
#include <iostream>
using namespace std;
int user
{
cout << "So you're a user. What do you want to buy?"
}
I know it's not much code by now, but I was already testing.
As I can see, your program has an error in syntax.
Two things you need to consider here:
How to define a function:
return_type func_name(data_type args){
/// function body
}
How to call a function:
func_name(args);
I verified your code with little change on my system.
This is correct code:
main_file.cpp
#include <iostream>
#include "Benutzer.h"
using namespace std;
int main()
{
user();
}
Benutzer.h
#include <iostream>
using namespace std;
int user()
{
cout << "So you're a user. What do you want to buy?";
return 0;
}
This works.
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:
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.