Ebben mi lehet a hiba? C++
//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(); //Konstruktor
Counter(int initialValue); //Konstruktor_2
Counter(Counter &); //Copy konstruktor
~Counter(); //Destruktor
const Counter& operator++ (); //prefix
const Counter operator++ (int); //postfix
int GetItsval()const;
void SetItsval(int x);
Counter Add(const Counter &);
private:
int itsVal;
};
Counter::Counter():
itsVal(0)
{cout <<"Konstruktor \n";}
Counter::Counter(int initialValue):
itsVal(initialValue)
{cout <<"Konstruktor_2 \n";}
Counter::Counter(Counter &rhs)
{cout <<"Copy konstruktor \n";}
const Counter& Counter::operator++ ()
{cout <<"opeator++ prefix \n";}
const Counter Counter::operator++ (int)
{cout <<"operator++ postfix \n";}
int Counter::GetItsval()const
{return itsVal;}
void Counter::SetItsval(int x)
{itsVal = x;}
Counter Counter::Add(const Counter &rhs)
{
return Counter(itsVal + rhs.GetItsval());
}
int main()
{
Counter VarOne(2),VarTwo(4),VarThree;
VarThree = VarOne.Add(VarTwo);
cout <<"VarOne: "<< VarOne.GetItsval() << endl
<<"VarTwo: "<< VarTwo.GetItsval() << endl
<<"VarThree "<< VarThree.GetItsval() << endl;
return EXIT_SUCCESS;
}
Hibaüzi:
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp||In member function 'const Counter& Counter::operator++()':|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|42|warning: no return statement in function returning non-void [-Wreturn-type]|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp||In member function 'const Counter Counter::operator++(int)':|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|45|warning: no return statement in function returning non-void [-Wreturn-type]|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp||In member function 'Counter Counter::Add(const Counter&)':|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|55|error: no matching function for call to 'Counter::Counter(Counter)'|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|55|note: candidates are:|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|38|note: Counter::Counter(Counter&)|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|38|note: no known conversion for argument 1 from 'Counter {aka Counter}' to 'Counter& {aka Counter&}'|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|34|note: Counter::Counter(int)|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|34|note: no known conversion for argument 1 from 'Counter {aka Counter}' to 'int'|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|30|note: Counter::Counter()|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|30|note: candidate expects 0 arguments, 1 provided|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|56|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build finished: 8 errors, 3 warnings ===|
Még így sem 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(); //Konstruktor
Counter(int initialValue); //Konstruktor_2
Counter(Counter &); //Copy konstruktor
~Counter(); //Destruktor
const Counter& operator++ (); //prefix
const Counter operator++ (int); //postfix
int GetItsval()const;
void SetItsval(int x);
Counter Add(const Counter &);
private:
int itsVal;
};
Counter::Counter():
itsVal(0)
{cout <<"Konstruktor \n";}
Counter::Counter(int initialValue):
itsVal(initialValue)
{cout <<"Konstruktor_2 \n";}
Counter::Counter(Counter &rhs)
{cout <<"Copy konstruktor \n";}
Counter::~Counter()
{cout <<"Destruktor \n";}
const Counter& Counter::operator++ ()
{cout <<"opeator++ prefix \n"; return *this;}
const Counter Counter::operator++ (int)
{cout <<"operator++ postfix \n"; return this;}
int Counter::GetItsval()const
{return itsVal;}
void Counter::SetItsval(int x)
{itsVal = x;}
Counter Counter::Add(const Counter &rhs)
{
return Counter(itsVal + rhs.GetItsval());
}
int main()
{
Counter varOne(2),varTwo(4),varThree;
varThree = varOne.Add(varTwo);
cout <<"varOne: "<< varOne.GetItsval() << endl
<<"varTwo: "<< varTwo.GetItsval() << endl
<<"varThree "<< varThree.GetItsval() << endl;
return EXIT_SUCCESS;
}
A copy konstruktorban konstans referenciával lenne érdemes átadnod a paramétert, meg érdemes lenne az itsVal-t másolnod:
Counter::Counter(const Counter &rhs): itsVal(rhs.itsVal)
{cout <<"Copy konstruktor \n";}
Az op++ nem void, úgyhogy kéne mondjuk egy "return *this;" beléjük. A destruktort meg vagy ne deklaráld külön vagy definiáld is.
Valamiért 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(); //Konstruktor
Counter(int initialValue); //Konstruktor_2
Counter(const Counter &); //Copy konstruktor
~Counter(); //Destruktor
const Counter& operator++ (); //prefix
const Counter operator++ (int); //postfix
int GetItsval()const;
void SetItsval(int x);
Counter Add(const Counter &);
private:
int itsVal;
};
Counter::Counter():
itsVal(0)
{cout <<"Konstruktor \n";}
Counter::Counter(int initialValue):
itsVal(initialValue)
{cout <<"Konstruktor_2 \n";}
Counter::Counter(const Counter &rhs)
{cout <<"Copy konstruktor \n";}
Counter::~Counter()
{cout <<"Destruktor \n";}
const Counter& Counter::operator++ ()
{cout <<"opeator++ prefix \n"; return *this;}
const Counter Counter::operator++ (int)
{cout <<"operator++ postfix \n"; return this;}
int Counter::GetItsval()const
{return itsVal;}
void Counter::SetItsval(int x)
{itsVal = x;}
Counter Counter::Add(const Counter &rhs)
{
return Counter(itsVal + rhs.GetItsval());
}
int main()
{
Counter varOne(2),varTwo(4),varThree;
varThree = varOne.Add(varTwo);
cout <<"varOne: "<< varOne.GetItsval() << endl
<<"varTwo: "<< varTwo.GetItsval() << endl
<<"varThree "<< varThree.GetItsval() << endl;
return EXIT_SUCCESS;
}
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp||In member function 'const Counter Counter::operator++(int)':|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|48|error: invalid conversion from 'Counter* const' to 'int' [-fpermissive]|
C:\Users\ALWAYS_WINS\Desktop\Code Blocks mentesek\Practice_5\main.cpp|34|error: initializing argument 1 of 'Counter::Counter(int)' [-fpermissive]|
||=== Build finished: 2 errors, 0 warnings ===|
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
Ha kifogással szeretne élni valamely tartalommal kapcsolatban, kérjük jelezze e-mailes elérhetőségünkön!