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 6935153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:03:54+00:00 2026-05-27T12:03:54+00:00

I have the following code in a project I am doing for class. I

  • 0

I have the following code in a project I am doing for class. I have been having trouble with the print statement for hours, and I could not find what I needed on the internet.

Here is my templated class definition:

template <class T>
class oset {
    template <class U>
    class node { 
    .....
    };
    .....
public:
   template <class U>
    class iter {
        node<U> *pos;          // node _before_ the one with this->operator*
        // constructor is private:
        iter(node<U>* n) : pos(n) { }
    friend class oset<U>; 
    ....
    };
 private:
    iter<T> start;         // initialized in the constructors below
    iter<T> finish;        // initialized in the constructors below

 public:
    iter<T> begin() {
        return start;
    }
    iter<T> end() {
        return finish;
    }
....
};

Then the problem I’m having is in print:

template <class S>
void print(oset<S>& OS) {
    for (oset<S>::iter<S> i = OS.begin(); i != OS.end(); ++i) { 
        // ^-- error is on previous line
        cout << *i << " ";
   }
   cout << endl;
}

When I try to compile with g++ I get the following error message:

oset.cc:276: error: expected primary-expression before ‘>’ token

oset.cc:276: error: ‘i’ was not declared in this scope

The problem is in the commented line. The ‘>’ token giving problems is the one directly before the first i. For some reason it does not like the <S>. If i get rid of the <S> it tells me it expects a ‘;’ before the i

I honestly have no idea what is causing the problem. I am getting very frustrated and any help would be highly appreciated. 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-27T12:03:55+00:00Added an answer on May 27, 2026 at 12:03 pm
    for (oset<S>::iter<S> i = OS.begin(); i != OS.end(); ++i) { //error this line
    

    You’ve to use typename and template here:

    for (typename oset<S>::template iter<S> i = OS.begin(); i != OS.end(); ++i) 
    

    Note that none of the following would work:

    oset<S>::iter<S> i              //error
    typename oset<S>::iter<S> i     //error
    oset<S>::template iter<S> i     //error
    

    Your situation is such that you’ve to use both keywords : typename as well as template:

    typename oset<S>::template iter<S> i  //ok
    

    Why you need both keywords here, is explained by @Johannes in this topic:

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

    Few advices and improvements

    • Make the iterator class a non-template. Use T wherever you’re using U in its definition.
    • Also rename the class from iter to iterator. Make it look like standard container/iterator, so that you could use it in agorithms defined in <algorithm>.

    That is, your class should look like this:

    class iterator {
        node<T> *pos;
        iterator(node<T>* n) : pos(n) { }
        friend class oset<T>; 
    };
    

    Then in for loop, you would only need typename as:

    typename oset<S>::iterator i //ok
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in my ASP.NET project public sealed class IoC {
I have the following code. <s:push value=#session['person']> <s:if test=%{admin=='y'}> <a class=add href=/projit1/project/addProject.jsp>Create a Project</a>
In my Rails project I have the following code: auto_link( h( wrap_long_string(post.text,50) )).gsub(/\n/,<br />)
I have the following code: <%= select_tag :role, options_for_select(Project::COMPANY_ROLES.concat(['Other...']), @relationship.role) %> For some reason
I have the following models class Project < ActiveRecord::Base has_many :project_members has_many :members, :through
I have the following code in a tester class in my main assembly, PocoGenerator
I have the following code. I am doing this in the post build event,
i have the following code: using System; using System.IO; namespace Project { public partial
I have the following code in my project to change mouse cursor when the
I have the following code in one of our projects webpages: XmlDocument xDoc =

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.