私はこのプロジェクトの最後であり、私は理解できないコンパイルエラーが発生しています。それは私をナットにしている、私はそれがクラス間のリンクエラーの問題かもしれないことを読んでいた。しかし、私はそれがどこにあるのか、どのように修正するのかを理解することはできません。C++でのコンパイルエラー
> 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;
}
エラーテキストを生成するためにどのようなコマンドを実行していますか? – donjuedo
精神的なデバッガは言う:CruiseShip.cppとCruiseShip。cppがあなたのmakeファイルに2度入っています –
* "私はこのプロジェクトの最後です" *これは面白いです。あなたがコンパイルしているときに、リンクエラーの束を持っているとき、あなたは終わりの近くのどこにもありません。私はあなたが終わった1/2の方法よりも小さいと言いたい。 – abelenky