Kezdőoldal » Számítástechnika » Programozás » Mi a hiba a forráskódban? /C++

Mi a hiba a forráskódban? /C++

Figyelt kérdés

//main.cpp

# include <iostream>

# include <stdlib.h>


typedef unsigned short ushort;

typedef unsigned int uint;


using std::cout;

using std::cin;

using std::endl;



class Counter

{

public:

Counter(int x); //Konstruktor

Counter(const Counter &); //Másoló konstruktor

~Counter(); //Destruktor


void increment() {++*itsVal;}

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

const Counter operator++ (int) {} //postfix


void SetVal(int x);

int GetVal()const;

private:

int *itsVal;

};


Counter::Counter(int x):

itsVal(new int(x))

{cout <<"Konstruktor \n";}


Counter::Counter(const Counter &rhs)

{

cout <<"Copy konstruktor \n";

itsVal = new int;

*itsVal = rhs.GetVal();

}


Counter::~Counter()

{

cout <<"Destruktor \n";

delete itsVal;

itsVal = NULL;

}


const Counter& Counter::operator++ ()

{

++*itsVal;

return *this;

}

//

void Counter::SetVal(int x)

{*itsVal = x;}


int Counter::GetVal()const

{return *itsVal;}



//main kezdődik

int main()

{

Counter i(2);



return EXIT_SUCCESS;

}



C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|21|error: ISO C++ forbids declaration of 'Counter' with no type [-fpermissive]|

C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|21|error: expected ';' at end of member declaration|

C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|21|error: ISO C++ forbids declaration of 'operator++' with no type [-fpermissive]|

C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|22|error: 'Counter' does not name a type|

C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|21|error: field 'const int& Counter::Counter' with same name as class [-fpermissive]|

C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp||In constructor 'Counter::Counter(int)':|

C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|30|error: uninitialized reference member 'Counter::Counter' [-fpermissive]|

C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|34|error: 'Counter' does not name a type|

C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|34|error: ISO C++ forbids declaration of 'rhs' with no type [-fpermissive]|

C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|34|error: prototype for 'Counter::Counter(const int&)' does not match any in class 'Counter'|

C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|17|error: candidates are: Counter::Counter(const Counter&)|

C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|30|error: Counter::Counter(int)|

C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|48|error: prototype for 'const Counter& Counter::operator++()' does not match any in class 'Counter'|

C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|21|error: candidate is: int Counter::operator++()|

||=== Build finished: 13 errors, 0 warnings ===|


2012. márc. 17. 18:41
 1/10 anonim ***** válasza:
0%

szerintem az hogy a pc-d merevlemezére hivatkozik az egész.


"C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|"

2012. márc. 17. 18:43
Hasznos számodra ez a válasz?
 2/10 anonim ***** válasza:
0%
mégsem.. benéztem a kérdést
2012. márc. 17. 18:44
Hasznos számodra ez a válasz?
 3/10 anonim ***** válasza:

haverod is hasonlókat kérdezgetett: http://www.gyakorikerdesek.hu/szamitastechnika__programozas_..


- const &Counter operator++ (); //prefix

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


- const Counter operator++ (int) {} //postfix

+ const Counter operator++ (int) { return *this; } //postfix

2012. márc. 17. 18:52
Hasznos számodra ez a válasz?
 4/10 A kérdező kommentje:
Mi a hiba?
2012. márc. 17. 19:03
 5/10 anonim ***** válasza:
Te most szívatsz? :)
2012. márc. 17. 19:07
Hasznos számodra ez a válasz?
 6/10 A kérdező kommentje:

Nem.

De nem tudom mi a hiba.

Léci mondjátok meg.

2012. márc. 17. 20:20
 7/10 iostream ***** válasza:

A kolléga valamiért azt hiszi, hogy egy gyerek, aki elkezdett C++-t kódolni, tudni fogja olvasni a diffet.


Ez:

- const &Counter operator++ (); //prefix

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


azt jelenti, hogy az első ki, a második be. Azaz cseréld le azt a sort (az elsőt) a másikra.

2012. márc. 17. 20:25
Hasznos számodra ez a válasz?
 8/10 A kérdező kommentje:

Még mindig nem jó:


//main.cpp

# include <iostream>

# include <stdlib.h>


typedef unsigned short ushort;

typedef unsigned int uint;


using std::cout;

using std::cin;

using std::endl;



class Counter

{

public:

Counter(int x); //Konstruktor

Counter(const Counter &); //Másoló konstruktor

~Counter(); //Destruktor


void increment() {++*itsVal;}

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

const Counter operator++ (int) {} //postfix


void SetVal(int x);

int GetVal()const;

private:

int *itsVal;

};


Counter::Counter(int x):

itsVal(new int(x))

{cout <<"Konstruktor \n";}


Counter::Counter(const Counter &rhs)

{

cout <<"Copy konstruktor \n";

itsVal = new int;

*itsVal = rhs.GetVal();

}


Counter::~Counter()

{

cout <<"Destruktor \n";

delete itsVal;

itsVal = NULL;

}


const Counter& Counter::operator++ ()

{

++*itsVal;

return *this;

}

//

void Counter::SetVal(int x)

{*itsVal = x;}


int Counter::GetVal()const

{return *itsVal;}



//main kezdõdik

int main()

{

Counter i(1);

cout <<"i erteke: "<< i.GetVal << endl;

++i;

cout <<"i erteke: "<< i.GetVal << endl;



return EXIT_SUCCESS;

}


Olyan sok a hiba,hogy nem engedi ki íratni ide.

2012. márc. 17. 21:44
 9/10 anonim ***** válasza:

const Counter operator++ (int) {} //postfix

Ebből hiányzik a "return *this;", de ezt már másodjára mondom. Counter típusú objektummal kell visszatérnie, mert annak deklaráltad, tehát nem lehet üres a függvénytörzs.


cout <<"i erteke: "<< i.GetVal << endl;

A GetVal a Counter osztályban nem tagváltozó, hanem függvény, úgyhogy kellene mögé a zárójelpár. Itt a kijavított, működő kódod és a futtatás eredménye is: [link]

2012. márc. 18. 00:40
Hasznos számodra ez a válasz?
 10/10 A kérdező kommentje:
Köszönöm.
2012. márc. 19. 04:08

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!