friend function prob.

This is a discussion on friend function prob. within the C++ Programming forums, part of the General Programming Boards category; hi, ive made the following operator a friend because i need to access the private vector<int> set...but for some reason ...

  1. #1
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69

    friend function prob.

    hi,

    ive made the following operator a friend because i need to access the private vector<int> set...but for some reason it gives me the following compile error
    IntSet.h: In function `const IntSet operator+(const IntSet&, const IntSet&)':
    IntSet.h:29: error: `std::vector<int, std::allocator<int> > IntSet::set' is
    private
    IntSet.cc:80: error: within this context

    please note this is line 80:
    Code:
     for (constSetIterator it = b.set.begin(); it!=b.set.end(); ++it) {
    ..y?

    Code:
    /* returns a union of the sets a and b */
    const IntSet operator+(const IntSet &a, const IntSet &b){
    
           IntSet newSet = a;
    
           for (constSetIterator it = b.set.begin(); it!=b.set.end(); ++it) {
               if (!newSet.contains(*it))
                  newSet.add(*it);
           }
    
           return newSet;
    }
    its protoype in the .h file is
    Code:
    friend IntSet operator+(const IntSet a, const IntSet b);

  2. #2
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    The prototype in your .h file is differente, it receives two objects, not two references. Is the prototype inside the IntSet class?

  3. #3
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    yea...i think that was the problem ...i put the non-member function in side the class making it a member function...

  4. #4
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    i have another similar problem...the compiler give the error
    IntSet.cc: In function `const IntSet operator+(const IntSet&, const IntSet&)':
    IntSet.cc:76: error: new declaration `const IntSet operator+(const IntSet&,
    const IntSet&)'
    IntSet.h:47: error: ambiguates old declaration `IntSet operator+(const IntSet&,

    const IntSet&)'

    this is the operator
    Code:
    const IntSet operator+(const IntSet &a, const IntSet &b){
    
           IntSet newSet = a;
    
           for (constSetIterator it = b.set.begin(); it!=b.set.end(); ++it) {
               if (!newSet.contains(*it))
                  newSet.add(*it);
           }
    
           return newSet;
    }
    and this is its prototype
    Code:
    friend IntSet operator+(const IntSet &a, const IntSet &b);
    btw..do u need to put the const in front of the method in the prototype..i thought you dont have to.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You don't put const "in front of the method". const is part of the return type. The friend declaration is for a function that returns an "IntSet". The definition is for a function that returns a "const IntSet". This invalidates nonsensical code such as:
    Code:
    (a + b) = c;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21