Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector is not a member of std with everything included

I'm getting a very strange error and something I've never experienced before, I'm trying to initialize a vector at declaration like so:

  vector <int> myVector (5,4,3,4); 

It gives me an error saying that it cannot find a call matching that function, however, if I plug in only 2 numbers it doesn't give me an error.

Upon further investigation, even this piece of code throws that vector is not a member of std or that myVector is not a type when I try to call

myVector.push_back(4);

Now here is the code that gives me the vector is not a member of std, not matching function found, of that nature...

#include <vector>

using std::vector;

const std::vector<int> newvector;
std::newvector.push_back(5);

int main()
{
}

Error given: newvector in namespace std does not name a type

like image 586
Scholar Avatar asked Jan 23 '26 12:01

Scholar


1 Answers

There are two issues with this code.

  1. using std::vector; brings a vector from namespace std but you have to use this as follows:

    std::vector<int> newvector;
    newvector.push_back(5);
    
  2. You can notice that const dissapeared. This is because you want to change vector at last, so it cannot be const, it would be compile error to call a push_back on it otherwise.

Finally, this should be:

#include <vector>
using std::vector;

int main() {
  std::vector<int> newvector;
  newvector.push_back(5);
  std::vector<int> newvector;
  return 0;
}
like image 89
4pie0 Avatar answered Jan 25 '26 02:01

4pie0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!