C++ indentifier "read" conflicts? - c++

Recently I'm working on my toy final c++ project, which will read content from a file at the begining and then process it.
Here's the simplified code.
#include <iostream>
#include <fstream>
//using namespace std;
struct command{
int a;
};
command read[1];
int main() {
std::ifstream fin;
fin.open("123.txt");
if (!fin){
std::cout << "failed" << std::endl;
return 0;
}
char c;
fin >> c;
std::cout << c;
return 0;
}
It works fine with Visual Studio 2019. However, when I'm trying to use devc++ 5.11 TDM-GCC 4.9.2, a strange bug happens. I get a segmentation fault on line fin>>c;, with return code 3221225477.
With great effort, the easiest way to make this code works is changing the identifier read to names like reading or whatever. Besides, moving the line command read[1]; into main function also helps.
My questions are:
Is it a behavior related to the compiler? MSVC is fine but GCC 4.9.2 is a little bit old or ...?
Does the identifier read conflict with something in my code? Why does it not a compile error but a segmentation fault?
Why does moving the declaration of read into main function help?
Update:Thanks for tips and I removed using namespace std. I think it has something to do with ifstream, because just std::cout<<"hello world"; works.
-Wall -Wextra provides no warnings.

GCC compiler is stricter than other compiler.
According to tadman's describe, we can guess there's read symbol in namespace std.
So you put it out of main,it's a conflict, you put in the main, it become a local symbol.

There is very likely a collision happening between your read and another in the global namespace. Not all compilers handle this situation the same way, but you can always avoid it if you are careful.
Visual Studio's Intellisense (or whatever alternative you prefer) can help you identify what symbol your read is colliding with. To do that, scope or comment out your code, then start typing "read". If there's another read in your scope, you'll likely see it and be able to get information about it.
This is one of the reasons I don't like using namespace std;. It pollutes your global namespace with a bunch of stuff and increases your chance of collision with a standard library identifier.

Related

VS Code: Declaring a stack leads to problems with cout

I am learning C++ and was writing a simple program when I noticed that declaring a stack caused cout to not output to the terminal. Here is my program:
#include <iostream>
#include <stack>
int main ()
{
std::stack<int> myStack;
std::cout<<"Hello World!"<<std::endl;
return 0;
}
This problem only occurs in the Visual Studio Code editor. If I use an online C++ compiler, there are no issues and I see "Hello World!" as output. If I remove the declaration of the stack, the output is also normal, even in VS Code. However, when the declaration is present, I see no output when I compile and run my code in VS Code. I have no idea why this would be the case, and other STL containers (I've tested vector, array, and set) do not cause a similar issue, although the problem still occurs when queue is used.
Any help resolving this issue, or any insight as to why it occurs in the first place, would be greatly appreciated.
p.s. This is my first time asking a question, so I apologize if it is poorly written.

I'm just starting c++, and I'm having a lot of trouble with VSCode [duplicate]

This question already has answers here:
How do I set up Visual Studio Code to compile C++ code?
(14 answers)
How does one set up the Visual Studio Code compiler/debugger to GCC?
(7 answers)
Closed 1 year ago.
So, I created a new project, here is my code:
#include <iostream>
using namespace std;
int main()
{
return 0;
}
For whatever reason, #include has an error, and the lightbulb won't give me any solutions. I have the code runner and c++ extensions installed.
I'm getting errors left and right for no reason including:
A red line under #include
Running the code doesn't work and says "'g++' is not recognized as an internal or external command,operable program or batch file.
" which doesn't even exist.
And so much more.
It depends on how you start off learning C++. At the very least your starter sandbox code should look like this:
#include <iostream>
int main()
{
return 0;
}
and you should have no problems.
When it comes to introducing input/output operators, the use of using namespace std; is a polarizing one.
Method 1:
#include <iostream>
using namespace std;
int main()
{
return 0;
}
Some people don't want you using it at all because ... what if you decide to create something called "namespace" yourself? In fact, this school of thought is very fearful of global things for reasons that escape me. Then you would have to take it out and rely on the traditional method ...
Method 2: eschewing namespace and using std::. You would have to use std::cout for printing text output to the prompt and std::cin for prompting input from the user.
Also, it looks like you are encountering multiple problems, so try attacking them one at a time. I haven't worked with g++ in a long time, so it sounds like you should first do some research on how to compile and run code with g++.

Defining a vector breaks program - MinGW

I have been coding for quite a few years now, but have only just recently started getting into C++.
I have already made quite a few programs in it, but have recently started running into some odd behaviour. The cases are simple enough that I expect this to be an error with my environment, and not the language itself, but I have run into a dead end.
Consider this simple program:
#include <iostream>
using namespace std;
int main() {
cout << "Test" << endl;
return 0;
}
If I compile that and run it, I get, as expected, "Test", in my console.
Now, if I add a vector to it:
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << "Test" << endl;
vector<double> whatever;
return 0;
}
For some reason, I do not get any output from that.
I have tried initalizing the vector as an empty vector aswell as with predefined values.
I tried adding a for loop running from 0 to 2^32 to see if the program failed entirely, or if it is just the output. This is where things got even weirder.
At first, I placed the loop before defining the vector; that caused the cout to suddenly work again (i.e. "Test" was printed in the console), aswell as stalling the program as expected. I then moved the for loop to after the vector definition, and then it broke entirely; I received no output, and the program exited almost instantly without error.
The issues persist when I remove the using namespace std; and prefix my cout and vector with std::
I use the g++ v6.3 compiler from MinGW. I am running Windows 10.
I am aware that this problem is probably extremely hard to reproduce, but I'll try my luck here before throwing my computer out the window.
Thanks in advance.
Edit: I fixed the issue by using Cygwin instead of MinGW. I will leave the question open in case someone has encountered a similar issue and has a fix that doesn't involve abandoning MinGW
I am running Linux and ran the code, and it ran successfully.
So this must be a compiler issue.
Maybe try using a different compiler like Cygwin / Microsoft Windows SDK.

