Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Yes I know using namespace std is bad practice, but I have already written a majority of the code with this declaration in place, and I don't think I have the time to go back and modify it.
The reason for the global variable is that I'm using multiple threads which need access to this variable and need to modify it.
My question is, I have int remainder = 0; declared globally, and within my master thread I call remainder = 13 % 5; for example.
This gives me an error saying 'int remainder' redeclared as a different kind of symbol and I've read that the reason is that using namespace std overrides the std::modulus operator, if I understood that correctly.
What other methods can I use to perform this function, keeping using namespace std and remainder as a global variable?
#include<iostream>
#include<cmath>
using namespace std;
int remainder = 0;
void testing();
int main(){
testing();
cout << remainder << endl;
return 0;
}
void testing(){
remainder = 13 % 5;
}
The problem is your global variable name conflicts with std::remainder from the standard library. Example on Compiler Explorer.
The problem with using namespace std; is that it brings so many symbols into the global namespace that this error is almost inevitable. It's a bad practice for anything but the simplest toy programs.
The conflict is with std::remainder, not with %. The variable name you've chosen conflicts with a function in the std namespace. You already know using namespace std; is bad, so I'll spare you.
Options:
Lose the using statement.
Rename the remainder variable.
Put the remainder variable in its own namespace, and explicitly refer to it through that namespace.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
To ensure readability I want to keep the type names as short as possible. Below are 4 versions of a trivial code. What is the best practice to adopt? Are there even better ones?
Version a:
#include <iostream>
main ()
{
::std::string s;
s = "hello";
::std::cout << s << ::std::endl;
}
Version b:
#include <iostream>
main ()
{
std::string s;
s = "hello";
std::cout << s << std::endl;
}
Version c:
#include <iostream>
using namespace std;
main ()
{
string s;
s = "hello";
cout << s << endl;
}
Version d:
#include <iostream>
main ()
{
using namespace std;
string s;
s = "hello";
cout << s << endl;
}
Is it a good idea in a .cpp file to always explicit the full name of a type (i.e. to write "std::string" instead of "string")?
Yes, it is a good idea to write std::string.
"String" is an extremely generic name, and a common programming concept. There are many string types across many libraries. You cannot know whether string refers to std::string or perhaps some other type in the current namespace, without parsing the entire code to see if there are using namespace or using declarations.
std is very specific. It's not a word in English. Programmers typically don't define namespaces by that name (although it would technically be allowed to define another_ns::std). Programmers immediately know that std::string refers to, with a very high likelihood, ::std::string.
Knowing what entities names refer to is an essential part of understanding source code.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Codes bellowing compiles:
namespace x
{
namespace y
{
namespace x
{
struct C
{
C ()
{
std::cout << "x::y::z::C" << std::endl;
}
};
}
}
struct C
{
C ()
{
std::cout << "x::C" << std::endl;
}
};
}
x::C and x::y::x::C are not same, but it's sometimes confusing.
Why is x::y::x allowed in C++? Isn't it more clear to forbid this?
Why is x::y::x allowed in C++? Isn't it more clear to forbid this?
No offense, but I think your premise is seriously flawed.
Maybe you didn't notice but having names being the same on different levels of nesting is something very natural. Consider constructors. The fully qualified name of a constructor of class foo is foo::foo(). Nothing unusual is it?
Now what if I want to put my class inside a namespace called foo. I am not arguing that this is the best naming scheme, but from the top of my head I also see no reason to outright forbid it. The constructor would be foo::foo::foo() then.
Having a rule that would disallow such naming would lead to lots of frustration to anybody that wants to use such (possibly suboptimal, but thats just opinions) naming scheme while having absolutely zero gain for someone that does not want to use such naming. In total there would be no benefit.
It's similar to variables having the same name in different scopes. Technically valid. After all, at the assembly level there are no names, just pointers and sizes.
void foo()
{
int x = 1;
if (true)
{
int x = 2;
x = 3; // Whops
}
}
C++ is not a forgiving language, if you mess up with anything, including variables naming, you are on your own. If you want the language to save you, there are (plenty of) other languages to pick.
That said, MSVC (and probably other compilers) issues a warning when a declared variable hides another variable in an outer scope, so by reading compiler warnings you can be helped.
This question already has answers here:
Why would one use nested classes in C++?
(6 answers)
Closed 7 years ago.
so I've been playing around with C++. I was trying to figure out if it's possible to define a structure in the definition of another structure. And here's my code.
#include <iostream>
using namespace std;
int main(){
struct structure1{
int integer1;
struct structure2{
int integer2;
}struct2;
}struct1;
structure1 *s1 = &struct1;
s1->integer1 = 50;
cout<<"STRUCTURE ONE'S INTEGER: "<<s1->integer1<<endl;
cout<<"STRUCTURE ONE'S STRUCTURE2.integer2: "<<s1->struct2.integer2;
}
OUTPUT:
$ ./a.out
STRUCTURE ONE'S INTEGER: 50
STRUCTURE ONE'S STRUCTURE2.integer2: 0
From what I saw in the output it had seemed to be working. But I just don't understand why or how it worked. Is it good practice? Is there any application to this?
Thanks!
But I just don't understand why or how it worked.
Why wouldn't it work? It's perfectly legal C++.
Is it good practice?
Depends on how it's used.
Is there any application to this?
Definitely. For example when you only need structure2 inside structure1 and nowhere else. It's good to make its scope as small as possible.
For more information: Why would one use nested classes in C++?
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am replacing my #defines, for instance #define NUM_SLIDER_POSITIONS 5 for constant variables. Should I keep the old naming like:
const unsigned int NUM_SLIDER_POSITIONS = 5;
Or should I use something more like:
const unsigned int kNumSliderPositions = 5;
.
EDIT: The post has been put on hold, but anyway I'd like to sum up your answers:
Other option would be using underscores as a separators using lower case letters:
const unsigned int num_slider_positions = 5;
Constant identifier.
Regarding the use of a prefix as a way of identifying constants , the most common options are not using it, as it may not add relevant information:
const unsigned int num_slider_positions = 5;
Use a "k" before the name:
const unsigned int k_num_slider_positions = 5;
Or declaring the variable inside a class or namespace, in order to avoid polluting the global scope and providing a more self-explanatory name:
namespace defaults // or "config", or "settings" or something like that
{
const unsigned int num_slider_positions = 5;
}
Client code:
int slider_positions = defaults::num_slider_positions;
I am replacing my #defines for constant variables.
Kudos! :)
Should I keep the old naming like: [all-caps]
If the coding conventions of your project designate constants to be in all-caps, you should (as it spares you an effort). Otherwise, you should not (because it will be confusing later, for maintenance).
Or should I use something more like: [bastardized hungarian convention]
This is up to you. Personally I do not like to add weird letters for my constants, because when reading the code - or writing it - I do not care much that they are constant (and if I try to write into them, the compiler will let me know).
My (personal) choice would be to use a namespace for providing context (instead of a prefix), along these lines:
namespace defaults // or "config", or "settings" or something like that
{
const unsigned int num_slider_positions = 5;
}
Client code:
int slider_positions = defaults::num_slider_positions;
I find this to be a superior alternative, because the context is more self-explanatory (than a "k" in front of it, or a "g" or a whatever else).
It's up to you for any name convention. But for C++ code you may also consider putting constants inside a class that use it, instead of pollute the global scope.
the naming convention of constant name with C++ is use a k followed by mixed case,
for constants defined globally or within a class, As a convenience to the reader, compile-time constants of global or class scope follow a different naming convention from other variables. Use a k followed by words with uppercase first letters
const unsigned int kNumSliderPositions = 5;
see more
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi i am kind of new in c++. I was trying to optimize my code. My code includes two for loops with a if block inside the second loop. first loop will iterate for 10^14 times and inner for loop will iterate for 10^4 times. My code is as folows
#include<iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
#include<stdio.h>
#include<fstream>
using namespace std;
#include<math.h>
#include<thread>
using namespace std;
signed long long run,i,j;
int main()
{
run=0;
for (i=0;i<100000000000000;i++)
{
for (j=0;j<10000;j++)
{
run=run+1;
}
}
cout<<run<<"\n";
}
time it is takling to complete is around 1 day. So I was using thread in my code to make it first. But it is showing to include -std=c++0x. So where to include this?
Is there anyone who would like to help me out?
I'd optimise it as follows, but good compilers might do that anyway:
#include<iostream>
signed long long run,i,j;
int main()
{
i = 100000000000000;
j = 10000;
run = i * j;
std::cout << run << '\n';
}
-std=c++0x is a compiling option, and it tells the compiler you are using C++11 standard.
You might want to take a look at a link like this one http://www.cs.cf.ac.uk/Dave/C/node3.html regarding compiling.