473,396 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Calling by same function name

I am doing coding in c++ using vectors.

I have used 2 calling functions:
virtual std::vector<table1>getText(std::string);
virtual std::vector<table2>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<table1>,std::vector<table2>.

Could someone help me out?
Jul 16 '12 #1

✓ answered by weaknessforcats

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.

5 2028
weaknessforcats
9,208 Expert Mod 8TB
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.
Jul 16 '12 #2
Banfa
9,065 Expert Mod 8TB
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)).
Jul 16 '12 #3
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?
Jul 17 '12 #4
weaknessforcats
9,208 Expert Mod 8TB
You may need a little polymorphism here. Does this work for you?

First a base class:
Expand|Select|Wrap|Line Numbers
  1. class Geofence
  2. {
  3. public:
  4.  
  5.  
  6. };
Then derived classes for each of your geofences.
Expand|Select|Wrap|Line Numbers
  1. class Triangle :public Geofence
  2. {
  3. public:
  4.  
  5.  
  6. private:
  7.         vector<int> arr; //your triangle data type
  8.  
  9. };
  10. class Rectangle :public Geofence
  11. {
  12. public:
  13.  
  14.  
  15. private:
  16.         vector<int> arr; //your rectangle data type
  17.  
  18. };
  19.  
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:
Expand|Select|Wrap|Line Numbers
  1. class GeofenceFactory
  2. {
  3. public:
  4.     Geofence* Create(std::string fence);
  5. };
  6.  
  7. Geofence* GeofenceFactory::Create(std::string fence)
  8. {
  9.     if (fence == "Triangle")
  10.     {
  11.         return new Triangle;
  12.     }
  13.     if (fence == "Rectagle")
  14.     {
  15.         return new Rectangle;
  16.     }
  17. }
  18.  
Then in main() you create your factory on the heap, create your geofence object (or objects) then delete the factory.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   GeofenceFactory* f = new GeofenceFactory; //create factory
  4.   Geofence* triangle = f->Create("Triangle");
  5.  
  6.   Geofence* rectangle = f->Create("Rectangle");
  7.  
  8.    delete f;
  9.  
  10.   //you are now ready for business
  11.  
  12. }
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.
Jul 17 '12 #5
Thanks a lot....it worked out. I proceeded further. Could you tell me how to write vector c++ for point in polygon?
http://bytes.com/topic/c/answers/941695-point-polygon
Jul 17 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Wootaek Choi | last post by:
Hi, everybody.. I try to make common debug function that call in every js files used in a HTML.. but if same debug function name is defined in.... more than two javascrpt files, it would...
3
by: man.shahi | last post by:
Hi all, i have a html page with some javascript functions in a js file linked to it. now, if i create a function in the html page with the same name as one of js functions, how can i call the...
3
by: Martijn | last post by:
Hi, I have come unto a situation where two source files of one project contain similar functions with the same name (it reads settings from a file and thus named FetchSettings). When I...
10
by: Peter | last post by:
Hi, how can I do this (I don't really want to do this but what I do want the same function name but have different return types and the compiler keeps saying "public function .... they differ only...
15
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which...
1
by: PengYu.UT | last post by:
Hi, Are there any walkaround to enable functions in the derived class with the same function name but different return type? In the following example, D1 and D2 are B's derived class. I want...
3
by: jacelyn2211 | last post by:
can i have 2 function with same function name but different argument in a javascript? my code are as followings:- function crtHidObj(name,value,idx){ crtHidObj(name,value); ...
7
by: TkNeo | last post by:
I don't know the exact terminology in python, but this is something i am trying to do i have 3 functions lets say FA(param1,param2) FB(param1,param2) FC(param1,param2) temp = "B" #something...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.