2

getting this error in my code error: cannot convert 'Aircrafts' to 'Aircrafts*'

All I am trying to do is add a class record into a vector.

it is a c++ code and using -ansi -Wall -pedantic

it is bit annoying me. Hope somebody can help. Here is my code:

error is given for this line temp_aircraft = Aircrafts("Scenic", "Piper Arrow", 3, 120, 0, 0);

#include <iostream>
#include <string>
#include <stdlib.h>
#include <vector>
#include <iomanip>

using namespace std;

class Aircrafts
{
   public:
      string category;
      string aircraft;

   public: 

      Aircrafts () { 
         category = "";
         aircraft = "";         
      }

      Aircrafts (string f_cat, string airc) {
         category = f_cat;
         aircraft = airc;
      }      
}

void main()
{

   vector <Aircrafts> aircraft_list;

   Aircrafts *temp_aircraft;

   temp_aircraft = new Aircrafts;
   temp_aircraft = Aircrafts("Abc", "Xyz");
   aircraft_list.push_back(*temp_aircraft);
}
Oscar
  • 233
  • 2
  • 6
  • 15

9 Answers9

4

To fix the error in levels of indirection, you can replace

temp_aircraft = new Aircrafts;
temp_aircraft = Aircrafts("Abc", "Xyz");

with

temp_aircraft = new Aircrafts("Abc", "Xyz");

That's the correct syntax for calling a constructor with parameters. However, this creates a memory leak.

You do not need new there at all - you can do everything on a single line without using a temp_aircraft pointer:

aircraft_list.push_back(Aircrafts("Abc", "Xyz"));
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • And even if you _did_ use a `temp_Aircraft`, you still wouldn't need to make it a pointer: `Aircrafts temp_Aircraft{"Abc", "Xyz"};` – MSalters Nov 21 '13 at 12:51
1

You're assigning an Aircrafts object into a pointer to Aircrafts. This doesn't work.

Looking at your code, you don't need any dynamic allocation (= new) at all. Just do this:

void main()
{

   vector <Aircrafts> aircraft_list;

   aircraft_list.push_back(Aircrafts("Abc", "Xyz"));
}
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
1

No need for pointers

Aircrafts temp_aircraft("Abc", "Xyz");
aircraft_list.push_back(temp_aircraft);

Why do beginners love pointers?

EDIT I rest my case, the OP picked the answer that continued to use pointers, despite every other answer telling them that pointers were unnecessary.

john
  • 85,011
  • 4
  • 57
  • 81
1

First you declare temp_aircraft as a pointer to Aircrafts, which you then initialize using new correctly. However, you then try to assign a non-pointer object to the pointer.

There is no need for pointers here, just create the object and push it into the container in one go:

aircraft_list.push_back(Aircrafts("Abc", "Xyz"));

This will work with your class even if you haven't defined an explicit copy-assignment operator (the compiler will create a simple for you). But you might want to learn about the rule of three anyway.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

This line of code,

temp_aircraft = Aircrafts("Abc", "Xyz");

should be

temp_aircraft = new Aircrafts("Abc", "Xyz");

And remember to include semicolon at the end of the class.

class Aircrafts
{
   .....
}; // <-- semicolon here
Deidrei
  • 2,125
  • 1
  • 14
  • 14
1

Instead of

temp_aircraft = new Aircrafts;
temp_aircraft = Aircrafts("Abc", "Xyz");

you should do:

temp_aircraft = new Aircrafts("Abc", "Xyz");

constructor Aircrafts() gives you Aircrafts object, meantime temp_aircraft is Aircrafts *


P.S. also you have memory leak problem. All what you have allocated with new you must free with delete when you don't need it anymore; You can do it like:

   Aircrafts *temp_aircraft;

   temp_aircraft = new Aircrafts;
   temp_aircraft = Aircrafts("Abc", "Xyz");
   aircraft_list.push_back(*temp_aircraft);
   delete temp_aircraft;

or may be you don't need a pointer at all. Same result you can achieve with one line:

   aircraft_list.push_back( Aircrafts("Abc", "Xyz") );
klm123
  • 12,105
  • 14
  • 57
  • 95
1

no need for a temporary object.

int main()
{
   vector <Aircrafts> aircraft_list;
   aircraft_list.emplace_back("Abc", "Xyz");
}
Walter
  • 44,150
  • 20
  • 113
  • 196
0

temp_aircraft is a pointer, so when you try to do

temp_aircraft = Aircrafts("Scenic", "Piper Arrow", 3, 120, 0, 0);

you are trying to use the assignment operator to assign a Aircrafts to temp_aircarft

You can just use this constructor when you say new:

   temp_aircraft = new Aircrafts("Abc", "Xyz");

(or whatever the line really is that gives the error)

Of course, when you then do

aircraft_list.push_back(*temp_aircraft);

you are going to forget to delete the pointer. As other answers say, you can (and should) do this without pointers.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

The problem is on this line: temp_aircraft = Aircrafts("Abc", "Xyz");

WHen you write Aircrafts("Abc", "Xyz"), you create an object of the class Aircrafts, and you try to affect it to a variable of type Aircrafts* (pointer to Aircrafts). If you want to create a pointer, use the operator new, like this :

temp_aircraft = new Aircrafts("Abc", "Xyz");

If you don't want a pointer, create your variable with the Aircraft type, like this:

Aircrafts temp_aircraft;

Here, you mix the two ways, that's why it doesn't work.

Kaidjin
  • 1,433
  • 1
  • 12
  • 18