|
-
Feb 27th, 2002, 09:40 PM
#1
Thread Starter
Fanatic Member
Friend functions and VC++
I put together this code after looking at some other code here:
Code:
class Thing
{
private:
int x;
public:
Thing();
Thing(int);
void set(int);
friend Thing operator +(const Thing&, const Thing&);
friend ostream operator <<(const ostream&, const Thing&);
};
I get this error on the bold line:
c:\cpp\oper\thing.h(12) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.
What does that mean?
Alcohol & calculus don't mix.
Never drink & derive.
-
Feb 28th, 2002, 03:45 AM
#2
transcendental analytic
It means that you are ****ed, because MSVC6 sucks.
You have to copy in blocks of code until you can track down the error, and then post that line here (along with the rest).
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Feb 28th, 2002, 06:52 AM
#3
Fanatic Member
what does friend do? i saw it somewhere too, but didn't understand what it was for/meant
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Feb 28th, 2002, 08:03 AM
#4
transcendental analytic
friend is a anti OOP thing it allows access of a class' private members to a global function
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Feb 28th, 2002, 02:51 PM
#5
Monday Morning Lunatic
Originally posted by kedaman
friend is a anti OOP thing it allows access of a class' private members to a global function
Nope, it allows access to private or protected members from any named item.
Frequently used for things like linked lists:
Code:
class list {
class link {
friend class list;
};
public:
void add(...);
};
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 1st, 2002, 05:14 PM
#6
Could it be that you shouldn't specify the return type? The name and the parameters are enough to identify the function, so I think you don't need to specify type.
friend certainly has it's uses. But you can usually avoid it. e.g. a global + operator can be realized by giving the class a += operator.
Code:
class MyBadClass
{
int m_i;
friend operator +(const MyBadClass&, int);
friend operator +(int, const MyBadClass&);
};
MyBadClass operator +(int i, const MyBadClass& rbc)
{
MyBadClass tmp;
tmp = rbc;
tmp.m_i += i;
return tmp;
}
class MyGoodClass
{
public:
MyGoodClass& operator +=(int i) { m_i += i; return *this;};
};
MyGoodClass operator +(int i, const MyGoodClass& rgc)
{
MyGoodClass tmp = rgc;
tmp += i;
return tmp;
}
That's both the bad and the good way to do it.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 1st, 2002, 06:35 PM
#7
Thread Starter
Fanatic Member
Originally posted by CornedBee
Code:
class MyGoodClass
{
public:
MyGoodClass& operator +=(int i) { m_i += i; return *this;};
};
Kinds off the subject, but can you explain this line to me? Why do you have an ampersand in the return type, and why return *this? (Sorry, I'm new at this and can't find a good explanation anywhere.)
Alcohol & calculus don't mix.
Never drink & derive.
-
Mar 1st, 2002, 06:47 PM
#8
transcendental analytic
Originally posted by parksie
Nope, it allows access to private or protected members from any named item.
Frequently used for things like linked lists:
Code:
class list {
class link {
friend class list;
};
public:
void add(...);
};
Come on parksie! I know this stuff!
1. The object in OOP can do two things a) encapsulates contained data and provide access trough accessors and methods, b) delegate other objects trough its accessors.
2. Everything is an Object, there should not be any global functions, in other words, all functions are bound to their first parameter, their object.
Hence these paradoxal facilities:
1. Friend functions and classes
2. Protected inheritance
3. Functors
OOP can't work alone, its too obvious, functional style does.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Mar 1st, 2002, 06:51 PM
#9
transcendental analytic
Originally posted by Wynd
Kinds off the subject, but can you explain this line to me? Why do you have an ampersand in the return type, and why return *this? (Sorry, I'm new at this and can't find a good explanation anywhere.)
the ampersand before the type means that its a reference to the type, returning a& for a+=b means that you can still use a for further reference, ex: (a+=b)*=c
*this is dereferenced this, and this points to the object of which function you have called
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Mar 2nd, 2002, 04:15 AM
#10
Since there is no int class that you can modify, global + operators are necessary if you want to add a predefined type to your own class and want this type as first parameter.
Like:
Code:
int i = 4;
MC mc1(2, 3), mc2;
mc2 = mc1 + i; // this can be handled inside the class
mc2 = i + mc2; // this requires a global operator
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 2nd, 2002, 05:13 AM
#11
transcendental analytic
Yep, but that's faaar from OOP
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Mar 2nd, 2002, 08:31 AM
#12
Still I'm happy when I can do "predefined type" + "object" and not only "object" + "predefined type".
Didn't you complain just some days ago that Java is so fixed on objects?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 2nd, 2002, 08:46 AM
#13
transcendental analytic
I did, and I didn't insist OOP was good in any ways 
You'll like how SQ handle things
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|