'C++ code wont run if a vector contains value
I am using VS-Code and Vim, Windows OS, MSYS2 MingW GCC/G++ (V.11.2.0) compiler.
If my code contains a vector there is no output when I run the code. I get no error/warning when compiling or running the code.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main () {
vector<string> test = {"Why", "Wont", "This", "Run", "??\n"};
cout << test[0] << test[1]<< test[2] << test[3] << test[4];
cout <<"This is a test";
return 0;
}
If I comment out:
//vector<string> test = {"Why", "Wont", "This", "Run", "??\n"};
//cout << test[0] << test[1] << test[2] << test[3] << test[4];
I get the output: "This is a test"
I have tried reinstalling the compiler with no luck.
Solution 1:[1]
I spent about 20 hours fighting this one and I lost -- at least on windows. My very smart friend said that there was "something wrong with my runtime" and recommended a fresh install of windows to try fixing it for that environment (I haven't tested that yet because I am lazy)
What did work was installing WSL (Windows Subsystem for Linux) and grabbing Ubuntu. C++ works as expected now, just launch VS Code through the bash terminal you'll get
Note: I had this same issue with a very similar setup. Laptop with VS Code, Windows 10, MSYS2 MingW GCC/G++ (V.11.2.0). If your error is very similar to mine, your code should run when the vector exists but does not have any contents. I could run C++ programs unrelated to vectors, or use vector's size() and empty() functions on empty vectors that were created. Allocating contents to a vector either on initialization or with push_back() was what prevented me from getting output. If you're able to compile other things and you have this issue without seeing extra info from -Wall
, the compiler is probably fine but not the runtime. I feel that this is moreso correct because I made test programs, compiled the .exes, and they could be run by another computer, just not my own.
My other attempted fixes included uninstalling and reinstalling MSYS2 and its compilers, VS Code, Microsoft Visual C++ Redistributables, several restarts, and more. Windows itself is the last ditch effort, I'm just waiting on making a backup.
Solution 2:[2]
Looks like C Trigraph sequence.
The ??
is special sequence from the old C days.
When followed by certain characters will convert to another. You sequence does not seem to be a valid Trigraph sequence but there may be an issue in VS. Trying putting space between the question marks.
Solution 3:[3]
Sorry, I can't comment since my contribution is too low... Aside from installing WSL, here's what works for me: stackoverflow.com/a/6405064/6871623
Basically add -static-libstdc++
when compiling.
I had the same issue with similar environment, except it's broken after I installed Visual Studio 2022 for C/C++ development which includes Windows 10 SDK tools.
Now I don't have to deal with several seconds loading WSL! (Solution by Harris B)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Harris B |
Solution 2 | Martin York |
Solution 3 | danzel artamadja |