CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2010
    Posts
    4

    Problems accessing overloaded member functions

    The problem I am having is creating an object in main() using the overloaded Marble() functions. In the code fragment of main I have commented "//ref1" "//ref2" so you can easily help. The header file is at the bottom of this post.

    Code:
    #include "std_lib_facilities.h"
    #include "marble.h"
    
    int main(){
    	//Marble();			
    	//Marble m1;			
    	m1.Marble();		//ref 1
    	Marble("red");		//ref 2
    }
    Question for //ref1. Why does this not work? When I run it i get the error:
    error: invalid use of `class Marble'

    and

    Question for //ref2. Why does this not allow me to access the function Marble(Color c);? The error i get when I try this is:
    error: no matching function for call to `Marble::Marble(const char[4])'
    marble.h:13: note: candidates are: Marble::Marble(const Marble&)
    marble.h:54: note: Marble::Marble(Marble::Color, Marble::Size)
    marble.h:48: note: Marble::Marble(Marble::Size)
    marble.h:42: note: Marble::Marble(Marble::Color)
    marble.h:36: note: Marble::Marble()

    Code:
    #ifndef MARBLE_H_
    #define MARBLE_H_
    
    #include <iostream>
    
    using namespace std;
    
    class Marble
    {
    public:
      enum Color {red, blue, yellow, green};
      enum Size {small, medium, large};
    
    private:
      Color color;
      Size  size;
    
      // internal functions
    
      int random_color(){
      return (rand()%4);
      }
    
      int random_size(){
      return (rand()%3);
      }
    
    
    public:
      // constructors
      // write 4 constructors here:
      Marble(){			// -- random color and size
    	  color = (Color)this->random_color();
    	  size = (Size)this->random_size();
    	  cout<<"The Marble has color:  "<<color<<" and size:  "<<size<<"\n\n";
      }
    
      Marble(Color c){							// -- given color, random size
    	  color = c;
    	  size = (Size)this->random_size();
    	  cout<<"The Marble has color:  "<<color<<" and size:  "<<size<<"\n\n";
      }
    
      Marble(Size s){
    	  color = (Color)this->random_color();	// -- random color, given size
    	  size = s;
    	  cout<<"The Marble has color:  "<<color<<" and size:  "<<size<<"\n\n";
      }
    
      Marble(Color c, Size s){					// -- given color and size
    	  color = c;
    	  size = s;
    	  cout<<"The Marble has color:  "<<color<<" and size:  "<<size<<"\n\n";
      }
    
      // utility functions
      Color get_color() const
      {
      return (Color)color;
      }
      //------------------------------
      Size get_size() const
      {
      return (Size)size;
      }
    
      bool operator==(const Marble m) const;
    
      friend ostream& operator<<(ostream& os, const Marble& m);
    };
    
    
    #endif

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Problems accessing overloaded member functions

    The error is pretty explicit. You don't have a constructor that takes a const char[4]. "red" is a const char[4] as far as the compiler is concerned.

    Try:

    Code:
      Marble m1(Marble::red);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured