0

이 프로그램은 매우 자명하므로이 프로그램의 목적을 실제로 이해하지 못합니다.나중에 프로그램에서 if else 구문 내에서 선언 된 변수를 사용하면 선언되지 않은 식별자 오류가 발생합니다.

내 주된 문제는 현재 82, 89, 95 및 101 행에 있는데 컴파일 할 때 "arr"및 "input"에 대해 "선언되지 않은 식별자"오류가 발생합니다.

if if else if 구조체 내부에 선언했기 때문에 이것이 가능합니까? 그렇다면이 문제를 해결할 수있는 방법이 있습니다. 사전에 도움을 주셔서 감사합니다 !!!!

여기 당신이 경우/다른 블록 내부를 선언하기 때문입니다 코드

#include <iostream> 
#include <string> 
using namespace std; 

template<class T> void selectionSort(T arr[], T num) 
{ 
    int pos_min; 
    T temp; 

    for (int i = 0; i < num - 1; i++) 
    { 
     pos_min = i; 

     for (int j = i + 1; j < num; j++) 
     { 
      for (arr[j] < arr[pos_min]) 
      { 
       pos_min = j; 
      } 
     } 


     if (pos_min != i) 
     { 
      temp = arr[i]; 
      arr[i] = arr[pos_min]; 
      arr[pos_min] = temp; 
     } 
    } 
} 

int main() 
{ 

    char check = 'C'; 

    while (toupper(check) != 'Q') 
    { 
     char dataType; 
     int num = 0; 

     cout << "What kind of data do you want to sort?" << endl; 
     cout << " For integer enter i, for string enter s, for character enter c. "; 
     cin >> dataType; 

     //User input dataType 
     if (toupper(dataType) == 'I') 
     { 
      int arr[100]; 
      int input; 
      cout << " You've chosen Integer dataType" << endl; 
     } 
     else if (toupper(dataType) == 'S') 
     { 
      string arr[100]; 
      string input; 
      cout << " You've chosen String dataType" << endl; 
     } 
     else if(toupper(dataType) == 'C') 
     { 
      char arr[100]; 
      char input; 
      cout << " You've chosen Character dataType" << endl; 
     } 
     else 
     { 
      cout << "Not a recognizable dataType. Shuting down..." << endl; 
      return -1; 
     } 

     //User input # of num 
     cout << "How many num will be sorted? "; 
     cin >> num; 

     for (int i = 0; i < num; i++) 
     { 
      cout << "Enter an input of the dataType you selected: "; 
      cin >> input; 
      arr[i] = input; 
     } 

     //Display user input 
     cout << "The data as you entered it: "; 
     for (int i = 0; i < num; i++) 
     { 
      cout << arr[i]; 
      cout << " "; 
     } 
     cout << endl; 

     //Sort user input by calling template functon selectionSort 
     selectionSort(arr, num); 

     //Display sorted user input 
     cout << "After sorting your data by calling selectionSort: "; 
     for (int i = 0; i < num; i++) 
     { 
      cout << arr[i]; 
      cout << " "; 
     } 

     cout << endl; 
     //Query user to quit or continue 
    cout << " Would you like to continue? Enter 'Q'. Enter anything else to continue."; 
    cin >> check; 

    } 



    return 0; 
} 
+1

실제로 문제가있는 부분으로 미리보기를 제한해야합니다. – pyon

+0

런타임에 사용자 선택에 따라 유형이 달라지는 변수 (즉,'array','input' 등)를 원한다고 생각합니다. 이것은 정적으로 (그리고 비 종속적으로) 유형화 된 언어 인 C++에서는 가능하지 않습니다. – pyon

답변

1

입니다. 블록이 완료되면이 변수는 scope을 벗어나 더 이상 액세스 할 수 없습니다.
이 문제를 해결하는 한 가지 방법은 문자 데이터로 입력을 항상 읽은 다음 사실 이후에 지정된 유형으로 변환하는 것입니다. char에서 int로 변환하는 방법은 atoi을 참조하십시오.

+0

'strtol'과'strtod '도 표준이며 훨씬 더 잘 작동합니다 (실제로 오류를 감지 할 수 있습니다). –

1

변수에 알 수없는 유형이있을 수 없습니다. 템플릿 내에서도 모든 변수의 유형은 특정 인스턴스화에 대해 고정되어 있습니다.

이는 해결책을 제시합니다. 여러 유형의 변수에서 작동하는 모든 코드를 템플릿 함수에 배치 할 수 있습니다.

당신은 유용한 임의의 요소 유형의 임의의 길이의 배열을 전달하기위한 템플릿 구문을 찾을 수 있습니다 :

template<typename T, size_t N> 
void func1(T (&arr)[N]) 
{ 
    //... 
} 

하지만 당신은 정말 심지어 배열을 전달 할 필요가 없습니다. 유형을 전달하고 함수 내에 배열을 만들 때 해당 유형을 사용하십시오.

template<typename T> 
void process_it() 
{ 
    T arr[100]; 
    T input; 

    // now work on them 
} 

어느 쪽이든, 당신은 정확한 유형을 알고 모든 if/else 지점, 내부에서이 함수를 호출해야합니다.