Jump to content

Can someone tell me what is wrong with this vector?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
phantom3380

phantom3380

    Newbie

  • Members
  • PipPip
  • 15 posts
Its the string i've highlighted in red thats the problem...my compiler gives me the error message: "13 C:\Dev-Cpp\My Projects\vector.cpp
no matching function for call to `vector<int,allocator<int> >::at (int)'"



#include <iostream>
#include <stdlib.h>
#include <vector>

int main(int argc, char *argv[])

{
vector <int> vec(3,100);
cout << "Vector size is " << vec.size() << endl;
vec.push_back(7); vec.push_back(8); vec.push_back(9);
cout << "\t3 elements added" << endl;
cout << "Vector size is now " << vec.size() << endl;
cout << "\tFirst element is "<< vec.front() << endl;
cout << "\tSecond element is" << vec.at(1) << endl;
cout << "\tLast element is " << vec.back() << endl;


system("PAUSE");
return 0;
}


What gives? :confused: :confused: :confused: :confused:
Yea as it was writ: "Compile and Run" :p

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
You've to use the namespace, std.
You can use it by using, using namespace ___ or by using std and the scope operators.
#include <iostream>
#include <vector>

int main()
{
	std::vector<int> vec(3, 100);
	
	std::cout << "Vector size is " << vec.size() << std::endl;
	
	vec.push_back(7); 
	vec.push_back(8); 
	vec.push_back(9);
	
	std::cout << "\t3 elements added"    << std::endl;
	std::cout << "Vector size is now   " << vec.size()  << std::endl;
	std::cout << "\tFirst element is   " << vec.front() << std::endl;
	std::cout << "\tSecond element is  " << vec.at(1)   << std::endl;
	std::cout << "\tLast element is    " << vec.back()  << std::endl;
	
	
	system("PAUSE");
	return 0;
}
In that example, I'm using the scope operators, but you can also just use
using namespace std;


#3
phantom3380

phantom3380

    Newbie

  • Members
  • PipPip
  • 15 posts
muchas gracias amigo :)
Yea as it was writ: "Compile and Run" :p