2016-05-03 125 views
-1

这是我收到的错误。C++编译器错误

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

这些线是在错误发生。

这里是代码的其余部分: 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 
+0

这应该是一个模板?这不是你如何使用模板。 –

+1

我不太明白这些行应该做什么。请发布[mcve]。 –

+0

发布您的代码。 –

回答

2

此行似乎并没有任何意义。
你认为它应该做什么?

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

它看起来像一个构造函数用于class CruiseShip开始,但随后开始看起来像的构造class Ship前一个范围(::)。

这是我想你的意思:

在头文件(.h):

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

实施(的.cpp)文件:

// 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. 
} 


数其他说明:

  • 如果您没有足够的理由按值传递,通常应该通过const引用传递变量。这将避免不必要的复制构造函数。

  • 通过使用#pragma once来避免整个#ifdef - #endif的保护,现在大多数主要编译器都支持该保护。

  • 不要做using namespace std;。它带来了整个名字空间,这真的很大。只需导入你需要的东西:using std::string;(见This Topic

3

显然,CruiseShip继承Ship

声明应该说构造函数的原型是唯一的东西,

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

和定义并初始化:

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

} 

请注意,只有有一个冒号和基类的初始化没有按不提类型,就像函数调用一样。
此外,构造函数定义需要范围规范CruiseShip::

+0

正在输入这个。你太快了 – Hearner

+0

你有'maxPassengers'的变量冲突,你抛弃了参数'maxPassengers'而不用它。 – abelenky

+0

愚蠢的问题,声明是在头文件中吗?因为如果我改变编译器将不会找到匹配的函数,因为它们不同。 @molbdnilo – willh231

0

你的船类必须有这样的事情:

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

在公开声明。因为这是当你在CruiseShip

你与内在正确的构造函数的方式给参数您呼叫的是这样的:

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

您调用带有参数Ship(std::string,std::string)这需要通过给定参数的构造函数CruiseShip。你只需告诉程序你正在给哪些变量

你的CruiseShip类,因为它是错误的。你不要告诉程序要先打电话

#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