Kezdőoldal » Számítástechnika » Programozás » Mi lehet a hiba ebben a c++...

Mi lehet a hiba ebben a c++ forráskódban?

Figyelt kérdés

//main.cpp

# include <iostream> //std::cout,std::cin,std::endl

# include <string> //std::string

# include <stdlib.h> //EXIT_SUCCESS

# include <windows.h> //Sleep(1000),exit(0)


typedef unsigned short ushort; //Szinoníma készítése az unsigned short-ra.

typedef unsigned int uint; //Szinoníma készítése az unsigned int-re.


using std::cout; //std::cout névtér be emelése a jelenlegi névtérbe.

using std::cin; //std::cin névtér be emelése a jelenlegi névtérbe.

using std::endl; //std::endl névtér be emelése a jelenlegi névtérbe.

using std::string; //std::string névtér be emelése a jelenlegi névtérbe.



//class CAT

class CAT

{

public:

CAT(); //Konstruktor1

CAT(ushort age,ushort weight); //Konstruktor2

CAT(const CAT &rhs); //Copy konstruktor

~CAT(); //destruktor

const CAT operator=(const CAT &rhs); //operator=


const CAT& operator++(); //operator++ prefix

const CAT operator++(int); //operator++ postfix

const CAT operator+(const CAT &rhs); //operatot+

operator ushort()const; //operator ushort


void SetAge(ushort age); //SetAge

ushort GetAge()const; //GetAge

void SetWeight(ushort weight); //SetWeight

ushort GetWeight()const; //GetWeight

void Miau()const; //Miau

private:

ushort *itsAge;

ushort *itsWeight;

};

//CAT definíció

//konstruktor1,konstruktor2,copy konstruktor,destruktor,operator=

CAT::CAT():

itsAge(new ushort(0)),

itsWeight(new ushort(0))

{cout <<"Konstruktor1 \n";}


CAT::CAT(ushort age,ushort weight = 0):

itsAge(new ushort(age)),

itsWeight(new ushort(weight))

{cout <<"Konstruktor2 \n";}


CAT::CAT(const CAT &rhs)

{

cout <<"Copy konstruktor \n";

itsAge = new ushort;

itsWeight = new ushort;

*itsAge = rhs.GetAge();

*itsWeight = rhs.GetWeight();

}


CAT::~CAT()

{

cout <<"Destruktor \n";

delete itsAge;

delete itsWeight;

}


const CAT CAT::operator=(const CAT &rhs)

{

cout <<"operator= \n";

if(this==&rhs)

return *this;

delete itsAge;

delete itsWeight;

itsAge = new ushort;

itsWeight = new ushort;

*itsAge = rhs.GetAge();

*itsWeight = rhs.GetWeight();

return *this;

}

//operator++ prefix,operator++ postfix,operator+,operator ushort

const CAT& CAT::operator++()

{

cout <<"operator++ prefix \n";

++(*itsAge);

return *this;

}


const CAT CAT::operator++(int)

{

cout <<"operator++ postfix \n";

const CAT Temp(*this);

++(*itsAge);

return Temp;

}


const CAT CAT::operator+(const CAT &rhs)

{

cout <<"operator+ \n";

return CAT(*itsAge+rhs.GetAge());

}


CAT::operator ushort()const

{

cout <<"operator ushort \n";

return(ushort(*itsAge));

}

//SetAge,GetAge,SetWeight,GetWeight,Miau

void CAT::SetAge(ushort age)

{*itsAge = age;}


ushort CAT::GetAge()const

{return *itsAge;}


void CAT::SetWeight(ushort weight)

{*itsWeight = weight;}


ushort CAT::GetWeight()const

{return *itsWeight;}


void CAT::Miau()const

{cout <<"miau,miau \n";}



//main start

int main()

{

const CAT *const Bolyhos(4,6);

CAT *const Morcos;


CAT myArray[3];

CAT &Kokszos = myArray[0];

CAT &Mancsos = myArray[1];

CAT &Tappancs = myArray[2];



delete Bolyhos;

delete Morcos;



return EXIT_SUCCESS;

}


hibaüzi:

main.cc: In function 'int main()':

main.cc:129:32: error: expression list treated as compound expression in initializer [-fpermissive]

main.cc:129:32: error: invalid conversion from 'int' to 'const CAT*' [-fpermissive]

main.cc:130:15: error: uninitialized const 'Morcos' [-fpermissive]

make[2]: *** [build/Debug/MinGW-Windows/main.o] Error 1

make[1]: *** [.build-conf] Error 2

make: *** [.build-impl] Error 2



BUILD FAILED (exit value 2, total time: 3s)


2012. ápr. 24. 03:13
 1/3 iostream ***** válasza:

Hogyhogy mi a hiba? Egy MUTATÓNAK (Bolyhos) azt az értéket akarod adni, hogy 4,6. A 4,6 kifejezés értéke 6, 6-al pedig nem lehet inicializálni egy mutatót.

A másik hiba pedig hogy van egy const mutatód (Morcos), aminek nem változhat meg az értéke, és minden constot kötelező inicializálni, különben semmi értelmük, hiszen utólag nem tudsz értéket adni neki.

2012. ápr. 24. 09:16
Hasznos számodra ez a válasz?
 2/3 A kérdező kommentje:
Ezt valaki elmagyarázná érthetőbben azt a kifejezés értéke részt nem értem.
2012. ápr. 24. 11:44
 3/3 iostream ***** válasza:
Az operator, (operátor vessző) működése. (a, b) értéke a b kifejezés értéke.
2012. ápr. 24. 13:07
Hasznos számodra ez a válasz?

Kapcsolódó kérdések:





Minden jog fenntartva © 2024, www.gyakorikerdesek.hu
GYIK | Szabályzat | Jogi nyilatkozat | Adatvédelem | Cookie beállítások | WebMinute Kft. | Facebook | Kapcsolat: info(kukac)gyakorikerdesek.hu

A weboldalon megjelenő anyagok nem minősülnek szerkesztői tartalomnak, előzetes ellenőrzésen nem esnek át, az üzemeltető véleményét nem tükrözik.
Ha kifogással szeretne élni valamely tartalommal kapcsolatban, kérjük jelezze e-mailes elérhetőségünkön!