[英]Compiler error in C++
這是我遇到的錯誤。
CruiseShip.h:10:錯誤:預期為“;” 在'::'標記之前
CruiseShip.cpp:8:錯誤:在“名稱”之前輸入預期的“)”:***
[CruiseShip.o]錯誤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
顯然, 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::
。
這條線似乎沒有任何意義。
您認為應該怎么辦?
CruiseShip(std::string name,std::string year, int maxPassengers)::Ship(std::string name,std::string year);
它看起來像一個構造函數的開始class CruiseShip
,但后來有一個作用域( ::
開始看起來像的構造之前) class Ship
。
我認為這是您的意思:
在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);
};
在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.
}
如果您沒有充分的理由按值傳遞,則通常應通過const引用傳遞變量。 這將避免不必要的復制構造函數。
通過#pragma once
使用#pragma once
避免整個#ifdef - #endif
保護,現在大多數主流編譯器都支持此保護。
不要using namespace std;
。 它引入了整個名稱空間,這確實很大。 只需導入您需要的內容即可: using std::string;
(請參閱本主題 )
您的Ship類必須具有以下內容:
Ship(std::string,std::string);
在公開聲明中。 因為這就是在CruiseShip
提供參數時要調用的
通過內在方式構造正確的構造函數的方式是這樣的:
CruiseShip::CruiseShip(string name, string year, int maxPassengers):Ship(name,year){
maxPassengers=0;
}
您正在使用參數CruiseShip
Ship(std::string,std::string)
調用構造函數,該參數采用CruiseShip
給定的參數。 然后您只需告訴程序要提供的變量
您的CruiseShip類,因為它是錯誤的。 您不告訴程序先調用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.