absolut newbie question about overloading

E

Edith Gross

I should like to have

myclass a=b;

and

myclass a;
a = b;

So I must define a copy constructor AND overload 'operator=' (probably as
a friend. i.e.

friend myclass& operator=(myclass& c1, myclass& c2)

).

The overloaded function returns then c1.
Is all this correct?

TIA,

EG
 
M

Marcin Kalicinski

I should like to have
myclass a=b;

and

myclass a;
a = b;

So I must define a copy constructor AND overload 'operator=' (probably as
a friend.

You don't need to overload 'operator =', you have to define it. In C++
overloading occurs when you have more than one version of some function,
differing by parameters only. In case of 'operator =' you have only one, so
there is no overloading of any kind.

Yes, you have to define both 'operator =' and copy constructor. 'operator ='
must be a member function, and as such cannot be a friend. So the below is
wrong and will not compile.
friend myclass& operator=(myclass& c1, myclass& c2)

Instead, it should be:

myclass &operator =(const myclass &c)

(note the const parameter)

cheers,
M.
 
J

Justin Naidl

I'm having a similar problem but I can't get it figured out.

// In subscription.h
Subscription const & operator=(const Subscription &);

// In subscription.cpp
Subscription const& Subscription::eek:perator=(Subscription const
&copy_subscription)
{
subscriber_ID = copy_subscription.subscriber_ID;
comic_ID = copy_subscription.comic_ID;
subscription_ID = copy_subscription.subscription_ID;
return *this;
}

I'm using .net 2003
The error I get a linker error:
error LNK2019: unresolved external symbol "public: class Subscription
__thiscall Subscription:: operator = (class Subscription const &)"
(??4Subscription@@QAE...) referenced in function _main

The test code looks like this.

Subscription s1;
Subscription s2(166,167,168);
s1 = s2;
cout << s1;

I have an overloaded copy constructor. Please help I'm losing my mind on
this.

~Justin Naidl
 
V

Victor Bazarov

Justin said:
I'm having a similar problem but I can't get it figured out.

// In subscription.h

Is that inside the 'Subscription' class definition or not?
Subscription const & operator=(const Subscription &);
^^^^^
Notice the const here.
// In subscription.cpp
Subscription const& Subscription::eek:perator=(Subscription const
^^^^^
Notice the const here as well.
&copy_subscription)
{
subscriber_ID = copy_subscription.subscriber_ID;
comic_ID = copy_subscription.comic_ID;
subscription_ID = copy_subscription.subscription_ID;
return *this;
}

I'm using .net 2003
The error I get a linker error:
error LNK2019: unresolved external symbol "public: class Subscription ^^^^^^^^^^^^^^^^^^^^^^^^^^
__thiscall Subscription:: operator = (class Subscription const &)"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Notice that there is no 'const' anywhere between 'public' and the opening
parenthesis.
(??4Subscription@@QAE...) referenced in function _main

The test code looks like this.

Subscription s1;
Subscription s2(166,167,168);
s1 = s2;
cout << s1;

I have an overloaded copy constructor. Please help I'm losing my mind on
this.

The copy constructor has nothing to do with it.

The copy assignment operator for class A usually has the signature

A& operator =(A const&);

Yours for whatever reason returns a reference to a const object. Why?

V
 
J

Justin Naidl

Victor Bazarov said:
Is that inside the 'Subscription' class definition or not?

^^^^^
Notice the const here.

^^^^^
Notice the const here as well.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Notice that there is no 'const' anywhere between 'public' and the opening
parenthesis.


The copy constructor has nothing to do with it.

The copy assignment operator for class A usually has the signature

A& operator =(A const&);

Yours for whatever reason returns a reference to a const object. Why?

V

Even when I remove the const I get the same error. I put the const there
because the last web page I looked at for help had the const there. I was
trying anything I could find. Also, the operator is declared inside the
class definition.

Thanks for any help
~Justin

Just for reference, here is the code again without the const.
// In subscription.h
Subscription & operator=(const Subscription &);

// In subscription.cpp
Subscription & Subscription::eek:perator=(Subscription const
&copy_subscription)
{
subscriber_ID = copy_subscription.subscriber_ID;
comic_ID = copy_subscription.comic_ID;
subscription_ID = copy_subscription.subscription_ID;
return *this;
}
 
V

Victor Bazarov

Justin said:
[...]
Even when I remove the const I get the same error. I put the const there
because the last web page I looked at for help had the const there. I was
trying anything I could find. Also, the operator is declared inside the
class definition.

Thanks for any help
~Justin

Just for reference, here is the code again without the const.
// In subscription.h
Subscription & operator=(const Subscription &);

// In subscription.cpp
Subscription & Subscription::eek:perator=(Subscription const
&copy_subscription)
{
subscriber_ID = copy_subscription.subscriber_ID;
comic_ID = copy_subscription.comic_ID;
subscription_ID = copy_subscription.subscription_ID;
return *this;
}


The FAQ 5.8 addresses this.

V
 
J

Justin Naidl

Here is the faq friendly version of my code. I was having trouble with the
code I had compiling on .NET I was getting a linking error. error LNK2019:
unresolved external symbol "public: class Subscription __thiscall
Subscription:: operator = (class Subscription const &)" When it linked with
the main code. I have not tried compiling it as it shows here on .net it
was set up with a header file and .cpp file for the implentation of the
class and a .cpp for the main program. When I compiled the code as it is
shown here on linux g++ I had not trouple and it worked fine. So if anybody
knows of anything with .net that would get uppity while linking this please
tell me. Otherwise let me know what is wrong with my code. Thanks in
advance.

~Justin

#include <iostream>
using namespace std;

class Subscription
{
friend ostream& operator <<(ostream &out, const Subscription
&subscription)
{
out << "Subscriber: " << subscription.subscriber_ID << endl;
out << "Comic: " << subscription.comic_ID << endl;
out << "Subscription: " << subscription.subscription_ID;
return out;
}

protected:
long subscriber_ID;
long comic_ID;
long subscription_ID;

public:
// constructors
Subscription()
{
subscriber_ID = 0;
comic_ID = 0;
subscription_ID =0;
}

Subscription(const Subscription &copy_subscription)
{
this->subscriber_ID = copy_subscription.subscriber_ID;
this->comic_ID = copy_subscription.comic_ID;
this->subscription_ID = copy_subscription.subscription_ID;
}

Subscription(const long &new_subscriber_ID,
const long &new_comic_ID,
const long &new_subscription_ID)
{
subscriber_ID = new_subscriber_ID;
comic_ID = new_comic_ID;
subscription_ID = new_subscription_ID;
}

// destructor
~Subscription()
{
}

// accessors
long get_subscriber_ID()
{
return subscriber_ID;
}

long get_comic_ID()
{
return comic_ID;
}

long get_subscription_ID()
{
return subscription_ID;
}

// mutators
void set_subscriber_ID(const long &new_subscriber_ID)
{
subscriber_ID = new_subscriber_ID;
}

void set_comic_ID(const long &new_comic_ID)
{
comic_ID = new_comic_ID;
}

void set_subscription_ID(const long &new_subscription_ID)
{
subscription_ID = new_subscription_ID;
}

Subscription & operator=(Subscription const &copy_subscription)
{
subscriber_ID = copy_subscription.subscriber_ID;
comic_ID = copy_subscription.comic_ID;
subscription_ID = copy_subscription.subscription_ID;
return *this;
}
};
 
V

Victor Bazarov

Justin said:
Here is the faq friendly version of my code.

I am actually not sure about that. I took your code, slapped

int main() { return 0; }

on the end, and it compiled just fine in VC++ v7.1 (.NET).
I was having trouble with the
code I had compiling on .NET I was getting a linking error. error LNK2019:
unresolved external symbol "public: class Subscription __thiscall
Subscription:: operator = (class Subscription const &)" When it linked with
the main code.

Perhaps you should reduce your "main code" to the bare minimum and post it
as well? Just a thought...
I have not tried compiling it as it shows here on .net it
was set up with a header file and .cpp file for the implentation of the
class and a .cpp for the main program. When I compiled the code as it is
shown here on linux g++ I had not trouple and it worked fine. So if anybody
knows of anything with .net that would get uppity while linking this please
tell me. Otherwise let me know what is wrong with my code.

Nothing is wrong. The code as posted is just fine.

[..code snipped..]

V
 
J

Justin Naidl

Ok, I found the problem. I forgot that I had to update the header file in
two different places. One where the implementation was located and I had to
have a copy of that along with the main program as well because it was in a
different directory. I know, I'm a moron. Thanks for the help though.

~Justin


Victor Bazarov said:
Justin said:
Here is the faq friendly version of my code.

I am actually not sure about that. I took your code, slapped

int main() { return 0; }

on the end, and it compiled just fine in VC++ v7.1 (.NET).
I was having trouble with the
code I had compiling on .NET I was getting a linking error. error
LNK2019: unresolved external symbol "public: class Subscription
__thiscall Subscription:: operator = (class Subscription const &)" When
it linked with the main code.

Perhaps you should reduce your "main code" to the bare minimum and post it
as well? Just a thought...
I have not tried compiling it as it shows here on .net it
was set up with a header file and .cpp file for the implentation of the
class and a .cpp for the main program. When I compiled the code as it is
shown here on linux g++ I had not trouple and it worked fine. So if
anybody knows of anything with .net that would get uppity while linking
this please tell me. Otherwise let me know what is wrong with my code.

Nothing is wrong. The code as posted is just fine.

[..code snipped..]

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top