2014-01-27 2 views
-6

누구든지이 코드에서 오류를 해결할 수 있습니까?오버로드 된 기능에서 오류가 발생했습니다.

# include<iostream> 
using namespace std; 

void bubble_sort (int a[], int size); 
float bubble_sort (float b[], int size); 
int read (int a[], int size); 
float read (float b[], int size); 
void display (int a[], int size); 
void display (float b[], int size); 


    int main() 

{ 

    int const size=10; 

    int a[10]; 

    int b[10]; 

    read (a,size); 

    read (b,size); 

    bubble_sort(a,size); 

    bubble_sort(b,size); 

    display(a, size); 

    display(b, size); 

    return 0; 
} 

    int read (int a[], int size) 
     { 
      cout<<"Enter the integer values:"; 
      for (int i=0; i<10;i++) 
       cin>>a[i]; 

     } 

    float read (float b[], int size) 
     { 
      cout<<"Enter the float values :"; 
      for (int i=0; i<10;i++) 
        cin>>b[i]; 
     } 

    void bubble_sort (int a[], int size) 
     { 
      for (int i = 0; i < size; i++) 
      { 
       for (int j = 0; j < size - 1; j++) 
       if (a[j]>a[j + 1]) 
       { 
        swap = a[j]; 
        a[j] = a[j + 1]; 
        a[j + 1] = swap; 
       } 
      } 
     } 

    float bubble_sort (float b[], int size) 
     { 
      for (int i = 0; i < size; i++) 
      { 
       for (int j = 0; j < size - 1; j++) 
       if (b[j]>b[j + 1]) 
       { 
        swap = b[j]; 
        b[j] = b[j + 1]; 
        b[j + 1] = swap; 
       } 
      } 
     } 


    void display (int a[], int size) 
     { 
      for (int i=0;i<size;i++) 
      cout<<a[i]<<" "; 

     } 

    void display (float b[], int size) 
     { 
        cout<<endl; 
      for(int i=0 ;i<size; i++) 
       cout<<b[i]<<" "; 
     } 

컴파일러는 컨텍스트 유형 정보없이 오버로드 된 기능을 보여줍니다. 하지만 오버로드 된 함수를 사용해야합니다. 내가 어떻게 해결할 수 있니? bubble_sort에서

In function ‘void bubble_sort(int*, int)’: 
error: overloaded function with no contextual type information 
       swap = a[j]; 
        ^
error: cannot resolve overloaded function ‘swap’ based on conversion to type ‘int’ 
       a[j + 1] = swap; 
+0

대신 함수 템플릿을 시도하십시오 – taocp

+0

_ '컴파일러는 컨텍스트 유형 정보없이 오버로드 된 함수를 보여줍니다'어떻게? 오류 텍스트? –

+0

당신은'swap'을 사용하고 있으며 그것을 선언하지 않았습니다. 'std :: swap' 함수 템플릿이있을 수 있습니다. – chris

답변

3

, 당신은 swap = a[j];을하고 있지만 실제로 swap 변수를 선언하지 않았습니다.

float swap; 

당신은 std::swap 기능이 있기 때문에 당신이지고있는 오류가있어 당신이 using namespace std;을 수행 한 : 당신이 그런 짓을 의도 한 수 있음을 보인다. 이것은 정확하게 당신이하지 말아야 할 이유 중 하나입니다 using namespace std;. 당신이 그것을하지 않은 경우, 오류가 훨씬 명확하게 될 것이다 : 또한 당신의 readbubble_sort 기능에서 아무것도 반환하지 않는 것을

error: ‘swap’ was not declared in this scope 

참고.

1

1. 배열 중 둘 모두 []와 b []는 정수 배열입니다.

2.in function read (int *, int) 및 float read (int *, int), 아무 것도 반환하지 않습니다.

3. 컴파일러는 스왑을 스왑이 iostream의 미리 정의 된 함수이고이 변수를 정의하지 않았기 때문에 문맥 정보없이 오버로드한다고합니다.

관련 문제