mardi 15 août 2017

Errors with the class [duplicate]

Hello I have a really simple code in C++ but it does not work...

This is the code :

main.cpp

#include <iostream>
#include "class1.hpp"

int main(){
Class1 class1;
return EXIT_SUCCESS;
}

class1.cpp

#include <iostream>
#include "class1.hpp"

Class1 :: Class1(){
    class2("hello");
}

class1.hpp

#ifndef DEF_CLASS1
#define DEF_CLASS1

#include "class2.hpp"

class Class1{

public:
Class1();
Class2 class2(std::string a);
};

#endif

class2.cpp

#include <iostream>
#include "class2.hpp"


Class2 :: Class2(std::string a){

std::cout << "Test OK1" << a << std::endl;
}

And the last class2.hpp

#ifndef DEF_CLASS2
#define DEF_CLASS2

class Class2{

public:
Class2(std::string a);

};

#endif

And the problem is when I try to compile this code I have this error :

/tmp/ccLyNA4m.o : In the function « Class1::Class1() » :
class1.cpp:(.text+0x4c) : undefined reference to « Class1::class2(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) »
collect2: error: ld returned 1 exit status

And I don't understand why it does not work ! I spent a lot of times on this problem. That's why if you could help me you will save my day. Really !

Thank you a lot for your help !

Aucun commentaire:

Enregistrer un commentaire