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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:45:45+00:00 2026-05-19T11:45:45+00:00

#include <iostream> class Car { private: Car(){}; int _no; public: Car(int no) { _no=no;

  • 0
#include <iostream>
class Car
{
private:
  Car(){};
  int _no;
public:
  Car(int no)
  {
    _no=no;
  }
  void printNo()
  {
    std::cout<<_no<<std::endl;
  }
};
void printCarNumbers(Car *cars, int length)
{
    for(int i = 0; i<length;i++)
         std::cout<<cars[i].printNo();
}

int main()
{
  int userInput = 10;
  Car *mycars = new Car[userInput];
  for(int i =0;i < userInput;i++)
         mycars[i]=new Car[i+1];
  printCarNumbers(mycars,userInput);
  return 0;
}    

I want to create a car array but I get the following error:

cartest.cpp: In function ‘int main()’:
cartest.cpp:5: error: ‘Car::Car()’ is private
cartest.cpp:21: error: within this context

is there a way to make this initialization without making Car() constructor public?

  • 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-19T11:45:46+00:00Added an answer on May 19, 2026 at 11:45 am

    Nope.

    But lo! If you use std::vector<Car>, like you should be (never ever use new[]), then you can specify exactly how elements should be constructed*.

    *Well sort of. You can specify the value of which to make copies of.


    Like this:

    #include <iostream>
    #include <vector>
    
    class Car
    {
    private:
        Car(); // if you don't use it, you can just declare it to make it private
        int _no;
    public:
        Car(int no) :
        _no(no)
        {
            // use an initialization list to initialize members,
            // not the constructor body to assign them
        }
    
        void printNo()
        {
            // use whitespace, itmakesthingseasiertoread
            std::cout << _no << std::endl;
        }
    };
    
    int main()
    {
        int userInput = 10;
    
        // first method: userInput copies of Car(5)
        std::vector<Car> mycars(userInput, Car(5)); 
    
        // second method:
        std::vector<Car> mycars; // empty
        mycars.reserve(userInput); // optional: reserve the memory upfront
    
        for (int i = 0; i < userInput; ++i)
            mycars.push_back(Car(i)); // ith element is a copy of this
    
        // return 0 is implicit on main's with no return statement,
        // useful for snippets and short code samples
    } 
    

    With the additional function:

    void printCarNumbers(Car *cars, int length)
    {
        for(int i = 0; i < length; i++) // whitespace! :)
             std::cout << cars[i].printNo();
    }
    
    int main()
    {
        // ...
    
        printCarNumbers(&mycars[0], mycars.size());
    } 
    

    Note printCarNumbers really should be designed differently, to accept two iterators denoting a range.

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

Sidebar

Related Questions

#include <iostream> class base { public: virtual void print (int a) { std::cout <<
#include <iostream> using namespace std; class A{ int b; public: A(){ cout<<Constructor for class
#include<iostream> using namespace std; class Abc { public: int a; Abc() { cout<<Def cstr
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
#include iostream using namespace std; class A { public: void mprint() { cout<<\n TESTING
#include <iostream> class A { public: void foo() const { std::cout << const version
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include <iostream> class A { public: A() { std::cout << A ctor << std::endl;
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include <iostream> using namespace std; class A { public: A() { cout << Default

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.