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

  • Home
  • SEARCH
  • 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 498273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:51:46+00:00 2026-05-13T05:51:46+00:00

I have a vector like so: vector<MyType*> _types; And I want to iterate over

  • 0

I have a vector like so:

vector<MyType*> _types;

And I want to iterate over the vector and call a function on each of MyTypes in the vector, but I’m getting invalid return errors from the compiler. It appears the pos iterator isn’t a pointer to MyType, it’s something else. What am I not understanding?

Edit: Some code..

    for (pos = _types.begin(); pos < _types.end(); pos++)
{
    InternalType* inst = *pos->GetInternalType();
}

The compiler errors are:

  • invalid return type ‘InternalType**’ for overloaded ‘operator ->’
  • ‘GetInternalType’ : is not a member of ‘std::_Vector_iterator<_Ty,_Alloc>’

Edit pt2

Should my vector contain pointers or objects? What are the pros and cons? If I am using new to create an instance, I am guessing I can only use a vector of pointers to MyType is that correct?

  • 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-13T05:51:46+00:00Added an answer on May 13, 2026 at 5:51 am

    If the vector contained objects, not pointers, you could do pos->foo(). The iterator “acts like” a pointer. But your vector contains pointers, so an iterator will act like a pointer to a pointer, so needs to be dereferenced twice.

    MyType *pMyType = *pos; // first dereference
    if (pMyType) {          // make sure the pointer is not null
      pMyType->foo();       // second dereference
    }
    

    If you are sure the pointer is not null, you could do this:

    (*pos)->foo();
    

    The parenthesis around *pos are needed so the dereference applies to pos, not to pos->foo(). Order of operations.

    If your vector needs to contain items from a class hierarchy (e.g., subclasses of MyType), then you have to make it a vector of pointers. Otherwise a vector of objects is probably simpler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector of pointers. I would like to call a function for
I have a vector of pointers like so: vector<Item*> items; I want to clear
I have a vector like this: std::vector<std::vector<char> > _lines; I would like to iterate
I have a vector of pair like such: vector<pair<string,double>> revenue; I want to add
I have a vector-like class that contains an array of objects of type T
Let's assume that we have a vector like x = -1:0.05:1; ids = randperm(length(x));
I have a vector in R that looks like this: dat <- c(TRUE, TRUE,
I have a vector of pointers to objects. I'd like to remove objects from
I have a vector, v3, that looks like this: v3 <- matrix(c(1,1,3,3,2,2, 2,3,1,2,3,1, 3,2,2,1,1,3),
I have a vector of Rect: vector<Rect> myRecVec; I would like to remove the

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.