class ship

im stuck and my program won't responed keep saying
" In function `int main(int, char**)': "
" no matching function for call to `Ship::Ship(const char[8], int)' "
" candidates are: Ship::Ship() "

" Ship::Ship(const Ship&) "

here's the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <string>
#include <iostream>

using namespace std;

class Ship
{
  
             char ShipName[20];
             int fuel;
     public:
           
              void Status();
              
};

     

void Ship::Status()
{
     cout<< "Ship Name:   "<< ShipName<< "Fuel Level: " <<fuel <<endl;
  
}

int main(int argc, char *argv[])
{
  Ship vessel1;
        vessel1.Status();
        
        Ship vessel2("Voyager", 10);
        vessel2.Status();
        Ship vessel3("Millennium Falcon", -10);
        vessel3.Status();
        
        
        system("PAUSE");
    return 0;
}
The problem is that you haven't written a constructor for your Ship class.

As a result, the candidates (Ship::Ship() and Ship::Ship(const Ship&)) have been automatically generated for you by the compiler.

However, they don't do what you tried in main, which is to construct a Ship object using a const char* and an int.

You need to write a constructor to do that.
The problem is exactly what the compiler error says it is: you're trying to create a Ship object using a constructor with two arguments, e.g. at line 30, but you haven't defined a constructor that takes those arguments. In fact, you haven't defined any constructors at all.
i did made one but i took it out

1
2
3
4
5
6
7
 void Ship::records();
{
cout << "Enter Ship Name: ";
cin >> ShipName;
cout <<"Enter Fuel level: ";
cin>>fuel;
}


i doubt thats correct
It's not really a good idea to put complicated stuff that could fail into a constructor. Really, a constructor should just do simple initialisation of the state of the class.

Oh, and just as with any other function definition, you don't put a semicolon at the end of the first line, like you did there.

In any case, that's a constructor that doesn't take any arguments - what we call a default constructor. However, what you're attempting to call at lines 30 and 32 is a constructor that takes arguments. The compiler can't magically write it for you - you'll have to define it.
yah i knw i kinda did that out of habit with the semicolon. thats what im trying to do the default constructor . like the ship defualt is enterprise adn the fuel is 0. im trying to figure out but nothing, hoping it might work.

i really need your help pls.
Well, for the default constructor, you don't need to have those cout and cin lines. Just set ShipName and fuel to appropriate defaults.

Once you've got your default constructor working, then write one that takes the arguments you're trying to pass in on lines 30 and 32, and set the data members to the values of those arguments.
You can't name your constructor "records" like you did above. A constructor must be named the same as the class, and it doesn't have a return value.
Topic archived. No new replies allowed.