Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4091636
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:23:30+00:00 2026-05-20T19:23:30+00:00

I am working through Accelerated C++ at the moment, and I am stuck on

  • 0

I am working through Accelerated C++ at the moment, and I am stuck on exercise 11-6. The idea is to reimplement the standard library vector as a class called Vec.

I am having trouble with the iterator erase(iterator) member, because I don’t know the correct syntax when outside of the class definition, and everything I’ve tried results in a compiler error. The code I currently have is:

template <class T> T* Vec<T>::erase(T* pos){

    if(pos < avail)
        std::copy(pos + 1, avail, pos);

    --avail;
    alloc.destroy(avail);

    return pos;

}

This works perfectly. However, for maintainability purposes and compatibility with algorithms in the stl, I know I should be doing something like this:

template <class T> Vec<T>::iterator Vec<T>::erase(Vec<T>::iterator pos){

    // As before

}

I’ve already defined iterator in the class definition, as follows:

 typedef T* iterator;

Trying to compile with the second code snippet results in:

D:\Documents\Programming\Accelerated C++\Chapter 11>cl /EHsc Vec.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

Vec.cpp
Vec.cpp(23) : warning C4346: 'Vec<T>::iterator' : dependent name is not a type prefix with 'typename' to indicate a type
Vec.cpp(23) : error C2143: syntax error : missing ';' before 'Vec<T>::erase'
Vec.cpp(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Vec.cpp(23) : fatal error C1903: unable to recover from previous error(s); stopping compilation

Sadly, the warning doesn’t make much sense to me, and after reading the MSDN page for it I can’t exacly see how it would apply to my code.

The rest of the messages appear as if the compiler didn’t recognise the return type.

I’ve tried many different combinations and searching hasn’t been very helpful. I would appreciate any help. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:23:31+00:00Added an answer on May 20, 2026 at 7:23 pm
      template <class T> 
      typename Vec<T>::iterator Vec<T>::erase(typename Vec<T>::iterator pos){
     //^^^^^^^^ note this                      ^^^^^^^^ note this as well!
    
           //your code!
      }
    

    That is, typename is required at two places. It’s because iterator is a dependent name, so typename is required!

    Read this excellent explanation by a great guy called Johannes, on Stackoverflow itself:

    • Where and why do I have to put the "template" and "typename" keywords?

    After reading that you can also read this topic:

    • Use of typename keyword with typedef and new

    Links to other good articles:

    • Name binding and dependent names (C++ only)
    • Dependent Names (scroll down and read this section – better if you read the complete article)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working through the exercises in Accelerated C++ and I'm stuck on question 5-6.
Hi i'm working through the Hartl's Ruby on Rails Tutorial and i'm stuck with
I'm currently working my way through Accelerated C++ and just came across this in
While working through Real World Haskell, I tried to complete the palindrome exercise using
I am working through a Zend framework book and is stuck for days on
I'm an inexperienced JavaScript user working through the book CoffeeScript: Accelerated JavaScript Development ,
I am working through problems in Accelerated C++ We have a program which stores
I am working through the examples in Accelerated C++. One of the problems asks
I've been working through Practical Common Lisp and as an exercise decided to write
Im working through a weather app tutorial. I am having trouble getting the reverse

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.