Calling by same function name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gayathri81
    New Member
    • Jul 2012
    • 25

    Calling by same function name

    I am doing coding in c++ using vectors.

    I have used 2 calling functions:
    virtual std::vector<tab le1>getText(std ::string);
    virtual std::vector<tab le2>getText(std ::string);

    As you can see, the name of the function, number and type of parameters are same. However the name of the table is different.

    In spite of that I get errors that:
    error: conflicting declaration std::vector<tab le1>,std::vecto r<table2>.

    Could someone help me out?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Consider your functions returning an array if int. One returns an array of Fibonacci numbers and the the other returns an array of triangle side dimensions. Both functons return an int* yet the arrays are different.

    So with your vectors. Each function returns a vectot<T> so the overload differs only by return type hence your compiler's problem.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Just to expand what weaknessforcats has said, the C++ standard explicitly states that you can not use function overloads that differ only by return type, they must differ in some over way as well (parameter type/number or function attributes (const)).

      Comment

      • gayathri81
        New Member
        • Jul 2012
        • 25

        #4
        Thanks for your reply. Actually I am trying to make geofences. Table 1 is triangle and table 2 is rectangle. Both of them can be accessed by its name whic is string. Is it possible to use the same parameter type and number of parameters?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          You may need a little polymorphism here. Does this work for you?

          First a base class:
          Code:
          class Geofence
          {
          public:
          
          	 
          };
          Then derived classes for each of your geofences.
          Code:
          class Triangle :public Geofence
          {
          public:
          
          
          private:
          		vector<int> arr; //your triangle data type
          
          };
          class Rectangle :public Geofence
          {
          public:
          
          
          private:
          		vector<int> arr; //your rectangle data type
          
          };
          The idea is you create a Triangle or Rectangle object and use it as a base class pointer. So all your code uses only the base class.

          To create your objects, use a factory. This oe creates a triangle or rectangle object based on specific input:
          Code:
          class GeofenceFactory
          {
          public:
          	Geofence* Create(std::string fence);
          };
          
          Geofence* GeofenceFactory::Create(std::string fence)
          {
          	if (fence == "Triangle")
          	{
          		return new Triangle;
          	}
          	if (fence == "Rectagle")
          	{
          		return new Rectangle;
          	}
          }
          Then in main() you create your factory on the heap, create your geofence object (or objects) then delete the factory.
          Code:
          int main()
          {
            GeofenceFactory* f = new GeofenceFactory; //create factory
            Geofence* triangle = f->Create("Triangle");
          
            Geofence* rectangle = f->Create("Rectangle");
          
             delete f;
          
            //you are now ready for business
          
          }
          In this scheme any fuction you write that has a base class pointer or reference argument can use the derived objects. The vectors have been hidden you you don't need the same implementation for each type of geofence.

          Comment

          • gayathri81
            New Member
            • Jul 2012
            • 25

            #6
            Thanks a lot....it worked out. I proceeded further. Could you tell me how to write vector c++ for point in polygon?

            Comment

            Working...