[英]Compiler error in C++
Here is the error I am getting. 这是我遇到的错误。
CruiseShip.h:10: error: expected ';'
CruiseShip.h:10:错误:预期为“;” before '::' token
在'::'标记之前
CruiseShip.cpp:8: error: expected ')' before 'name' make: ***
CruiseShip.cpp:8:错误:在“名称”之前输入预期的“)”:***
[CruiseShip.o] Error 1
[CruiseShip.o]错误1
CruiseShip.h CruiseShip.h
CruiseShip(std::string name,std::string year, int maxPassengers)::Ship(std::string name,std::string year);
CruiseShip.cpp 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 这是其余的代码: 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 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
Apparently, CruiseShip
inherits from Ship
. 显然,
CruiseShip
继承自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::
. 另外,构造函数定义需要范围规范
CruiseShip::
。
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
. 它看起来像一个构造函数的开始
class CruiseShip
,但后来有一个作用域( ::
开始看起来像的构造之前) class Ship
。
Here is what I think you mean: 我认为这是您的意思:
In Header(.h) file: 在Header(.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);
};
In Implementation(.cpp) File: 在Implementation(.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.
}
You should generally pass variables by const-reference if you don't have a good reason for passing by value. 如果您没有充分的理由按值传递,则通常应通过const引用传递变量。 This will avoid needless copy-constructors.
这将避免不必要的复制构造函数。
Avoid the whole #ifdef - #endif
protection by using #pragma once
, which is supported by most major compilers now. 通过
#pragma once
使用#pragma once
避免整个#ifdef - #endif
保护,现在大多数主流编译器都支持此保护。
Don't do using namespace std;
不要
using namespace std;
. 。 It brings in the entire namespace, which is really big.
它引入了整个名称空间,这确实很大。 Just import what you need:
using std::string;
只需导入您需要的内容即可:
using std::string;
(see This Topic ) (请参阅本主题 )
Your Ship class has to have something like this : 您的Ship类必须具有以下内容:
Ship(std::string,std::string);
in public declarations. 在公开声明中。 Because this is what you are calling when you give parameters in
CruiseShip
因为这就是在
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
. 您正在使用参数
CruiseShip
Ship(std::string,std::string)
调用构造函数,该参数采用CruiseShip
给定的参数。 And you simply tell the program which variables you are giving 然后您只需告诉程序要提供的变量
Your CruiseShip class because it's wrong. 您的CruiseShip类,因为它是错误的。 You don't tell the program to call Ship first
您不告诉程序先调用Ship
#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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.