Solved

error C2146: syntax error : missing ';' before identifier 'value_type'

Posted on 2013-12-22
10
1,768 Views
Last Modified: 2014-01-30
I'm trying to work on the C++ iterator example at https://secweb.cs.odu.edu/~zeil/cs361/web/website/Lectures/iterators/pages/cppiterator.html
#pragma once
class iterator
{
public:
  typedef std::forward_iterator_tag iterator_category;
  typedef T                          value_type;
  typedef ptrdiff_t                  difference_type;
  typedef T*                         pointer;
  typedef T&                         reference;

  iterator();

  // Get the data element at this position
  reference operator*() const;
  pointer operator->() const;

  // Move position forward 1 place
  iterator& operator++();
  iterator operator++(int);

  // Comparison operators
  bool operator== (const iterator&) const;
  bool operator!= (const iterator&) const;
private:
  
};

Open in new window

I get the compile error complaining about line 6:
error C2146: syntax error : missing ';' before identifier 'value_type'
Is there an #include I need to add to make this work?
0
Comment
Question by:deleyd
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39735195
>>Is there an #include I need to add to make this work?

Yes, you are most likely to need to add

#include <iterator>

Open in new window


You could as well try to only use 'xutility' (which introduces it), but that way it's IMHO safer.
0
 

Author Comment

by:deleyd
ID: 39735226
added #include <iterator> it still complains same complaint.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39735234
Sorry for not noting that right away, but that source snippet is pseudocode and is just meant to illustrate the concept. Just look at the lines 15 and 19, neither 'reference' nor 'pointer' are valid keywords in C++. You will find the actual implementation in <iterator>.
0
 

Author Comment

by:deleyd
ID: 39735250
OK I started a new C++ window forms project, added a blank class Author, and a class Book.
#pragma once
#include <string>
#include "Author.h"
#include <iterator>

class Book
{
  struct AuthorListNode {
     Author data;
     AuthorListNode* next;
  };
public:
  
  Book (std::string theTitle,
        Author theAuthor, 
        std::string theIdentifier);

  Book (std::string theTitle, int numberOfAuthors,
        Author* theAuthors, std::string theIdentifier);

  Book (const Book& b);

  ~Book();
  
  Book& operator= (const Book&);

  typedef AuthorIterator iterator;
  typedef ConstAuthorIterator const_iterator;

  std::string getTitle() const        {return title;}
  void putTitle(std::string theTitle) {title = theTitle;}

  int getNumberOfAuthors() const {return numAuthors;}

  void addAuthor (Author);
  void removeAuthor (Author);

  std::string getIdentifier() const   {return identifier;}
  void putIdentifier(std::string id)  {identifier = id;}


  iterator begin();
  const_iterator begin() const;

  iterator end();
  const_iterator end() const;
  
  iterator findAuthor (Name n);
  const_iterator findAuthor (Name n) const;

private:
  std::string title;
  int numAuthors;

  AuthorListNode* authors;  // linked list of pointers to authors
  std::string identifier;
  friend class AuthorIterator;
  friend class ConstAuthorIterator;
};

Open in new window

It now complains about line 27:
Book.h(27): error C2146: syntax error : missing ';' before identifier 'iterator'
Also complains about lines 28, 42, 43, 45, 46, 48, 49.

(Also complains lines 48,49 about Name. Is there supposed to be a class named Name? I would think that would just be a string? Does it really need its own class? Or is there some sort of TypeDef thing I'm missing?)
0
 
LVL 86

Expert Comment

by:jkr
ID: 39736309
You will either have to add

using namespace std;

Open in new window


(not recommended in a header file, though) or refer to these identifiers as 'std::iterator' etc.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:deleyd
ID: 39736773
I changed line 27 to read
 typedef AuthorIterator std::iterator;

Open in new window

and it complains:
error C2955: 'std::iterator' : use of class template requires template argument list
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 39736944
here your typedef seems wrong.
It should be:    
typedef std::iterator AuthorIterator ;
0
 

Author Comment

by:deleyd
ID: 39738288
Ok I switched it around to read:
typedef std::iterator AuthorIterator;

Open in new window

and now it complains
error C2955: 'std::iterator' : use of class template requires template argument list
0
 
LVL 8

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 500 total points
ID: 39741086
Generally typedef is used to define a type.For a longer syntax we prefer doing typedef.
Ex: if I want unsigned long int to be used as ULINT then my declaration should be as follows.

typedef unsigned long int ULINT;

For iterator, we need to define the type of container upon which it'll work.
Lets we have a vector of ints. I want to traverse using iterator, So I can do declaration of iterator as follows.

typedef std::vector<int>::iterator vitrInt.

But in your case what exactly u want, I don't know.

If possible, share the entire source code.
0
 

Author Comment

by:deleyd
ID: 39742910
Well I was trying to go through what looks like a lecture posted by a university professor, but I can't get his code to compile. Thus I'm not sure what he's doing.

What would I use if I wanted an iterator for a collection of MyClass classes?
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now