C++ cannot convert 'type' to 'type*'

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);
}

Answers


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"));

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"));
}

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.


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

no need for a temporary object.

int main()
{
   vector <Aircrafts> aircraft_list;
   aircraft_list.emplace_back("Abc", "Xyz");
}

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") );

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.


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.


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.


Need Your Help

'Helper' functions in C++

c++ class namespaces

While refactoring some old code I have stripped out a number of public methods that should actually of been statics as they a) don't operate on any member data or call any other member functions an...

Synchronous console logging in Chrome

javascript google-chrome logging webkit

Is it posible to make logging to the console synchronous? I often run into situations where the code execution is faster than dumping the structures. That resolves in outputting already changed obj...