Why and how can "using namespace" declarations confuse the compiler in C++?

I have read about the "cout/cin is ambiguous" plenty of times by now, from many different kinds of sources, and I keep hearing:
"Use 'std::' as a prefix, since it elsewise might confuse your compiler"
Now, I do know that not using the scope operator can be a bad practise according to some, and that people who usually receives this sort of error is on an elementary stage in their programming. However, the sources says that I'm supposed to understand how this work with time, which unfortunately, I don't yet. I still receive the errors occasionally (since I, despite all the warnings, still use "using namespace" declarations), and I still don't get why it works in 99% or the cases, and not in the other.
I'm degugging my code in Visual Studio express (2012), and of course, all the required sources are included (which in this case is the iostream library), so there isn't any sort of permanent error; it actually tends to popup quite randomly (a couple of times for instance, I've changed a piece of code, making the error appear, then even after changing it back, the error persists)... Really, I don't see any logical explaination to why the error appears in random contexts - hence why I'm asking:
What is causing this error, exactly (what does it mean, and why and how does it confuse the processor)? Moreover, is it worth skipping these declarations because of that? Can't it be fixed somehow?
Thanks in beforehand!
This is how you can confuse it by doing using namespace ... religiously. Now suppose some library you are using has defined a function called foo. Let's assume it's boost or whatever. Maybe even your own namespace called "op". You have a function called foo. My namespace may have the same function signature as yours. If you import both namespaces, how will the compiler know which to call? See below for example.
#include <cstdio>
namespace me
{
void foo()
{
printf("%s", "Calling me::foo\n");
}
}
namespace op
{
void foo()
{
printf("%s", "Calling op::foo\n");
}
}
using namespace me;
using namespace op;
int main()
{
foo(); //abiguous call.. Which foo is it going to use? op::foo? Or me::foo?
//now remove the using namespace me and using namespace op.. then try the below:
me::foo();
op::foo();
//The compiler now knows which foo you're trying to call and calls the correct one.
}

Error in compiling c++ program in Visual Studio 2010

I can't get my following c++ program compiled in Visual Studio 2010. I already have a working build of the same code so I know the code is correct. But I have no idea with what or how it was compiled.
So I would appreciate if someone could just copy the code and try to compile it in VS 2010.
Code:
http://codepad.org/4VtrVBdK
new:
Ok, I did editing according to the comments below. Now the only problems that seem to have remained are related to calls to the overloaded functions. So how to go about it?
so I know the code is correct
What you "know" is false. The code is wrong in many ways. Here is but one example:
for(unsigned int i=0;i<GPNO;i++) //SORTING ACCORDING TO FITNESS
for(unsigned int j=i+1;j<GPNO;j++)
if((gp[i]->fitness)>(gp[j]->fitness))
{
gp[i]->mycopy(tmp);
gp[j]->mycopy(gp[i]);
tmp->mycopy(gp[j]);
}
for(i=1;i<=no;i++)
{
gp[i]->mycopy(gp[GPNO-i]);
}
In the second for loop, i is undeclared. I suspect the original compiler was VC6, which allowed this.
Another problem is the way you're calling pow. You call it with macros (which are patently evil for this purpose), for instance:
pf[i].frq+=(unsigned int)pow(2,2*PF-1);
And the compiler doesn't know which version of pow you had in mind. Case in point for macros being evil for this purpose. Do this:
pf[i].frq+=(unsigned int)pow(2.0,2*PF-1);
Or better yet, get rid of the macros.
Another example of your code being wrong:
#include "stdlib.h"
#include "conio.h"
#include "math.h"
None of these includes are part of the Standard. If you can get them to compile, its only because your compiler was anticipating your mistake. But it's still a mistake.
Looks like you're missing using namespace std;