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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:20:49+00:00 2026-06-05T17:20:49+00:00

When trying to compile this code: #include <iostream> #include <vector> using namespace std; class

  • 0

When trying to compile this code:

#include <iostream>
#include <vector>
using namespace std;

class Test {
    int a;

public:
    Test(int pa) : a(pa) { }

    void print() {
        std::cout << a << std::endl;
    }
};

int main() {

    Test t(31415);

    t.print();

    vector<Test &> vet;

    vet.push_back(&t);

    return 0;
}

gcc 4.4.5-8 report various erros, starting with:

In file included from /usr/include/c++/4.4/i486-linux-gnu/bits/c++allocator.h:34,
                 from /usr/include/c++/4.4/bits/allocator.h:48,
                 from /usr/include/c++/4.4/string:43,
                 from /usr/include/c++/4.4/bits/locale_classes.h:42,
                 from /usr/include/c++/4.4/bits/ios_base.h:43,
                 from /usr/include/c++/4.4/ios:43,
                 from /usr/include/c++/4.4/ostream:40,
                 from /usr/include/c++/4.4/iostream:40,
                 from references.cpp:1:
/usr/include/c++/4.4/ext/new_allocator.h: In instantiation of ‘__gnu_cxx::new_allocator<Test&>’:
/usr/include/c++/4.4/bits/allocator.h:87:   instantiated from ‘std::allocator<Test&>’
/usr/include/c++/4.4/bits/stl_vector.h:71:   instantiated from ‘std::_Vector_base<Test&, std::allocator<Test&> >’
/usr/include/c++/4.4/bits/stl_vector.h:171:   instantiated from ‘std::vector<Test&, std::allocator<Test&> >’
references.cpp:22:   instantiated from here
...

where is the error?

  • 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-05T17:20:50+00:00Added an answer on June 5, 2026 at 5:20 pm

    The problem is that you are trying to create a vector of references. The object’s type to be stored in the vector must be assignable, which is not the case for references. A reference can only be initialized upon declaration and can not be changed later.

    What you most probably want is

    Test t(31415);
    std::vector<Test> vet;
    vet.push_back(t);
    

    which creates a copy of t that is then stored in the vector.

    You can actually see the problem in the compiler error messages, although they are quite cryptic. The compiler fails to generate code for a *allocator<Test&>, which takes care of memory allocation of the objects to be stored in the vector – there is no way to allocate memory for a reference.

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

Sidebar

Related Questions

So I am trying to compile this simple code: // In Test.h #include <iostream>
#include <iostream> #include <vector> int main() { class Int { public: Int(int _i) :
#include <iostream> class B; class A{ int a; public: friend void B::frndA(); }; class
#include <iostream> #include <string> using namespace std; void printstr( const string & s )
When trying to compile the following code #include <thread> #include <iostream> void foo() {
I am trying to read from stdin using C++, using this code #include <iostream>
I am trying to implement postfix-expression evaluation, here is my code: #include<iostream> #include<string.h> using
I've this following code in C++: //file0.cpp #include file11.cpp #include file22.cpp #include<iostream> #include<conio.h> using
I am trying to compile some C++ code that uses threads: #include <iostream> #include
I trying to compile this code: Int64 itag = BitConverter.ToInt64(temp, 0); itag &= 0xFFFFFFFFFFFFFC00;

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.