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

Need Your Help

How to use libraries in AIDE

android gradle aide-ide

I'm not very familiar with Gradle, but from what I understand you're supposed to be able to pull libraries straight from the internet? I've followed the project's directions, but got an error

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.