Hello,
I'm trying to learn C++.
For the moment, I'm exploring classes and objects.
I want to split my program into 3 files: DBus.cpp - bus.h - bus.cpp
With Geany this doesn't work (mingw32/8.3). It only compiles, if I
integrate bus.cpp into bus.h.
I tried the same with Visual Studio (CMake) and it compiles the
splittend files without problems.
I want generally use Geany, because VS is too complex for learning, not
uncomplicated to handle.
What can I do?
Below the 3 Files: DBus.cpp - bus.h - bus.cpp and the error file.
---------DBus.cpp------------------
#include "bus.h"
#include <iostream>
int main() {
DBus dbus1(130, 25000, "rot");
DBus dbus2(180, 40000, "blau");
dbus1.ausgabeDBus();
dbus2.ausgabeDBus();
}
---------bus.h--------------------------
#pragma once
#include <string>
#include <iostream>
class DBus {
private:
int kw;
int preis;
std::string farbe;
public:
DBus(int KW, int Pr, std::string Far);
void ausgabeDBus();
int getPreis();
std::string getFarbe();
};
----------bus.cpp--------------------------
#include <iostream>
#include "bus.h"
DBus::DBus(int KW, int Pr, std::string Far) {
kw = KW;
preis = Pr;
farbe = Far;
}
void DBus::ausgabeDBus() {
std::cout << "der " << farbe << "e Campingbus hat " << kw << " KW
und kostete " << preis << " Euro" << std::endl;
}
int DBus::getPreis() {
return preis;
}
std::string DBus::getFarbe() {
return farbe;
}
-------------error----------------------
g++ -Wall -o "DBus" "DBus.cpp" (im Verzeichnis:
C:\Users\hz\Documents\C++\Class)
C:/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
C:\Users\hz\AppData\Local\Temp\ccrZlTnk.o:DBus.cpp:(.text+0x54):
undefined reference to `DBus::DBus(int, int,
std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >)'
C:/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
C:\Users\hz\AppData\Local\Temp\ccrZlTnk.o:DBus.cpp:(.text+0xaf):
undefined reference to `DBus::DBus(int, int,
std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >)'
C:/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
C:\Users\hz\AppData\Local\Temp\ccrZlTnk.o:DBus.cpp:(.text+0xd3):
undefined reference to `DBus::ausgabeDBus()'
C:/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
C:\Users\hz\AppData\Local\Temp\ccrZlTnk.o:DBus.cpp:(.text+0xdf):
undefined reference to `DBus::ausgabeDBus()'
collect2.exe: error: ld returned 1 exit status
Kompilierung fehlgeschlagen.