2

If I run this code in VScode using the g++ build settings it does not output anything. Unless I comment out "std::vector myvector = {10,20,30,40,50};" in which case it prints as expected. I found this out because using push_back() has the same effects.

#include <iostream>
#include <vector>
using namespace std;
 
int main()
{
    std::vector<int> myints;
    std::cout << "0. size: " << myints.size() << '\n';
    std::vector<int> myvector = {10,20,30,40,50};
    return 0;
}

Setup: VScode, MSYS MinGW X64, g++ build tasks.

Haven't experienced an issue like this, if you have a better way of making a dynamic array I'll stick with that.

Harris B
  • 71
  • 6
  • What version of GCC (/MinGW) are you using? Can't reproduce with GCC v6.1 through v11.2. – Brian61354270 Jan 22 '22 at 01:35
  • v11.2. -- nobody else can reproduce this either. Got a way to make a dynamic array without just implementing linked lists? – Harris B Jan 22 '22 at 02:00
  • 1
    If no one can reproduce this, maybe you can just try another compiler. I tried it on v11.1.0, and it works correctly. – Đumić Branislav Jan 22 '22 at 02:17
  • What on earth makes you think this is a "dynamic array"? You simply have an empty vector `myints` and then an initialized vector `myvector` -- that doesn't serve any purpose. – David C. Rankin Jan 22 '22 at 03:10
  • I'm not doing anything with it yet, I just want an array that I can append stuff to. Supposedly that's a vector but the code doesn't run for me if my vectors are going to have anything inside them. Allegedly I'm supposed to be able to insert things into these or add length but that doesn't run. – Harris B Jan 22 '22 at 03:39
  • Okay, the way it was phrased in the question -- had me scratching my head... – David C. Rankin Jan 22 '22 at 03:40
  • Do you do anything else besides a vector when you want a mutable array in C++? I'd be perfectly happy to just not use this type – Harris B Jan 22 '22 at 03:42
  • If those four lines of code are causing you headaches, then any comparable replacement is going to be equally damaging. You have a fundamentally broken compiler. Nothing you've done in that code is anything less than kosher. – Silvio Mayolo Jan 22 '22 at 03:50
  • How do I fix a broken compiler then? – Harris B Jan 22 '22 at 03:59
  • First, I'd compile with `-Wall` and `-Wextra` to see if anything pops up. If you get any warnings, that could give us a clue as to what on earth is going on. If you're going through an IDE, stop doing that and compile by command line, to remove that extra variable. Otherwise, burn the compiler and install it anew. gcc should not be giving you this much trouble; three commenters here (myself included) have been able to run your code flawlessly. – Silvio Mayolo Jan 22 '22 at 04:08
  • Figured that one out. Neither Wall or Wextra threw any extra warnings. – Harris B Jan 22 '22 at 04:41
  • 2
    Try using std::endl instead of "\n". If have seen weird effect if you do not flush on program end. – gerum Jan 22 '22 at 07:33
  • Related: [C++ code wont run if a vector contains value](https://stackoverflow.com/q/70994977/11082165) – Brian61354270 Feb 05 '22 at 03:18
  • I have the same issue, how do you fix this problem? Thank you. – m4gg Feb 18 '22 at 00:57
  • @m4gg check my response to the related issue Brian linked. My solution basically ended up being run C++ on WSL instead of windows – Harris B Feb 26 '22 at 22:48

0 Answers0