![]() |
C++ hilfe bitte
Code:
#include <iostream> 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$ |
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(); } |
Code:
#include <iostream> |
§2. Posten von Beiträgen sowie Erstellen von Themen
Zitat:
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.