what's the problem in this code? 
Author Message
 what's the problem in this code?

I can compile below code in borland c++ compiler 5.5...

But can't compile in visual studio .net...

plz, help me..

ps. this code came from chapter 16 in "STL Tutorial and Reference Guide
(2nd) "

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

template<typename ForwardIterator>
class counting_iterator
{
 private:
  ForwardIterator current;
  int plus_count;
  string name;
  int version;

 public:
  typedef counting_iterator<ForwardIterator> self;

  typedef typename iterator_traits<ForwardIterator>::value_type value_type;
  typedef typename iterator_traits<ForwardIterator>::reference reference;
  typedef typename iterator_traits<ForwardIterator>::pointer pointer;
  typedef typename iterator_traits<ForwardIterator>::difference_type
difference_type;
  typedef forward_iterator_tag iterator_category;

  counting_iterator(ForwardIterator first, const string& n)
  {
   current = first;
   plus_count = 0;
   name = n;
   version = 1;
  }

  counting_iterator(const self& other)
  {
   current = other.current;
   plus_count = other.plus_count;
   name = other.name;
   version = other.version + 1;

   cout << "Copying " << name << ", new version is " << version << endl;
  }

  reference operator*()
  {
   return *current;
  }

  bool operator==(const self& other) const
  {
   return current == other.current;
  }

  bool operator!=(const self& other) const
  {
   return current != other.current;
  }

  self& operator++()
  {
   ++current;
   ++plus_count;
   return *this;
  }

  self operator++(int)
  {
   self tmp = *this;
   ++(*this);
   return tmp;
  }

  void report(ostream& o) const
  {
   o << "Iterator " << name << ", version " << version
    << ", reporting " << plus_count << " ++ operations " << endl;
  }

Quote:
};

int main()
{
 cout << "Demostrating a counting iterator class." << endl;

 int x[] = {12, 4, 3, 7, 17, 9, 11, 6};

 counting_iterator< int* > i(&x[0], "Curly"), j(&x[0], "Moe"), end(&x[8],
"Larry");

 cout << "Traversing array x from i (Curly) to end (Larry)\n";
 while(i != end)
 {
  cout << *i << endl;
  ++i;
 }

 cout << "After the traversal : \n";
 i.report(cout);
 end.report(cout);

 cout << "Assigning j (Moe) to i (Curly). " << endl;
 i = j;

 cout << "Searching the array form i (Moe) to end (Larry) using find\n";
 counting_iterator<int*> k = find(i, end, 9);

 cout << "After the find : \n";
 k.report(cout);
 i.report(cout);
 end.report(cout);

 return 0;

Quote:
}



Thu, 30 Dec 2004 18:56:59 GMT  
 what's the problem in this code?
Don't you think it would have been helpful to tell us what the compiler
says about the code?  Just posting a 150 lines and asking "what's wrong
with this?" is asking a lot from the people in this newsgroup.


Fri, 31 Dec 2004 07:36:25 GMT  
 what's the problem in this code?
MSVC has some problems with its STL. It seems that even the "excellent"
VC.Net isn't perfect
The problem is that you don't have iterator_traits for int*, so you have to
write your own.
Put this in your code, after the includes and "using" declarations:

#ifdef _MSC_VER
template<>
struct std::iterator_traits<int*>
{
typedef std::_Ranit iterator_category;
typedef int value_type;
typedef int& reference;
typedef int* pointer;
typedef int difference_type;

Quote:
};

#endif

At least that fixed the problems in MSVC6.

Regards,
Boris



Fri, 31 Dec 2004 15:59:13 GMT  
 what's the problem in this code?


Fri, 19 Jun 1992 00:00:00 GMT  
 what's the problem in this code?
I'm so sorry that I posted too long code

and very thanks for your kind answer. ^^ ( <- this is korean netizen's
smile )

But, I can't compile it. The error message is

"error C2955: 'std::_Ranit' : use of class template requires template
argument list"

So, I changed the code to std::_Ranit<int*, int, int*, int&> or
random_access_iterator_category , and I can compile it...

However.... The Linking errors attacked me!!  T.T ( <-cry... )

libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol
__malloc_dbg referenced in function "void * __cdecl operator new(unsigned
int,struct std::_DebugHeapTag_t const &,char *,int)"

libcpmtd.lib(locale.obj) : error LNK2001: unresolved external symbol
__malloc_dbg
libcpmtd.lib(_tolower.obj) : error LNK2019: unresolved external symbol
__malloc_dbg referenced in function __Tolower
libcpmtd.lib(xwcsxfrm.obj) : error LNK2019: unresolved external symbol
__malloc_dbg referenced in function __Wcsxfrm
libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol
__free_dbg referenced in function "void __cdecl operator delete(void
*,struct std::_DebugHeapTag_t const &,char *,int)"

libcpmtd.lib(xwcsxfrm.obj) : error LNK2001: unresolved external symbol
__free_dbg
libcpmtd.lib(locale.obj) : error LNK2019: unresolved external symbol
__realloc_dbg referenced in function "private: void __thiscall
std::locale::_Locimp::_Addfac(class std::locale::facet *,unsigned int)"

libcpmtd.lib(xmbtowc.obj) : error LNK2019: unresolved external symbol
__CrtDbgReport referenced in function __Mbrtowc_lk

Help me, please...

ps. I'm an STL primer. And I think that STL is very useful and interesting..
But it is too difficult.. --a

-JY



Mon, 03 Jan 2005 12:02:45 GMT  
 what's the problem in this code?


Fri, 19 Jun 1992 00:00:00 GMT  
 what's the problem in this code?
I changed from debug mode to release mode. ^^


Wed, 05 Jan 2005 14:22:50 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. What's my problem in the code?

2. what's the problem in my codes?

3. DAO code samples - found some but having problems with Release'ing objects

4. 'Apply code changes' strange behaviour

5. view'n'modify built-in class code

6. C source code of 'black jack'

7. compiling code containning 'try-except'

8. compiling code containning 'try-except'

9. Questions on 'weird' code

10. Wanted: Program to scramble 'C' code

11. 'commenting out' code with #define

12. Status from 'C' code

 

 
Powered by phpBB® Forum Software