I'm studying operator overloading and can not understand how that is identified using the + + operator pre-set or post-set.
First, use proper code tags when posting code. There is no need for "Indent" tags if you just posted using code tags:
[code]
Your code goes here
[/code]
Example:
Code:
#include <iostream>
int main()
{
std::cout << "Hello World";
}
See how the code looks when I post as compared to when you post code?
Second, if you're studying operator overloading, why are you using such a confusing and wrong piece of code to study this? Please see the FAQ on overloading ++ here, and compare the simple example and explanation of this operator and how it is to be overloaded to what you're doing.
I am grateful for the support I have received from members of this forum and apologize for the mistakes of posting, no formatting required.
I created a new code to study the overhead of a binary operator, where the operator +, but the compiler returns the following error: "main.obj: -1: error: LNK2019: unresolved external symbol" public: __ thiscall sale sale :: (void) "(? 0venda @ @ QAE @ XZ) referenced in function _main".
I do not understand what is generating the error. Can anyone help me?
Follow the code:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
class venda
{
private:
int npecas;
float precos;
public:
venda();
venda(int np, float p) {npecas = np;precos = p;}
venda operator + (venda v) const;
void getvenda()
{
cout << "\nInsira o nr de pecas: "; cin >> npecas;
cout << "\nInsira o preco: "; cin >> precos;
}
void printvenda() const;
};
//int venda::npecas;
//float venda::precos;
venda venda::operator +(venda v) const
{
int pec = npecas + v.npecas;
float pre = precos + v.precos;
return venda(pec,pre);
}
void venda::printvenda() const
{
cout << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2)
<< setw(10) <<npecas;
cout << setw(10) << precos << '\n';
}
void main()
{
venda A(58,34.53), B, C(30,60.3), T, Total;
B.getvenda();
T = A + B;
Total= A + B + C;
cout << "Venda: A .............."; A.printvenda();
cout << "Venda: B .............."; B.printvenda();
cout << "Venda: A + B .........."; T.printvenda();
cout << "Venda: Totais A+B+C...."; Total.printvenda();
}
You need to study a bit more before creating your own examples:
Code:
string2 operator +=(string2 s) const //Concatena
Why are you passing string2 objects by value? You should be passing them by const reference, not by value.
Code:
string2 operator +=(string2 s) const //Concatena
Why is operator += returning a new object? It should be returning a reference to the existing object. That is the whole purpose of +=. It takes the existing object, adds something to it, and returns the existing object.
Everything I pointed out is covered in good books. Are you learning from real books? If you're just throwing things together without any guidance, that is not the way you learn C++. You're supposed to be using good books (and tutorials) on the language, i.e. see how the experts in the language do things. Putting stuff together yourself just leads to mistakes and a misunderstanding of how to use C++ properly.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 16th, 2012 at 11:57 PM.
Bookmarks