这是我得到的错误。
CruiseShip.h:10:错误:预期';'在'::'标记之前
CruiseShip.cpp:8:错误:预期')'在'name'之前制作:***
[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
答案 0 :(得分: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::
。
答案 1 :(得分: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-reference传递变量。这将避免不必要的复制构造函数。
使用#ifdef - #endif
来避免整个#pragma once
保护,现在大多数主要编译器都支持using namespace std;
。
不要using std::string;
。它带来了整个命名空间,这非常大。只需导入您需要的内容: outFile.open("Final output.txt");
// print the report title and column headings
outFile << " Meet Report" << endl;
outFile << "Gender Weight Class(LBS) Squat Bench Deadlift Wilks" << endl;
outFile << "---------------------------------------------------------------------" << endl;
// serves as the first input for sentinel loop and determining the correct formula for wilks calculation
cout << "Please enter your gender (male/female, or e to end): ";
cin >> gender;
cout << endl;
while (gender != "male" && gender != "female")
{
cout << "Incorrect input. Please try again." << endl;
cout << "Please enter your gender (male/female, or e to end): ";
cin >> gender;
cout << endl;
}
while (gender == "male")
{
cout << "Please use kilograms (KG) for all inputs." << endl;
cout << "Enter your bodyweight: ";
cin >> weight;
cout << "Enter your final bench press attempt: ";
cin >> bench;
cout << "Enter your final squat attempt: ";
cin >> squat;
cout << "Enter your final deadlift attempt: ";
cin >> deadlift;
total = bench + squat + deadlift;
wilksCalc = 500 / (A_MEN + B_MEN * (weight) + C_MEN * (weight * weight) + D_MEN * (weight * weight * weight) +
E_MEN * (weight * weight * weight * weight) + F_MEN * (weight * weight * weight * weight * weight));
wilks = wilksCalc * total;
cout << wilks;
cout << endl;
outFile << left << setw(21) << gender;
outFile << weight;
outFile << right << setw(11) << squat;
outFile << right << setw(11) << bench;
outFile << right << setw(11) << deadlift;
outFile << right << setw(11) << wilks;
if (gender == "female")
{
cout << "Please use kilograms (KG) for all inputs." << endl;
cout << "Enter your bodyweight: ";
cin >> weight;
cout << "Enter your final bench press attempt: ";
cin >> bench;
cout << "Enter your final squat attempt: ";
cin >> squat;
cout << "Enter your final deadlift attempt: ";
cin >> deadlift;
total = bench + squat + deadlift;
wilksCalc = 500 / (A_WOMEN + B_WOMEN * (weight) + C_WOMEN * (weight * weight) + D_WOMEN * (weight * weight * weight) +
E_WOMEN * (weight * weight * weight * weight) + F_WOMEN * (weight * weight * weight * weight * weight));
wilks = wilksCalc * total;
cout << wilks;
cout << endl;
outFile << left << setw(21) << gender;
outFile << weight;
outFile << right << setw(11) << squat;
outFile << right << setw(11) << bench;
outFile << right << setw(11) << deadlift;
outFile << right << setw(11) << wilks;
}
}
outFile.close();
return 0;
}
(请参阅This Topic)
答案 2 :(得分:0)
您的Ship类必须包含以下内容:
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课程因为它错了。您没有告诉程序先调用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