Devdit
 

error: no matching function for call to 'toupper(char [2])' คือ วิธีแก้ไข

761

ภาษา C++ ต้องการเปลี่ยนตัวอักษรจากตัวพิมพ์เล็กให้เป็นตัวพิมพ์ใหญ่ ด้วยคำสั่ง strlen แต่รันแล้วไม่สำเร็จขึ้นข้อความ error: no matching function for call to 'toupper(char [2])' แบบนี้ต้องแก้ไขอย่างไร

#include <iostream>

using namespace std;

int main()
{
    char text[] = "d";
    char c;
    c = toupper( text );
    cout << c;
    return 0;
}

 

วิธีแก้ไข

คำสั่ง toupper รับค่า argument เป็นตัวอักษร 1 ตัว จากตัวอย่างโปรแกรมให้ส่ง text[0] เข้าไปในคำสั่ง toupper แทน เนื่องจาก text[0] จะอ้างถึง char 1 ตัว สามารถแก้ไขโปรแกรมได้ดังนี้

#include <iostream>

using namespace std;

int main()
{
    char text[] = "d";
    char c;
    c = toupper( text[0] );
    cout << c;
    return 0;
}

ผลลัพธ์

D
เขียน 2 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