Lỗi sqrt was not declared in this scope trong c năm 2024

In the first you're trying to call a function named square[], and neither I nor your compiler can find a declaration of that function.

Last edited on

When the compiler says an identifier isn't within scope, it means it doesn't know what the identifier is; it's never seen it before; you've never told it what it is.

To fix this, you'll need to declare

square[]

. However, it seems you want the function to compute the square root. For that, you'll need

sqrt[]

.

Wazzak

Framework wrote:To fix this, you'll need to declare square[]. However, it seems you want the function to compute the square root. For that, you'll need sqrt[].

I think they're actually looking for a function to "square" i. The pow[] function should suffice.

Awesome. The pow[] function worked like butter. Thanks Zhuge, Volatile Pulse, and Framework.

You shouldn't bother with using pow for this kind of simple calculation. Just use i*i. pow is for weird stuff like 2-0.37

Trong bài này mình sẽ giới thiệu một số lỗi biên dịch thường gặp khi biên dịch chương trình C/C++ bằng trình biên dịch GCC trên Linux/Unix và cách khắc phục.

1. undefined reference to ***

Đây là lỗi linker, xuất hiện khi linker không thể tìm thấy symbol đã được sử dụng. Thông thường nó hay xảy ra khi các hàm của một thư viện nào đó được sử dụng trong source code nhưng thư viện đó không được link.

Để thực hiện link một thư viện nào đó thì config như sau →

Với qmake

Với cmake

TARGET_LINK_LIBRARIES[target nameOfLib]

Lệnh g++

g++ -o main main.cpp -LdirOfLibrary -lnameOfLib

hoặc link trực tiếp bằng cách chỉ định file obiect như sau

g++ -o main main.o functionsModule.o

functionsModule.o là file object chứa code của function được sử dụng bởi main.o

2. error: *** was not declared in this scope

Lỗi này xảy ra khi một biến hoặc hàm không xác định nào đó được sử dụng.

— Trường hợp lỗi xảy ra với ‘biến’

Compiling error →

include

int main[int argc, char *argv[]]

{

{

int i \= 2;

}

std::cout

Chủ Đề