-1

Here is the error I am getting.

CruiseShip.h:10: error: expected ‘;’ before ‘::’ token

CruiseShip.cpp:8: error: expected ‘)’ before ‘name’ make: ***

[CruiseShip.o] Error 1

CruiseShip.h

CruiseShip(std::string name,std::string year, int maxPassengers)::Ship(std::string name,std::string year);

CruiseShip.cpp

CruiseShip(string name, string year, int maxPassengers)::Ship(string name, string year){
    maxPassengers=0;
}

These are the line's where the error occurs.

Here is the rest of the code: CruiseShip.cpp

#include <iostream>
#include "Ship.h"
#include "CruiseShip.h"
using namespace std;



CruiseShip(string name, string year, int maxPassengers)::Ship(string name, string year){
    maxPassengers=0;
}

void CruiseShip::setPass(int maxPassengers){
    this->maxPassengers=maxPassengers;
}
int CruiseShip::getPass(){
    return maxPassengers;
}

void CruiseShip::print(){
    cout<<"The name of the ship is "<<getName()<<endl;
    cout<<"The capacity of the ship is "<<maxPassengers<<endl;

}

CruiseShip.h

#ifndef CRUISESHIP_H_
#define CRUISESHIP_H_
#include <string>
class CruiseShip: public Ship{
protected:
    int maxPassengers;
    

public:
    CruiseShip(std::string name,std::string year, int maxPassengers)::Ship(std::string name,std::string year);
    void setPass(int);
    int getPass();
    virtual void print();

};
#endif
Community
  • 1
  • 1
willh231
  • 37
  • 1
  • 4

3 Answers3

3

Apparently, CruiseShip inherits from Ship.

The declaration should say only what the constructor's prototype is,

CruiseShip(std::string name, std::string year, int maxPassengers);

and the definition does the initialisation:

CruiseShip::CruiseShip(string name, string year, int maxPassengers) 
   : Ship(name, year),
     maxPassengers(maxPassengers) 
{

}

Note that there's only a single colon and that the base class initialization doesn't mention the types, just like a function call.
Also, the constructor definition needs the scope specification CruiseShip::.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Was typing this. You're too fast – Hearner May 03 '16 at 16:56
  • You have conflicting variables for `maxPassengers` and you throw away the parameter `maxPassengers` without ever using it. – abelenky May 03 '16 at 16:59
  • Dumb question, the declaration is in the header file right? Because if I change that the compiler won't find a matching function since they differ. @molbdnilo – willh231 May 03 '16 at 17:00
  • 1
    @abelenky The conflicting names don't matter for correctness, just for style. – Baum mit Augen May 03 '16 at 17:02
  • The conflicting names don't matter ***to the computer***. They will confuse the heck out of many ***humans***. Code is written for humans to understand. – abelenky May 03 '16 at 17:03
2

This line doesn't seem to make any sense.
What do you think it is supposed to do?

CruiseShip(std::string name,std::string year, int maxPassengers)::Ship(std::string name,std::string year);

It looks like the start of a constructor for class CruiseShip, but then has a scoping (::) before starting to look like the constructor for class Ship.

Here is what I think you mean:

In Header(.h) file:

#pragma once
#include <string>
using std::string;
class CruiseShip :
    public Ship   // Class inherits from base-class Ship
{
    // Constructor takes 3 parameters:
    CruiseShip(const string& name, const string& year, int maxPassengers);
};

In Implementation(.cpp) File:

// Implementation of the Constructor, which begins by passing
// name and year to the Base-Class constructor.
// Then completes the constructor by handling the maxPassengers parameter.
CruiseShip::CruiseShip(const string& name, const string& year, int maxPassengers): 
    Ship(name, year)  // Call the base-class constructor
{
    this->maxPassengers = maxPassengers; // Also assign member variable.
}


A few other notes:
  • You should generally pass variables by const-reference if you don't have a good reason for passing by value. This will avoid needless copy-constructors.

  • Avoid the whole #ifdef - #endif protection by using #pragma once, which is supported by most major compilers now.

  • Don't do using namespace std;. It brings in the entire namespace, which is really big. Just import what you need: using std::string; (see This Topic)

Community
  • 1
  • 1
abelenky
  • 63,815
  • 23
  • 109
  • 159
0

Your Ship class has to have something like this :

Ship(std::string,std::string);

in public declarations. Because this is what you are calling when you give parameters in CruiseShip

The way you make a correct constructor with inherence is this way :

CruiseShip::CruiseShip(string name, string year, int maxPassengers):Ship(name,year){
    maxPassengers=0;
}

You are calling the constructor with parameters Ship(std::string,std::string) that takes parameters given by CruiseShip. And you simply tell the program which variables you are giving

Your CruiseShip class because it's wrong. You don't tell the program to call Ship first

#ifndef CRUISESHIP_H_
#define CRUISESHIP_H_
#include <string>
class CruiseShip: public Ship{
protected:
    int maxPassengers;


public:
    CruiseShip(std::string name,std::string year, int maxPassengers);
    void setPass(int);
    int getPass();
    virtual void print();

};
#endif
Hearner
  • 2,711
  • 3
  • 17
  • 34