【发布时间】:2016-05-03 17:58:52
【问题描述】:
我在这个项目的最后,我得到这个我无法弄清楚的编译错误。这让我发疯,我读到这可能是类之间链接错误的问题。但我不知道它可能在哪里以及如何修复它。
> CruiseShip.o: In function `CruiseShip':
> /home/013/w/wn/wna130030/Assignment5_test/CruiseShip.cpp:8: multiple
> definition of `CruiseShip::CruiseShip(std::basic_string<char,
> std::char_traits<char>, std::allocator<char> >,
> std::basic_string<char, std::char_traits<char>, std::allocator<char>
> >, int)' CargoShip.o:/home/013/w/wn/wna130030/Assignment5_test/CruiseShip.cpp:8:
> first defined here CruiseShip.o: In function `CruiseShip':
> /home/013/w/wn/wna130030/Assignment5_test/CruiseShip.cpp:8: multiple
> definition of `CruiseShip::CruiseShip(std::basic_string<char,
> std::char_traits<char>, std::allocator<char> >,
> std::basic_string<char, std::char_traits<char>, std::allocator<char>
> >, int)' CargoShip.o:/home/013/w/wn/wna130030/Assignment5_test/CruiseShip.cpp:8:
> first defined here CruiseShip.o: In function
> `CruiseShip::setPass(int)':
> /home/013/w/wn/wna130030/Assignment5_test/CruiseShip.cpp:12: multiple
> definition of `CruiseShip::setPass(int)'
> CargoShip.o:/home/013/w/wn/wna130030/Assignment5_test/CruiseShip.cpp:12:
> first defined here CruiseShip.o: In function `CruiseShip::getPass()':
> /home/013/w/wn/wna130030/Assignment5_test/CruiseShip.cpp:15: multiple
> definition of `CruiseShip::getPass()'
> CargoShip.o:/home/013/w/wn/wna130030/Assignment5_test/CruiseShip.cpp:15:
> first defined here CruiseShip.o: In function `CruiseShip::print()':
> /home/013/w/wn/wna130030/Assignment5_test/CruiseShip.cpp:20: multiple
> definition of `CruiseShip::print()'
> CargoShip.o:/home/013/w/wn/wna130030/Assignment5_test/CruiseShip.cpp:20:
> first defined here main.o: In function `main':
> /home/013/w/wn/wna130030/Assignment5_test/main.cpp:18: undefined
> reference to `CargoShip::CargoShip(std::basic_string<char,
> std::char_traits<char>, std::allocator<char> >,
> std::basic_string<char, std::char_traits<char>, std::allocator<char>
> >, int)'
collect2: ld returned 1 exit status make: *** [app] Error 1
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);
void setPass(int);
int getPass();
virtual void print();
};
#endif
CruiseShip.cpp
#include <iostream>
#include "Ship.h"
#include "CruiseShip.h"
using namespace std;
CruiseShip::CruiseShip(std::string name,std::string year, int maxPassengers):Ship(name,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;
}
Ship.h
#ifndef SHIP_H_
#define SHIP_H_
#include <string>
class Ship{
protected:
std::string name;
std::string year;
public:
//Default Constructor
Ship(std::string name, std::string year);
void setName(std::string name);
void setYear(std::string year);
std::string getName();
std::string getYear();
virtual void print();
};
#endif
Ship.cpp
#include <iostream>
#include "Ship.h"
using namespace std;
Ship::Ship(string name, string year){
name="";
year = "";
}
void Ship::setName(string name){
this->name = name;
}
void Ship::setYear(string year){
this->year=year;
}
string Ship::getName(){
return name;
}
string Ship::getYear(){
return year;
}
void Ship::print(){
cout<<"The name of the ship is "<<name<<endl;
cout<<"The year the ship was built is "<<year<<endl;
}
CargoShip.h
#ifndef CARGOSHIP_H_
#define CARGOSHIP_H_
#include <string>
class CargoShip: public Ship{
protected:
int cargoCapacity;
public:
CargoShip(std::string name, std::string year,int cargoCapacity);
int getCapacity();
virtual void print();
};
#endif
CargoShip.cpp
#include <iostream>
#include "Ship.h"
#include "CruiseShip.h"
#include "CargoShip.h"
using namespace std;
CargoShip::CargoShip(int cargoCapacity):Ship(name,year){
this->cargoCapacity=cargoCapacity;
}
int getCapacity(){
return cargoCapacity;
}
void print(){
cout<<"The Name of the ship is "<<getName()<<endl;
cout<<"The ship's cargo capacity is "<<cargoCapacity<<endl;
}
main.cpp
#include <iostream>
#include <iomanip>
#include "Ship.h"
#include "CruiseShip.h"
#include "CargoShip.h"
using namespace std;
int main()
{
int i;
//An array of Ship pointers
Ship *ships[3]={
new Ship("Lolipop", "1960"),
new CruiseShip("Disney Magic","2010",2400),
new CargoShip("Black Pearl","2003",50000)
};
//Display output
for(i=0;i<3;i++){
ships[i]->print();
}
return 0;
}
【问题讨论】:
-
您正在执行什么命令来生成该错误文本?
-
心理调试器说:CruiseShip.cpp 和 CruiseShip.cpp 在你的 make 文件中有两次
-
“我在这个项目的结尾” 很有趣。当你在编译,并且有一堆链接错误时,你还没有走到尽头。我会说你不到 1/2 的方式来完成。
-
您在 cargoship.cpp 文件的 CargoShip 的构造函数中遗漏了几个参数,这就是为什么您得到最后一个错误 undefined reference to CargoShip::CargoShip..
-
另外:基类 Ship 中没有虚拟析构函数,并且您的 Ship 构造函数错误,它应该使用传递的参数进行初始化
标签: c++ inheritance polymorphism