myGully.com

myGully.com (https://mygully.com/index.php)
-   Programmierung (https://mygully.com/forumdisplay.php?f=67)
-   -   C++ hilfe bitte (https://mygully.com/showthread.php?t=4211942)

serking 30.11.15 15:37

C++ hilfe bitte
 
Code:

#include <iostream>


using namespace std;



class Computer{
   
    private :
   
    char* op_name;
    char* hardwarename;
   
   
    public :
   
    Computer();
   
    Computer(const char* op_name,const char* hardwarename);
   
    Computer(const Computer& anothercomputer);
   
    Computer& operator=(const Computer& other);
   
    void print();
   
};



Computer::Computer(){
 
    op_name=NULL;
    hardwarename=NULL;
   
};

Computer::Computer(const char* op_name,const char* hardwarename){
   

    if(op_name!=NULL&&hardwarename!=NULL){
       
        delete(this->op_name);
        delete(this->hardwarename);
       
        this->op_name=strdup(op_name);
        this->hardwarename=strdup(hardwarename);
       
    }
};


Computer::Computer(const Computer& anothercomputer){
   
    if(op_name!=NULL&&hardwarename!=NULL){
       
        delete(this->op_name);
        delete(this->hardwarename);
       
        this->op_name=strdup(anothercomputer.op_name);
        this->hardwarename=strdup(anothercomputer.hardwarename);
    }
};


Computer& Computer::operator=(const Computer& other){
   
    if(op_name!=NULL&&hardwarename!=NULL){
       
        delete(this->op_name);
        delete(this->hardwarename);
       
        this->op_name=strdup(other.op_name);
        this->hardwarename=strdup(other.hardwarename);
       
    }
   
    return *this;
   
};


void Computer::print(){
   
    cout<<"op_name : "<<op_name<<" hardwarename : "<<hardwarename<<endl;
}


int main(){
   
   
    Computer serotik("Mac","Serotik");
   
    serotik.print();
    Computer enis=serotik;
   
    enis.print();
   
   
   
   
}

Fehlermeldung

a.out(1025,0x7fff79a39000) malloc: *** error for object 0x7fff508cdc00: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Serhads-MacBook-Pro:Desktop Sero$ g++ computer.cpp
Serhads-MacBook-Pro:Desktop Sero$ ./a.out
a.out(1030,0x7fff79a39000) malloc: *** error for object 0x7fff5b058c00: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Serhads-MacBook-Pro:Desktop Sero$

serking 30.11.15 15:57

lösung
 
#include <iostream>

#include <string>
using namespace std;



class Computer{

private :

char* op_name;
char* hardwarename;


public :

Computer();

Computer(const char* op_name,const char* hardwarename);

Computer(const Computer& anothercomputer);

Computer& operator=(const Computer& other);

void print();

};



Computer::Computer(){

op_name=NULL;
hardwarename=NULL;

};

Computer::Computer(const char* op_name,const char* hardwarename){

/*

if(op_name!=NULL&&hardwarename!=NULL){

delete(this->op_name);
delete(this->hardwarename);

this->op_name=strdup(op_name);
this->hardwarename=strdup(hardwarename);

}*/



int len1=strlen(op_name);
int len2=strlen(hardwarename);

this->op_name=new char[len1+1];
this->hardwarename=new char[len2+1];

strcpy(this->hardwarename,hardwarename);
strcpy(this->op_name,op_name);




};


Computer::Computer(const Computer& anothercomputer){


int len1=strlen(anothercomputer.op_name);
int len2=strlen(anothercomputer.hardwarename);

this->op_name=new char[len1+1];
this->hardwarename=new char[len2+1];

strcpy(this->hardwarename,anothercomputer.hardwarename);
strcpy(this->op_name,anothercomputer.op_name);
};


Computer& Computer::operator=(const Computer& other){

int len1=strlen(other.op_name);
int len2=strlen(other.hardwarename);

this->op_name=new char[len1+1];
this->hardwarename=new char[len2+1];

strcpy(this->hardwarename,other.hardwarename);
strcpy(this->op_name,other.op_name);


return *this;

};


void Computer::print(){

cout<<"op_name : "<<op_name<<" hardwarename : "<<hardwarename<<endl;
}


int main(){


Computer serotik("Mac","Serotik");

serotik.print();

Computer enis=serotik;

enis.print();



}

serking 30.11.15 15:58

Code:

#include <iostream>

#include <string>
using namespace std;



class Computer{
   
    private :
   
    char* op_name;
    char* hardwarename;
   
   
    public :
   
    Computer();
   
    Computer(const char* op_name,const char* hardwarename);
   
    Computer(const Computer& anothercomputer);
   
    Computer& operator=(const Computer& other);
   
    void print();
   
};



Computer::Computer(){
 
    op_name=NULL;
    hardwarename=NULL;
   
};

Computer::Computer(const char* op_name,const char* hardwarename){
   
    /*

    if(op_name!=NULL&&hardwarename!=NULL){
       
        delete(this->op_name);
        delete(this->hardwarename);
       
        this->op_name=strdup(op_name);
        this->hardwarename=strdup(hardwarename);
       
    }*/
   
   
   
    int len1=strlen(op_name);
    int len2=strlen(hardwarename);
   
    this->op_name=new char[len1+1];
    this->hardwarename=new char[len2+1];
   
    strcpy(this->hardwarename,hardwarename);
    strcpy(this->op_name,op_name);
   
   
   
   
};


Computer::Computer(const Computer& anothercomputer){
   
   
    int len1=strlen(anothercomputer.op_name);
    int len2=strlen(anothercomputer.hardwarename);
   
    this->op_name=new char[len1+1];
    this->hardwarename=new char[len2+1];
   
    strcpy(this->hardwarename,anothercomputer.hardwarename);
    strcpy(this->op_name,anothercomputer.op_name);
};


Computer& Computer::operator=(const Computer& other){
   
    int len1=strlen(other.op_name);
    int len2=strlen(other.hardwarename);
   
    this->op_name=new char[len1+1];
    this->hardwarename=new char[len2+1];
   
    strcpy(this->hardwarename,other.hardwarename);
    strcpy(this->op_name,other.op_name);
   
   
    return *this;
   
};


void Computer::print(){
   
    cout<<"op_name : "<<op_name<<" hardwarename : "<<hardwarename<<endl;
}


int main(){
   
   
    Computer serotik("Mac","Serotik");

    serotik.print();
   
    Computer enis=serotik;
   
    enis.print();
   
   
   
}


Osiris1983 01.12.15 17:47

§2. Posten von Beiträgen sowie Erstellen von Themen
Zitat:

3. Bitte versucht, Euren Themen eine Aussagekräftige Überschrift zu geben, damit anderen Usern sofort klar ist, um was es sich bei Eurem Thema handelt oder womit Ihr Probleme habt. Thementitel wie "Suche...", "Bitte helft mir!", "Ich habe ein Problem!", "Bitte unbedingt lesen!" oder "Was sagt ihr dazu?" sollten vermieden werden, und werden von den zuständigen Moderatoren gelöscht, sofern er nach Aufforderung nicht geändert wurde.
Bitte Überschrift konkretisieren. Klicke dazu im ersten beitrag des Themas auf "editieren", dann auf "erweitert".

Ausserdem, bitte packt euren Code in einen Spoiler!


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:54 Uhr.

Powered by vBulletin® (Deutsch)
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.