main.cpp:46:1: error: ‘ma’ does not name a type wie beheben?

#include <iostream>

#include <string>

using namespace std;

class Person{

private:

string name;

int alter;

public:

void zeigePerson();

Person(string n,int a){

  name=n;

  alter=a;

}

};

void Person::zeigePerson(){

std::cout << name << std::endl;   

std::cout << alter << std::endl;

};

class Mitarbeiter : Person{

  private:

  int gehalt;

  public:

    void gehaltAendern(int g){

    gehalt=g;  

    }

    Mitarbeiter(string name,int alter,int gehalt):Person(name,alter){

      gehalt=gehalt;

    }

    void zeigePerson();

};

void Mitarbeiter::zeigePerson(){

      Person::zeigePerson();

      std::cout << gehalt << std::endl;

    } 

Person ma(“mario”,20);

ma.zeigePerson();

int main()

{

   

  return 0;

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
3 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Pandoratim
1 year ago

You have taken the code out of the main block and as an independent block according to the definition of the class

Mitarbeiter

written. The compiler cannot recognize that

ma

Type

Person

is because this code is outside a function and thus does not contain instructions.

In order to run the code, you need the instance of

ma

move to the main function and the method

zeigePerson()

call. The corrected code looks as follows:

c

Copy code
#include  #include  using namespace std; class Person { private: stringname; int alter; public: void zeigePerson(); Person(string n, int a) { name = n; alter = a; } }; void Person::zeigePerson() { std::cout << name << std::endl; std::cout << alter << std::endl; } class Mitarbeiter : Person { private: int gehalt; public: voidgehaltAendern(int g) { gehalt = g; } Mitarbeiter(string name, int alter, int gehalt) : Person(name, alter) { this->gehalt = gehalt; } void zeigePerson(); }; voidMitarbeiter::zeigePerson() { Person::zeigePerson(); std::cout << gehalt << std::endl; }int main() { Person ma("mario", 20); ma.zeigePerson(); return 0; }

Now the code should compile and run without errors.

Pandoratim
1 year ago
Reply to  Pandoratim

#include

#include

using namespace std;

class person

private:

string name;

int old;

public:

void showperson();

Person(string n, int a) {

name = n;

age = a;

}

};

void Person::Learn() {

std::cout < < < name < < < < < < std::endl;

std::cout < < < < < < < < < < < < < < < < < < < < < :: endl;

}

class Staff : Person {

private:

int content;

public:

void content Changes(int g) {

content = g;

}

Staff(string name, int old, int grade) : Person(name, old) {

this > content = content;

}

void showperson();

};

void Staff::Learn() {

Person:: showperson();

std::cout < < < < < < < < < < < < < < < stdd:: endl;

}

int main() {

Person ma(“mario”, 20);

the person(s);

return 0;

}