pacomartin
pacomartin
  • Threads: 649
  • Posts: 7895
Joined: Jan 14, 2010
March 18th, 2011 at 11:13:10 PM permalink
I recently downloaded Microsoft Visual C++ 2010 Express and I am trying to use vectors for the first time.

I have a code that contains the following two lines:

#include <vector>
typedef vector::iterator vii;


However I am getting the following error messages from Microsoft Visual C++ 2010 Express
error C2653: 'vector' : is not a class or namespace name
error C2146: syntax error : missing ';' before identifier 'vii'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

==============
It looks like something is not installed correctly. Can you help?
MathExtremist
MathExtremist
  • Threads: 88
  • Posts: 6526
Joined: Aug 31, 2010
March 18th, 2011 at 11:25:46 PM permalink
You didn't say what you were defining a vector of. Vector isn't a class, it's a container. Look at an STL reference for how to declare vectors of classes or primitives and how to use iterators over them.
"In my own case, when it seemed to me after a long illness that death was close at hand, I found no little solace in playing constantly at dice." -- Girolamo Cardano, 1563
pacomartin
pacomartin
  • Threads: 649
  • Posts: 7895
Joined: Jan 14, 2010
March 22nd, 2011 at 6:36:53 PM permalink
Sorry, I am still lost. Maybe it will help if I show the whole code.
This code came from Combinations in C++ .
I am declaring it a vector of integers in the main subroutine.

The code will not compile in Microsoft Visual C++ Express 2010. It doesn't recognize this line of code

typedef vector::iterator vii;
error C2653: 'vector' : is not a class or namespace name
error C2146: syntax error : missing ';' before identifier 'vii'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Here is the code from the website.

#include <iostream>
#include <vector>
#include <string>
#include "combination.h"

typedef vector::iterator vii;

void display(vii begin,vii end)
{
for (vii it=begin;it!=end;++it)
cout<<*it;
cout<<endl;
}

int main()
{
vector<int> ca;
ca.push_back (1);
ca.push_back (2);
ca.push_back (3);
ca.push_back (4);
ca.push_back (5);
ca.push_back (6);
vector<int> cb;
cb.push_back (1);
cb.push_back (2);
cb.push_back (3);
cb.push_back (4);

recursive_combination(ca.begin (),ca.end(),0,
cb.begin(),cb.end(),0,6-4,display);
cout<<"Complete!"<<endl;
return 0;
}


File combination.h

// Recursive template function
template <class RanIt, class Func>
void recursive_combination(RanIt nbegin, RanIt nend, int n_column,
RanIt rbegin, RanIt rend, int r_column,int loop, Func func)
{
int r_size=rend-rbegin;

int localloop=loop;
int local_n_column=n_column;

//A different combination is out
if(r_column>(r_size-1))
{
func(rbegin,rend);
return;
}
//===========================

for(int i=0;i<=loop;++i)
{
RanIt it1=rbegin;
for(int cnt=0;cnt<r_column;++cnt)
{
++it1;
}
RanIt it2=nbegin;
for(int cnt2=0;cnt2<n_column+i;++cnt2)
{
++it2;
}

*it1=*it2;

++local_n_column;

recursive_combination(nbegin,nend,local_n_column,
rbegin,rend,r_column+1,localloop,func);

--localloop;
}
}
weaselman
weaselman
  • Threads: 20
  • Posts: 2349
Joined: Jul 11, 2010
March 22nd, 2011 at 7:24:32 PM permalink
try "vector<int>::iterator" instead of "vector::iterator"
"When two people always agree one of them is unnecessary"
pacomartin
pacomartin
  • Threads: 649
  • Posts: 7895
Joined: Jan 14, 2010
March 22nd, 2011 at 7:34:29 PM permalink
Quote: weaselman

try "vector<int>::iterator" instead of "vector::iterator"



typedef vector <int> ::iterator vii;

I get:
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2039: 'iterator' : is not a member of '`global namespace''


When I look at the relevant section of vector.h it isn't very helpful to me

// TEMPLATE CLASS vector
template<class _Ty,
class _Ax = allocator<_Ty> >
class vector
: public _Vector_val<_Ty, _Ax>
{ // varying size array of values
public:
typedef vector<_Ty, _Ax> _Myt;
typedef _Vector_val<_Ty, _Ax> _Mybase;
typedef typename _Mybase::_Alty _Alloc;

typedef _Alloc allocator_type;
typedef typename _Alloc::size_type size_type;
typedef typename _Alloc::difference_type difference_type;
typedef typename _Alloc::pointer pointer;
typedef typename _Alloc::const_pointer const_pointer;
typedef typename _Alloc::reference reference;
typedef typename _Alloc::const_reference const_reference;
typedef typename _Alloc::value_type value_type;
weaselman
weaselman
  • Threads: 20
  • Posts: 2349
Joined: Jul 11, 2010
March 22nd, 2011 at 7:52:48 PM permalink
ah, one more thing. Put "using namespace std;" before typedef.
"When two people always agree one of them is unnecessary"
pacomartin
pacomartin
  • Threads: 649
  • Posts: 7895
Joined: Jan 14, 2010
March 23rd, 2011 at 1:07:30 AM permalink
Thank you, that did it. The example on the website probably used a C++ compiler that assumed certain things (like default int for vectors).
pacomartin
pacomartin
  • Threads: 649
  • Posts: 7895
Joined: Jan 14, 2010
March 28th, 2011 at 8:59:32 PM permalink
I'm still having vector problems. I have a function which is called "display" but now it is writing an ASCII output of vectors.
I want to write it to a binary file.

This used to be easy with arrays, but I cannot figure out how to do it with vectors.


typedef vector<unsigned int>::iterator vii;
FILE *fileout,*fileoutb;

void display(vii begin,vii end)
{
for (vii it=begin;it!=end;++it)
{
fprintf(fileout,"%4u",*it);
}
fprintf(fileout,"\n");
}


My inclination is to add the line of code:

fwrite(&it,sizeof(unsigned int),1,fileout);

but I get the same number over and over. I think I am printing out a locater, and not the value inside the vector.
MathExtremist
MathExtremist
  • Threads: 88
  • Posts: 6526
Joined: Aug 31, 2010
March 28th, 2011 at 9:29:37 PM permalink
Try using binary output streams. Or a different language -- sorry, but C++ isn't that good with binary object serialization. C# or Java are better. That's less important if you're just serializing the content of the vector (the integers, which is what your loop is doing), but if you wanted to serialize the vector object itself it'd be harder.
"In my own case, when it seemed to me after a long illness that death was close at hand, I found no little solace in playing constantly at dice." -- Girolamo Cardano, 1563
  • Jump to: