0

Main programm:

#include<iostream>
#include<string>
using namespace std;
#include "klasse_methoden.cpp"

int main(){

    postenTyp pEins;
    pEins.werteZuweisen(5,2.5,"Working?");
    pEins.ausgeben();

}

Class definition:

#include<string>
using namespace std;

class postenTyp{
    private:
        int anzahl;
        double kommaZahl;
        string name;
    public:
        void werteZuweisen(const int &, const double &, const string &);
        void ausgeben();
};

Class Methods:

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


void postenTyp::werteZuweisen(const int &a, const double &p, const string &b){
    anzahl = a;
    kommaZahl = p;
    name = b;
}

void postenTyp::ausgeben(){
    cout << "Anzahl: " << anzahl << "Kommazahl: " << kommaZahl << "Name: " << name << endl;

}

Compile error - multiple definition

The book teaches me to not include anything or using namespace in the class definition and class methods but then i get even more errors.

Ambitious
  • 55
  • 3
  • 7
    As a sidenote, I would suggest programming in english, because you can share your code more easily with other people. Sometimes (always?!) function names describe what they do and if you write german function names, somebody from france has no clue what it does mean. – hellow Aug 02 '18 at 12:36
  • 4
    `#include "klasse_methoden.cpp"` - nope. You include headers, not cpp files – UKMonkey Aug 02 '18 at 12:36
  • You shouldn't include `.cpp` files. – Dan M. Aug 02 '18 at 12:36
  • You are missing include guards. – hellow Aug 02 '18 at 12:36
  • 1
    highly related: https://stackoverflow.com/questions/9579930/separating-class-code-into-a-header-and-cpp-file – NathanOliver Aug 02 '18 at 12:37
  • Don't write "`using namespace std;`" in the global namespace, and especially not in a header. https://stackoverflow.com/questions/1452721 – aschepler Aug 02 '18 at 12:49
  • You should supply parameter *names* in your method declarations. This helps readers differentiate between the parameters and their purpose. For example, if you program requires X and Y, you don't want the reader to pass Y, X. – Thomas Matthews Aug 02 '18 at 13:54

1 Answers1

1

You should include header files instead of source files.

This is true for both the klasse_methoden.cpp source file where you should include klasse_methoden.h and not vice versa and in the main.cpp also include klasse_methoden.h.

Also to avoid including the content of a header twice you need to use the pragma specifier #pragma once at the beginning of the header or use defines like this

#ifndef _MY_HEADER_GUARD_
    #define _MY_HEADER_GUARD_

//header content

#endif
Petok Lorand
  • 893
  • 6
  • 13
  • Thank you for your answer. Well i just jumped through the book. Until the end not even once header files are mentiont with classes. Might have to look for another book. – Ambitious Aug 02 '18 at 12:48
  • @ambitious Get a [good book](https://stackoverflow.com/q/388242/9254539) and don't skip through it. Make sure you understand everything and do the exercises. – BessieTheCookie Aug 02 '18 at 16:56