0

I am facing a problem with shrink_to_fit() function of STL C++. The problem is which I use it, the compiler gives error "Method 'shrink_to_fit' could not be resolved" on Eclipse Luna (32 bit) with MinGW compiler but the same program works fine in Dev C++.

Image of the program: enter image description here

Error: enter image description here

Compiler do not recommend shrink_to_fit() after using dot(.): enter image description here

Original code:

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
   vector<int> v(128);

   cout << "Initial capacity = " << v.capacity() << endl;

   v.resize(25);
   cout << "Capacity after resize = " << v.capacity() << endl;

   v.shrink_to_fit();
   cout << "Capacity after shrink_to_fit = " << v.capacity() << endl;

   return 0;
}

Please let me know is this my fault or IDE's.

P.S. I am using C++14.

zdan
  • 28,667
  • 7
  • 60
  • 71
  • 1
    `shrink_to_fit` was introduced in C++11. Make sure your ide/compiler flags is set to allow at least C++11 – smac89 Oct 30 '19 at 22:28
  • I am using C++14 i.e. ISO C++1y (-std=c++1y) but still getting error – Ritik Saxena Oct 30 '19 at 23:04
  • I'm just saying I can't reproduce the error you are getting. https://onlinegdb.com/rJMIv9D9r. This seems to be a problem with your IDE – smac89 Oct 30 '19 at 23:06
  • See if this question helps you: https://stackoverflow.com/questions/9131763/how-to-enable-c11-c0x-support-in-eclipse-cdt or this one: https://stackoverflow.com/questions/33314814/how-to-enable-c11-in-eclipse-luna – smac89 Oct 30 '19 at 23:07
  • I know because I ran the same program in Dev C++ and it works fine. But I mainly prefer Eclipse. BTW thanks for help. – Ritik Saxena Oct 30 '19 at 23:09

2 Answers2

1

It works fine for me (with -std=c++11 flag, and MinGW distro from https://nuwen.net/mingw.html#install) on

Eclipse IDE for C/C++ Developers, Version: 2019-09 R (4.13.0) Build id: 20190917-1200 OS: Windows 10, v.10.0, x86_64 / win32 Java version: 13.0.1

as well as on Linux (with -std=c++11 flag and GCC 7.4.0 compiler). It could be a problem with your IDE, compiler (with right flag) or STL implementation. There can't be a 4th reason in my view.

nae9on
  • 249
  • 3
  • 9
0

Works fine for me. Try compile 'by hand' to find out if it is about the ide:

g++ foo.cpp -o foo
./foo