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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:08:50+00:00 2026-05-14T15:08:50+00:00

I have a vector t_vec that stores references to instances of class Too .

  • 0

I have a vector t_vec that stores references to instances of class Too. The code is shown below. In the main , I have a vector t_vec_2 which has the same memory address as B::t_vec.

But when I try to access t_vec_2[0].val1 it gives error val1 not declared.

Could you please point out what is wrong? Also, if you know of a better way to return a vector from a method, please let me know! Thanks in advance.

class Too {      
      public:             
             Too();
             ~Too(){};
          int val1;
};

Too::Too(){
           val1 = 10;
           };


class B {
      public:
             vector<Too*> t_vec;
             Too*  t1;
             vector<Too*>& get_tvec();
             B(){t1 = new Too();};
             ~B(){delete t1;};
};

vector<Too*>& B::get_tvec(){
    t_vec.push_back(t1);
    return t_vec;
}

int main(){
    B b;
    b = B();
    vector<Too*>& t_vec_2 = b.get_tvec();

    // Getting error    
    std::cout << "\n val1 = " << t_vec_2[0].val1;
    return 0;
}
  • 1 1 Answer
  • 1 View
  • 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-14T15:08:50+00:00Added an answer on May 14, 2026 at 3:08 pm

    You have 2 errors:
    The first was already said, you should write

    t_vec_2[0]->val1
    

    instead of

    t_vec_2[0].val1
    

    The second is the strange line
    b = B();

    I think you should delete it.
    The error occurs because right part B() is gonna be delete just after it is created.
    So you don’t get in the object b the 10 value as you want. Just delete this line and it’ll be ok!

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

Sidebar

Related Questions

Here is my issue. I have a std::vector<POINTFLOAT> Which stores verticies. The issue is
I'm wondering if this code will do any trouble: I have a vector that
Say you have a vector class that has a template length and type -
I have a std::vector<uint8_t> that contains strings at specific offsets. Here's a shortened dump:
I have a vector of beans that holds information I want to display in
I have a vector of pointers to a class. I need to call their
Here is a sample code that I have: void test() { Object1 *obj =
I have a function that processes a given vector, but may also create such
I have two functions that I have overloaded for my debug class: template<class IteratorT>
I have this vector. vector<CXFileEntity>* menu;// CXFileEntity is class When i try load an

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.