undefined reference to std::ios_base::Init::Init()

Je suis en train de travailler sur l'apprentissage de la programmation orientée objet avec C++ et avoir un problème. Je suis sûr que c'est un problème d'allocation de mémoire, mais ne peut pas sembler obtenir ma tête autour de lui. Toute aide sera grandement appréciée.

Mon Code Client

    #include <iostream>
    #include "Box.cpp"

    using namespace std;

    int main(){
        Box *box = new Box;
        return 0;
    }

Ma Classe De Boîte De...

    #include <iostream>
using namespace std;
class Box{
private:
double width;
double height;
double perimeter;
double area;
public:
Box(){
cout << "Box created" << endl;
}
~Box(){
cout << "Box Destroyed" << endl;
}
double getWidth(){
//
return this->width;
}
double getHeight(){
//
return this->height;
}
double getArea(){
//
return this->area;
}
double getPerimeter(){
//
return this->perimeter;
}
void setWidth(double w){
//
this->width = w;
if(!this->height){
computeSetArea(this->width, this->height);
computeSetPerimeter(this->width, this->height);
}
}
void setHeight(double h){
//
this->height = h;
if(!this->width){
computeSetArea(this->width, this->height);
computeSetPerimeter(this->width, this->height);
}
}
private:
void computeSetArea(double w, double h){
//
this->area = w*h;
}
void computeSetPerimeter(double w, double h){
//
this->perimeter = (w * 2) + (h + 2);
}
};

J'utilise gcc et de l'exécuter:

    gcc Box.cpp client.cpp -o mainfile

Après je reçois cette erreur.

/tmp/ccaVb21k.o: In function `__static_initialization_and_destruction_0(int, int)':
Box.cpp:(.text+0x1d): undefined reference to `std::ios_base::Init::Init()'
Box.cpp:(.text+0x22): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccaVb21k.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
/tmp/ccjtbzi4.o: In function `main':
client.cpp:(.text+0x14): undefined reference to `operator new(unsigned int)'
client.cpp:(.text+0x3f): undefined reference to `operator delete(void*)'
/tmp/ccjtbzi4.o: In function `__static_initialization_and_destruction_0(int, int)':
client.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::Init()'
client.cpp:(.text+0x71): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccjtbzi4.o: In function `Box::Box()':
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x11): undefined reference to `std::cout'
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x16): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x26): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccjtbzi4.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
/tmp/ccjtbzi4.o:(.eh_frame+0x4b): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
veuillez prendre un coup d'oeil ici: stackoverflow.com/questions/1696300/...
Par la convention .h fichiers contiennent la définition de la classe, tandis que l' .fichiers cpp contient l'implémentation de la classe, de sorte que vous devez soit: diviser votre définition à un .h de fichier, et de garder la mise en œuvre du rpc ou de renommer vos .le rpc .h (car il est vraiment un .h la mise en œuvre inline). Si vous renommer le fichier .h, veuillez retirer le using namespace std; de la .h fichier, que c'est une mauvaise pratique de polluer l'espace de noms global à l'intérieur d'un fichier d'en-tête.

OriginalL'auteur seanr | 2013-11-15