C++ Classes, Pointers, Overloaded Operators


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 15 of 15

Thread: C++ Classes, Pointers, Overloaded Operators

  1. #1
    Join Date
    Mar 2008
    Posts
    9

    Question C++ Classes, Pointers, Overloaded Operators

    I've been working on this program (Broken into 3 projects thus far, working on the 3rd part) since the beginning of the semester. We're working with a game of 3 handed poker. Previously, we made a Card class which held Rank and Suit; however, now we've made a CardArray class, which is composed of an int and a pointer to an array (of 52 elements of type Card) thats created on the fly. I seemed to have everything working but now after I've incorporated the overloaded operators, I keep getting linker errors. I've checked all the prototypes and I believe they all match up. Can anyone please take a look? Maybe theres something small I'm missing.

    I've attached all the files in a zip folder. You can ignore Deck.h, I haven't started that class yet. (The deck class will contain CardArray eventually)

    Thanks for any help
    Attached Files Attached Files

  2. #2
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    to overload any operator you have to make it member of a class ... in your Card class there is overloading for "<<" but you haven't included it in the class .

    HINT: make the overloading function "friend" it may helps in your case.
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  3. #3
    Join Date
    Mar 2008
    Posts
    9
    We were instructed not to use Friends, he told us to do it this way? Something about Friends of a class being able to access private data...
    Last edited by mdilliplane; 03-26-2008 at 10:46 PM.

  4. #4
    Join Date
    Mar 2008
    Posts
    9
    If you have some time Amahdy, I know you're busy with everyday stuff, I will be online and near my computer for about another 2 hours. Reading up on Overloading Operators :)
    Last edited by mdilliplane; 03-26-2008 at 10:53 PM.

  5. #5
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    yea anyway "friend function" will just help but you still can do it without "firend" ... and in all cases you have to make the overloded operator member of a class ...
    you can overload a function in anywhere but not the operators, only inside classess .
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  6. #6
    Join Date
    May 2007
    Posts
    843
    If you overload as friend, then u can used it in another class.

    If you overload it as member function, then it is only specific to a specific function.

    I hope this help.

  7. #7
    Join Date
    Mar 2008
    Posts
    9
    Well my professor tells me this will work and a handful of my classmates already finished the project and it works fine, although I recently got the first
    "<< cannot blah blah non-member" error

    I'm beating my head of the table haha I'll keep searching the good ol' web

  8. #8
    Join Date
    Mar 2008
    Posts
    9
    So where would I put it as a member function? inside Public?

  9. #9
    Join Date
    Mar 2008
    Posts
    9
    Here is what my assignment says, word for word:

    Card Class:

    Update the files for this class to include a prototype and implementation of a << operator for a Card. Printing of cards is to be done using this operator. And, it may NOT be a friend of the class (we know it can't be a class member).

    CardArray Class:
    Update the Array class by changing the ptr data member from type int to type Card. This requires importing the header of the Card class in the CardArray class. Then, update the functions that access the data to access elements of type Card instead of int

    Deck Class: (Haven't started this yet and I have no questions about it so I'm leaving it out)

  10. #10
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    As I told you before there is two things :
    -Overloading operators must be a class member
    -You still can do it without friend

    the declaration of the overloading will be something like :
    Code:
    ostream& operator << (ostream& X);
    and the definition will be:
    Code:
    ostream& MyClass::operator << (ostream& X)
    {
     return X<< this->getSomeStreamFromMyClass();
    }

    **small question: I'm confused, you knew my email from where :) ? this specifically I don't remember I told anybody about it !
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  11. #11
    Join Date
    Mar 2008
    Posts
    9
    Hey I actually just clicked the Yahoo thing in your profile, that gave me your yahoo id which i just figured would be your e-mail.

    So would I put the << operator above my Public and Private decs in the class?

  12. #12
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    nop you got my hotmail [which i don't use a lot FYI] .. but anyway you're welcome I just don't know you knew it from where as i don't tell anybody about it actually ...

    anyway ... private if you want to use it inside your class member functions only which doesn't have a sense !! because when you're inside the class you have access to all the class data and you don't need to have any operators ..
    otherwise the public one to use the operator from anywhere .

    hope this kan help :)
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  13. #13
    Join Date
    Mar 2008
    Posts
    9
    Can you tell me why the overloaded << and >> both work correctly in CardArray? They aren't part of the class but still work.

  14. #14
    Join Date
    Mar 2008
    Posts
    9
    I found this, which is exactly what my professor wants us to do...
    ----------
    C++ allows operators to be overloaded by functions that are not members of a class. To provide the overloading of the stream I/O operators the following function could be defined:

    ostream& operator<< (ostream& os , Array& a) {
    os << "[ ";
    for(int i = 0; i < 19; i++)
    os << a[i] << ", ";
    os << a[19] << " ]";
    }

    The class "ostream" used in this example is the actual type of the predefined library variable "cout" (similarly "cin" is implemented by the class "istream"). Upon examining the statement:

    cout << a;

    where "a" is an object of the class Array, the compiler will check if there is an overloaded operator of the form "operator<<(Array)" in the ostream class (which it will not find) and then will check if there is a non-member overloaded operator of the form "operator<<(ostream&, Array)" (which it will find).
    ----------

    I'm going to meet up with a classmate for some help but I will be checking back here as well..haha it's actually due at 11:59PM (about 4 hours from now) I'll let you guys know how it works out. Thanks so much for all the help!! :) Amahdy, you are the man!

  15. #15
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819

    Arrow

    well look at the attached version ...
    Attached Files Attached Files
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

Similar Threads

  1. operators overloading for (), [], ->
    By jinendrashankar in forum C++
    Replies: 1
    Last Post: 11-26-2007, 12:32 PM
  2. Secure classes in an assembly
    By abhijitbhosale in forum .NET
    Replies: 2
    Last Post: 08-16-2007, 12:33 PM
  3. Gradebook program
    By [gx]Shadow in forum Java
    Replies: 5
    Last Post: 10-25-2006, 11:20 PM
  4. java classes in windows and linux
    By DaveMere in forum Java
    Replies: 2
    Last Post: 08-09-2005, 02:11 PM
  5. COM+ SOAP 2.0 and Abstract Classes
    By Constant in forum VB Classic
    Replies: 0
    Last Post: 07-05-2002, 04:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center