Reputation: 243
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);
}
Upvotes: 2
Views: 4804
Reputation: 87959
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.
Upvotes: 1
Reputation: 12865
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") );
Upvotes: 1
Reputation: 45424
no need for a temporary object.
int main()
{
vector <Aircrafts> aircraft_list;
aircraft_list.emplace_back("Abc", "Xyz");
}
Upvotes: 1
Reputation: 1513
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.
Upvotes: 0
Reputation: 19242
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.
Upvotes: 0
Reputation: 2185
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
Upvotes: 1
Reputation: 726569
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"));
Upvotes: 4
Reputation: 409176
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.
Upvotes: 1
Reputation: 171127
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"));
}
Upvotes: 1