Jump to content
  • Advertisement
Sign in to follow this  
Kenny77

Vector Problem

This topic is 4278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just created this program using vectors, as a little refresher, and I can't seem to get it to work
#include <iostream>
#include <conio.h>
#include <vector>

using namespace std;

int main()
{
    vector <int> num( 25, 50, 75 );
    
    cout << num.at(0) << endl;
    cout << num.at(1) << endl;
    cout << num.at(2) << endl;
    
    getch();
    return 0;
}


I get the error: no matching function for call to 'std::vector<int::allocator<int> > vector(int,int,int)'

Share this post


Link to post
Share on other sites
Advertisement
You can't specify the contents of the vector in the constructor. One fairly common way to work around this is to construct from an array:
#include <iostream>
#include <vector>

using namespace std;

// a handy pair of templates for getting pointers to the
// first and one-past-the-end elements of a stack array
template < typename Type, std::size_t n >
Type const * begin(Type (&array)[n])
{
return array;
}

template < typename Type, std::size_t n >
Type const * end(Type (&array)[n])
{
return array + n;
}

int main()
{
int num_data[] = {25, 50, 75};

// use iterator range to construct vector
vector <int> num(begin(num_data), end(num_data));

cout << num.at(0) << endl;
cout << num.at(1) << endl;
cout << num.at(2) << endl;
}

Σnigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!