Devdit
 

error: 'strlen' was not declared in this scope C++ คือ วิธีแก้ไข

1.2K

ฝึกเขียนภาษา C++ ต้องการนับจำนวนตัวอักษรจากตัวแปรชนิด char ด้วยคำสั่ง strlen แต่ลองรันแล้วขึ้น error: 'strlen' was not declared in this scope ปัญหานี้ต้องแก้ไขอย่างไร

#include <iostream>

using namespace std;

int main()
{
    char name[] = "Hello, World";
    cout << strlen( name );
    return 0;
}

 

วิธีแก้ไข

ปัญหานี้เกิดจากยังไม่ได้ #include <cstring> เข้ามาในโค้ด เนื่องจากการคำสั่ง strlen อยู่ใน Class cstring แนะนำ #include <cstring> เข้ามาที่ส่วนด้านบนของโปรแกรมแล้วลองรันใหม่อีกครั้ง

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char name[] = "Hello, World";
    cout << strlen( name );
    return 0;
}

ผลลัพธ์

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