0

I have written some code where I want to set class BaseClass as interface for DerivedClass and MainClass, my code look like this

base_class.h

class BaseClass {
public:
    virtual int first_virtual_function() = 0;
    virtual int second_virtual_function() = 0;
    virtual ~BaseClass() {}
};

derived_class.h

#include "base_class.h"

class DerivedClass: public BaseClass {
private:
    int first_number = 1;
    int second_number = 2;
    void change_first_number();
    void change_second_number();

public:
    int first_virtual_function();
    int second_virtual_function();
};

derived_class.cpp

#include "derived_class.h"

void DerivedClass::change_first_number() {
    first_number = 55;
}

void DerivedClass::change_second_number() {
    second_number = 11;
}

int DerivedClass::first_virtual_function() {
    change_first_number();
    return first_number;
}

int DerivedClass::second_virtual_function() {
    change_second_number();
    return second_number;
}

main_class.h

#include "base_class.h"

class MainClass {
private:
    int main_first_number = 0;
    int main_second_number = 0;

public:
    void show_changed_first_number(BaseClass& bc);
    void show_changed_second_number(BaseClass& bc);
};

main_class.cpp

#include "main_class.h"

void MainClass::show_changed_first_number(BaseClass& bc) {
    main_first_number = bc.first_virtual_function();
    std::cout << main_first_number << std::endl;
}

void MainClass::show_changed_second_number(BaseClass& bc){
    main_second_number = bc.second_virtual_function();
    std::cout << main_second_number << std::endl;
}

and main.cpp

#include <iostream>
#include "derived_class.h"
#include "main_class.h"


int main() {
    std::cout << "Hello, World!" << std::endl;

    MainClass mc;
    DerivedClass dc;

    mc.show_changed_first_number(dc);
    mc.show_changed_second_number(dc);

    return 0;
}

When I want to compiled this I get error:

[100%] Linking CXX executable vtable_error CMakeFiles/vtable_error.dir/main.cpp.o: In function main: /home/lukasz/Programowanie/c-repo/C++ academy/Sandbox/vtable_error/main.cpp:12: undefined reference to MainClass::show_changed_first_number(BaseClass&) /home/lukasz/Programowanie/c-repo/C++ academy/Sandbox/vtable_error/main.cpp:13: undefined reference to MainClass::show_changed_second_number(BaseClass&) collect2: error: ld returned 1 exit status CMakeFiles/vtable_error.dir/build.make:120: polecenia dla obiektu vtable_error nie powiodły się

But when I put whole code into one main.cpp file everything works fine, I don't know why.

Rama
  • 3,222
  • 2
  • 11
  • 26
starcu
  • 1
  • 1
  • 2
    Are you certain that main_class.cpp is being compiled and linked with main.cpp? Recommend adding the compiler command line to the question. – user4581301 Feb 01 '17 at 19:39
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – user4581301 Feb 01 '17 at 20:13
  • Yes, after i added main_class.cpp to makelist everything compiled fine. Thanks for hint! – starcu Feb 02 '17 at 15:56

0 Answers0