undefined reference to 'Test_1 :: Test_1

1

Generally they say that the .cpp is not included in main and yes the .h, however in my case whenever I will include the .h gives error and I do not know why.

follow the code:

main.cpp

#include <iostream>
#include "Teste_1.h"

int main(){

    Teste_1 *test=new Teste_1("you never win! but you can get my money", "Hello");

    delete test;
    return 0;
}

Test_1.h

#ifndef TESTE_1_H_INCLUDED
#define TESTE_1_H_INCLUDED

class Teste_1{
    private:
        std::string var_str1;
        std::string var_str2;
    public:
        Teste_1(std::string var1, std::string var2);
        ~Teste_1();
};

#endif // TESTE_1_H_INCLUDED

Test_1.cpp

#include "Teste_1.h"

Teste_1::Teste_1(std::string var1, std::string var2){
    var_str1=var1;
    var_str2=var2;
}

Teste_1::~Teste_1(){

}

Yes! Yes! the code does not have the slightest meaning, idai? It's just an example of an error ...

    
asked by anonymous 07.10.2017 / 21:55

2 answers

1

Tip # 1:

Whenever a .h or .cpp has external references, the header of that external reference must be included. This increases decoupling between modules and improves the readability of the code.

References:

  • SoftwareEngineering : link
  • Tip # 2:

    Always use the reserved word void in methods / functions that do not receive parameters, for example, replacing T::foobar() with T::foobar(void) is an excellent practice because:

  • Improves readability: T::foobar() looks like a call while T::foobar(void) looks like an implementation and / or definition);

  • Improves the compiler's interpretation of the code, avoiding problem called most vexing parse ;

  • Ensures interface compatibility with C , since C a functions declared as foobar() do not have the same meaning which functions declared as foobar(void) .

  • References:

  • Wikipedia link

  • StackOverflow: link

    Tip # 3:

    Always using virtual destructors in class definitions is another good practice, this allows for polymorphic manipulations, allows cascatear to call destructors from the derived class to base classes, avoiding leaks from memory.

    References:

  • StackOverflow: link
  • Tip # 4:

    Class builders allow you to initialize your members in a startup list, this is meant to be used! Avoid initializing your class members within the constructor implementation.

    References:

  • StackOverflow: link

  • StackOverflow: link

    How would your code look like:

    main.cpp

    #include "Teste_1.h"
    
    int main(void){
        Teste_1 * test = new Teste_1( "you never win! but you can get my money", "Hello" );
        delete test;
        return 0;
    }
    

    Test_1.h:

    #ifndef TESTE_1_H_INCLUDED
    #define TESTE_1_H_INCLUDED
    
    #include <string>
    
    class Teste_1{
        private:
            std::string var_str1;
            std::string var_str2;
        public:
            Teste_1( std::string var1, std::string var2 );
            virtual ~Teste_1(void);
    };
    
    #endif // TESTE_1_H_INCLUDED
    

    Test_1.cpp

    #include "Teste_1.h"
    #include <string>
    
    Teste_1::Teste_1( std::string var1, std::string var2 ) :
        var_str1(var1),
        var_str2(var2)
    {
    }
    
    Teste_1::~Teste_1(void){
    }
    

    Compiling:

    $ g++ main.cpp Teste_1.cpp -o teste
    
        
    08.10.2017 / 01:39
    0

    Before setting the Teste_1 class in the Test_1.h header file, include the string library:

    #ifndef TESTE_1_H_INCLUDED
    #define TESTE_1_H_INCLUDED
    
    #include <string>
    
    class Teste_1 {
        ...
    

    After that, compile the source files:

    # g++ main.cpp Teste_1.cpp
    

    This command will generate a a.exe executable file.

        
    07.10.2017 / 23:23