0

Hello I have two files that define classes. In one of them I am trying declare an array of classes of another class using a pointer, I have some troubles with syntax, how I declare this in the first file? thank you in advance

the first file :

 #ifndef YARD_H_INCLUDED
 #define YARD_H_INCLUDED
 #include <iostream>

 using namespace std;

 class yard
 {
 public:
     int yard_size;
     car *car_array[];
     int cars_in_yard;

     yard();

     void show() const
     {
       for (int i=0; i<=cars_in_yard;i++)
       {
        car_array[i]->show();
       }
     }

     ~yard()
     {
       yard_size=0;
       delete car_array[cars_in_yard];
       cars_in_yard= 0;
     }
 };
 #endif // YARD_H_INCLUDED'

the second one is:

 #ifndef CAR_H_INCLUDED
 #define CAR_H_INCLUDED
 #include <iostream>
 #include <stdlib.h>
 #include <string>

 using namespace std;

 class car
 {
 public:
     char car_name[80];
     double price;
     int eng;

     car();

     void show() const
     {
          cout<<"Car Make : "<< *car_name <<" , Price: "<< price<<" , Engine : "<< eng <<"     , cc";
     }
 };

 car::car() 
 {
    car_name[0]=0;
    price=0.0;
    eng=0;
 }
 #endif // CAR_H_INCLUDED'
6
  • 1
    Like this: std::vector<car> car_array Commented May 20, 2014 at 15:30
  • Unfortunately vectors not allowed.... :/ Commented May 20, 2014 at 15:30
  • it seems that #include "car.h" is missing in the first file. Commented May 20, 2014 at 16:14
  • 5
    All this "no standard library features allowed" makes me wonder why people aren't being taught good honest C instead of crippled C++ with associated bad habits. Commented May 20, 2014 at 17:01
  • where is yard::yard() defined? Commented May 20, 2014 at 17:07

3 Answers 3

1

car *car_array[]; is a syntax error. Array sizes must be known at compile time. To use runtime sizing, you need to use dynamic memory allocation.

Based on Yard's destructor; it seems like the cars in this array are supposed to "belong" to the Yard - i.e. the Yard is responsible for clearing them.

It would be simpler to have the Yard store an array of car, instead of storing an array of car *. But I'm assuming you've decided to store an array of car * instead.

Of course the simplest way to do that is std::vector<car*> car_array; (or arguably, a vector of unique_ptr). However, since you're supposed to reinvent the wheel, you need to do the following things:

  • car **car_array;
  • In the constructors, initialize this (perhaps to nullptr)
  • make yard_size and cars_in_yard private - your Yard needs to accurately be able to keep track of how much memory is in use
  • Whenever yard_size is updated, if it got bigger you need to new[] some new space for car *'sand copy over values from the old space, and delete[] the old space
  • Whenever cars_in_yard is increased you need to check if you need to increase yard_size
  • Whenever cars_in_yard is decreased you need to delete the cars that are no longer in the yard, and shuffle everything down so there are no gaps (unless of course you want to allow gaps, but then your insertion logic is more complicated)
  • Have a copy-constructor, a copy-assignment operator that either do the right thing or are disabled
  • Have a destructor that deletes the cars in the yard and also car_array.
Sign up to request clarification or add additional context in comments.

Comments

0

Try this evil technique:

#define MAX_NAME_LENGTH 64
struct car
{
  char * p_name;
  car() : p_name(NULL)
  { }

  car(char const * const p_new_name)
  {
    if (p_name)
    {
        delete [] p_name;
    }
    const unsigned int size = strlen(p_new_name) + 1; // Remember that terminating nul char
    p_name = new char(size);
    strcpy(p_name, p_new_name);  // Make a copy of the parameter string
  }
};

If you really wanted to use dynamic memory correctly, your class would teach you to use smart or unique pointers.

The technique is evil since it is not using a tested data structure that uses dynamic memory, such as std::string or std::vector.

Edit 1:
Yes, evil, because the destructor doesn't exist and the compiler generated destructor would not free up the dynamic memory. Evil because it could lead to mistakes in the code above.

Adding the constructor:

  ~car()
  {
    delete [] p_name; // Note position of []
  }

Comments

0

well thanks for everybody who answered.. had no time to reply The problem is solved :) after some modifications and pain: code: first file

#ifndef YARD_H_INCLUDED
#define YARD_H_INCLUDED
#include "car.h"
#include <iostream>
using namespace std;
class yard
{
private:
int yard_size;
car **car_array;
int cars_in_yard;
public:
yard(int t_yard_size)
{
    yard_size=t_yard_size;
    car_array= new car *[yard_size];
    cars_in_yard=0;
}
~yard()
{
    for(int i=0; i<cars_in_yard; i++)
    {
        delete car_array[i];
    }
}
void show() const
{
  for (int i=0; i<cars_in_yard; i++)
  {
    car_array[i]->show();
  }
}
int num_Of_cars()
{
    return cars_in_yard;
}
void add_car(car *);
void show_last();
bool place();
};


#endif // YARD_H_INCLUDED

The Cpp of yard.h

#include "yard.h"
#include "car.h"

void yard::add_car(car *t_car)
{
if (yard_size == cars_in_yard)
{
    cout<< "There is no place in a Yard"<<endl;
}
else
{
    car_array[cars_in_yard++] = t_car;
    cout<<"New car added successfully"<<endl;
}
}
void yard::show_last()
{
car_array[cars_in_yard-1]->show();
}
bool yard::place()
{
if (yard_size==cars_in_yard)
{
    cout<<"There is no place in a Yard"<<endl;
    return false;
}
else
{
    return true;
}
}

and finally the car.h

#ifndef CAR_H_INCLUDED
#define CAR_H_INCLUDED
#include <iostream>
using namespace std;
class car
{   private:
char car_name[80];
double price;
int eng;
public:
car()
{
    price=0.0;
    eng=0;
}
car(char t_car_name[],int t_price, int t_eng)
{
    strcat(car_name, t_car_name);
    price = t_price;
    eng = t_eng;
}
void show()
{
  std::cout<<"Car Make : "<< car_name <<" , Price: "<< price<<" $ , Engine : "<< eng <<" cc ."<<std::endl;
}
};


#endif // CAR_H_INCLUDED

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.