0

I have a weird problem when trying to print out the content of a vector. I'm using Visual Studio Code with the CMake extension.

I can print out simple text using cout

#include <iostream>
#include <vector>

using namespace std;

int main() {

    cout << "test" << endl;

    return 0;
}

But I can't print out the vectors content

#include <iostream>
#include <vector>

using namespace std;

int main() {

    vector<int> test = {1,2,3};

    cout << "test" << endl;
    cout << test[1] << endl;

    return 0;
}

I've never really worked with C++ vectors, so I'm probably missing something fairly obvious but I followed a C++ vector tutorial step by step and for them, the output works fine.

Cheers,

Luca

Lardos
  • 11
  • 1
  • 1
    Don't post pictures of text, post text as properly formatted text. – Jabberwocky Nov 25 '21 at 11:29
  • The program seems fine. Does the compiler output some errors or warnings? Are you compiling in at least C++11 mode – PeterT Nov 25 '21 at 11:31
  • did you recompile after change your code? – che.wang Nov 25 '21 at 11:43
  • @Jabberwocky Sorry, I thought I would be easier to see whats going on. I will do it next time as you suggested – Lardos Nov 25 '21 at 11:53
  • @PeterT No errors or warnings. I've put "set (CMAKE_CXX_STANDARD 11)" in my CMakeLists which should activate C++ 11 – Lardos Nov 25 '21 at 11:53
  • @che.wang Yes, I recompiled my code without any errors – Lardos Nov 25 '21 at 11:53
  • 1
    Hmm. Which compiler are you using? I've had issues with programs sometimes not finding the runtime libraries. Do you see any error if you use windows explorer to go to the folder and double click the *.exe file? – PeterT Nov 25 '21 at 11:56
  • @PeterT I'm using the GCC compiler Version 11.2.0 I don't see any error when launching the exe directly from the folder – Lardos Nov 25 '21 at 12:09
  • Code is fine. Can you give details of your OS, editor or IDE that you are using and C++ compiler (version) – vish4071 Nov 25 '21 at 12:47
  • This is the duplicate(or same as) of [C++ Vector not printing on terminal](https://stackoverflow.com/questions/70105722/c-vector-not-printing-on-terminal#comment123927994_70105722) asked few hours ago. – Jason Nov 25 '21 at 12:52
  • @Lardos I had this exact issue once. The way i solved it was that . **Step 1**: Create a new separate directory/folder. **Step 2** Inside the new folder, create a new file named main.cpp. **Step 3** Now compile this file using g++ main.cpp -o program on the terminal. The above should definitely work. The problem as i realized in my case was that it was building some other old files/project.So i created a new directory and inside it a new main.cpp and then compiled it,and it worked. Hope it works for you too.Do let me know if it does/doesn't. – Jason Nov 25 '21 at 12:54
  • @AnoopRana: Looks for me to wrong build process and linking with gcc instead of g++. Maybe you have some intermediate Makefiles or other stuff generated by cmake. Please test again by manually compiling with g++ without any parms and start a.out. If that is not working, you have definitly a broken compiler setup. BTW: Why we need c++11 for this example? – Klaus Nov 25 '21 at 13:18
  • @Klaus `error: in C++98 'test' must be initialized by constructor, not by '{...}'` – PeterT Nov 25 '21 at 13:42
  • @PeterT: Sorry, miswording, my point was: "GCC compiler Version 11.2.0", so I did not expect we have to set this manually... :-) – Klaus Nov 25 '21 at 13:46
  • you don't have to set it for for gcc11, my hint to C++11 came before the compiler version was posted – PeterT Nov 25 '21 at 13:47

1 Answers1

0

I'm new to C++ and have exactly the same problem.

Temporary workaround as posted by Luckylone here would be to copy libstdc++-6.dll from your mingw64\bin folder into your project folder.

Something is messing up our link with the resources even though folders are properly added to system PATH.

EDIT: After an afternoon of troubleshooting, I've solved my problem by uninstalling compilers (originally provided through WinLibs) and reinstalling them using MSYS2.

Delete the existing Mingw64 folder, remove the PATH variables, then carefully follow these instructions. Keep in mind that I had to add mingw64/bin to both user and system Path, and restart VS Code before damn std::vector finally started to print.

Dragan
  • 21
  • 3
  • The comments suggest adding static linkage flag for MinGW. [link to answer](https://stackoverflow.com/a/6405064/12447766) – Burak Nov 19 '22 at 14:46
  • This, and other solutions I tried didn't worked for me, but reinstalling compilers did. I've made updates to my answer. – Dragan Nov 20 '22 at 11:13