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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:22:10+00:00 2026-06-10T18:22:10+00:00

I am having a class called Domain, which has a protected member std::vector called

  • 0

I am having a class called Domain, which has a protected member std::vector called
itsVec. The vector is accessible through getVec().

In main() it works fine when I directly address the elements via
getVec()[i]. However, when I try using an iterator, the pointer to the first
element seems to not getting initialized correctly. Oddly, using an iterator on the itsVec in the class constructor works like a charm.

The code

#include<iostream>
#include <vector>

class Domain
{
  public:
    /* ====================  LIFECYCLE     ======================================= */
    Domain  ( ) {};                                    /* constructor        */
    Domain  ( unsigned int , const unsigned int * ) ;  /* custom constructor */
    Domain  ( const Domain &other );                   /* copy constructor   */
    ~Domain ( ) {};                                    /* destructor         */

    /* ====================  ACCESSORS     ======================================= */
    unsigned int              getDim() const {return itsDim;}
    std::vector<unsigned int> getVec() const {return itsVec;}

  protected:
    /* ====================  DATA MEMBERS  ======================================= */
    unsigned int              itsDim;
    std::vector<unsigned int> itsVec;

}; /* -----  end of class Domain  ----- */


Domain::Domain( unsigned int dim, const unsigned int  *externVec)
{
  itsDim = dim;

  for ( size_t j = 0; j < dim ; j++ )
    itsVec.push_back(externVec[j]);

  std::vector<unsigned int>::const_iterator i_vec  = itsVec.begin();

// iterator access works
  for ( ; i_vec != itsVec.end(); i_vec++) 
    std::cout<<"Iterator in constructor: " << (*i_vec) << std::endl;

  std::cout<<std::endl;

} /* -----  end of constructor  ----- */


/*-----------------------------------------------------------------------------
 *  Main
 *-----------------------------------------------------------------------------*/
int main ()
{
  const unsigned int kVec[3] = { 1, 2, 3 };

  Domain k(3,kVec);

  size_t i = 0;

// direct access works
  for( ; i<3 ; i++)
    std::cout << "Vec direct: " << k.getVec()[i] << std::endl;

  std::cout << std::endl;

// iterator access FAILS for the first element
  std::vector<unsigned int>::const_iterator  it_vec  = k.getVec().begin();

  for ( ; it_vec != k.getVec().end(); it_vec++) 
    std::cout<<"Vec iterator " << (*it_vec) << std::endl;

  return 0;
}       /* ----------  end of function main  ---------- */

Compiled with g++ main.cc -o test results in

$ ./test

Iterator in constructor: 1
Iterator in constructor: 2
Iterator in constructor: 3

Vec direct: 1
Vec direct: 2
Vec direct: 3

Vec iterator 140124160
Vec iterator 2
Vec iterator 3

Testing on another machine gave:

Vec iterator 0
Vec iterator 0
Vec iterator 3

Is the way I’m returning the vector through getVec() wrong. Is the
iterator handled incorrectly? I have no clue, whats wrong here or how to fix it.

Any help is highly appreciated,

Da Frenk

  • 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-06-10T18:22:12+00:00Added an answer on June 10, 2026 at 6:22 pm

    You are returning a copy of the vector rather than a reference and so you are not matching the iterators correctly. The iterator you obtain from begin() is from a different copy of your internal vector to the one you obtain from end()

    Rather than returning a copy of the internal vector you can return a reference by changing the function prototype to:

    const std::vector<unsigned int>& getVec() const {return itsVec;}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a domain class called User which can follow other User
I am having one class called BaseClass which contains some logic applicable to whole
I created a class called Foo. Foo has three fields, which are all ints:
I am having a main class called ProccesA, after I run it I am
I have a domain class called Flight that represents a flight that has been
If I have a class called myclass does having static methods within the class
Im having rather an annoying problem. I have a class called Person and a
I am having a class called Customer. Customer *object; //in this object i have
I am having trouble with an assignment. I have a class called Grid and
I'm having some trouble creating an object in C++. I create a class called

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.