• Shopee đêm nay có mã cho ngày 5/5

thắc mắc Cao thủ C++ cho em hỏi tý ạ.

h1970

Senior Member
Em có ca này nhờ các cao nhân C++ trợ giúp. Đây là đoạn code mà em đang loay hoay mãi chưa hiểu tạo sao bị lỗi
C++:
#include <cstddef>
#include <iostream>

class IntArray;
class IntArrayIterator
{
private:
    const IntArray& fContainer;
    size_t fPosition;
public:
    IntArrayIterator(const IntArray& aContainer, size_t aStart = 0);
    const int operator*() const;
    IntArrayIterator& operator++(); // prefix
    bool operator==(const IntArrayIterator& aRHS) const;
    bool operator!=(const IntArrayIterator& aRHS) const;
    IntArrayIterator begin() const;
    IntArrayIterator end() const;
};
IntArrayIterator::IntArrayIterator(const IntArray& aContainer, size_t aStart): fContainer(aContainer), fPosition(aStart){}
const int IntArrayIterator::operator*() const
{
    return fContainer[fPosition];   
}
IntArrayIterator& IntArrayIterator::operator++()
{
    fPosition++;
    return *this;
}
bool IntArrayIterator::operator==(const IntArrayIterator& aRHS) const
{
    return fPosition == aRHS.fPosition;
}
bool IntArrayIterator::operator!=(const IntArrayIterator& aRHS) const
{
    return !(*this == aRHS);
}
class IntArray
{
private:
    const int* fElements;
    size_t fNumberOfElements;
public:
    IntArray(const int aArrayOfIntegers[], size_t aNumberOfElements);
    const int operator[](size_t aIndex) const;
    IntArrayIterator begin() const;
    IntArrayIterator end() const;
};
IntArray::IntArray(const int aArrayOfIntegers[], size_t aNumberOfElements) : fElements(aArrayOfIntegers), fNumberOfElements(aNumberOfElements){}
const int IntArray::operator[](size_t aIndex) const
{
    return fElements[aIndex];
}
IntArrayIterator IntArray::begin() const
{
    return IntArrayIterator(*this, 0);
}
IntArrayIterator IntArray::end() const
{
    return IntArrayIterator(*this, fNumberOfElements);
}
int main()
{
    int a[] = {5,10,15};
    IntArray iv(a, 3);
    for (const auto& it:iv)
    {
        std::cout << it << std::endl;
    }
    return 0;
}

Code này em có 2 classes. Một là IntArray là container cho mảng int, tính năng nó kiểu như vector rút gọn. Class còn lại là IntArrayIterator dùng để duyệt cái IntArray. Khi compile thì gặp lỗi ở dòng này

return fContainer[fPosition];

báo là no match for 'operator[]' (operand types are 'const IntArray' and 'const size_t' {aka 'const long long unsigned int'})

trong khi ở class IntArray em đã có cái operator[] ở đây rồi

const int IntArray::eek:perator[](size_t aIndex) const
{
return fElements[aIndex];
}

Cảm ơn các bác.
 
Em có ca này nhờ các cao nhân C++ trợ giúp. Đây là đoạn code mà em đang loay hoay mãi chưa hiểu tạo sao bị lỗi
C++:
#include <cstddef>
#include <iostream>

class IntArray;
class IntArrayIterator
{
private:
    const IntArray& fContainer;
    size_t fPosition;
public:
    IntArrayIterator(const IntArray& aContainer, size_t aStart = 0);
    const int operator*() const;
    IntArrayIterator& operator++(); // prefix
    bool operator==(const IntArrayIterator& aRHS) const;
    bool operator!=(const IntArrayIterator& aRHS) const;
    IntArrayIterator begin() const;
    IntArrayIterator end() const;
};
IntArrayIterator::IntArrayIterator(const IntArray& aContainer, size_t aStart): fContainer(aContainer), fPosition(aStart){}
const int IntArrayIterator::operator*() const
{
    return fContainer[fPosition];  
}
IntArrayIterator& IntArrayIterator::operator++()
{
    fPosition++;
    return *this;
}
bool IntArrayIterator::operator==(const IntArrayIterator& aRHS) const
{
    return fPosition == aRHS.fPosition;
}
bool IntArrayIterator::operator!=(const IntArrayIterator& aRHS) const
{
    return !(*this == aRHS);
}
class IntArray
{
private:
    const int* fElements;
    size_t fNumberOfElements;
public:
    IntArray(const int aArrayOfIntegers[], size_t aNumberOfElements);
    const int operator[](size_t aIndex) const;
    IntArrayIterator begin() const;
    IntArrayIterator end() const;
};
IntArray::IntArray(const int aArrayOfIntegers[], size_t aNumberOfElements) : fElements(aArrayOfIntegers), fNumberOfElements(aNumberOfElements){}
const int IntArray::operator[](size_t aIndex) const
{
    return fElements[aIndex];
}
IntArrayIterator IntArray::begin() const
{
    return IntArrayIterator(*this, 0);
}
IntArrayIterator IntArray::end() const
{
    return IntArrayIterator(*this, fNumberOfElements);
}
int main()
{
    int a[] = {5,10,15};
    IntArray iv(a, 3);
    for (const auto& it:iv)
    {
        std::cout << it << std::endl;
    }
    return 0;
}

Code này em có 2 classes. Một là IntArray là container cho mảng int, tính năng nó kiểu như vector rút gọn. Class còn lại là IntArrayIterator dùng để duyệt cái IntArray. Khi compile thì gặp lỗi ở dòng này

return fContainer[fPosition];

báo là no match for 'operator[]' (operand types are 'const IntArray' and 'const size_t' {aka 'const long long unsigned int'})

trong khi ở class IntArray em đã có cái operator[] ở đây rồi

const int IntArray::eek:perator[](size_t aIndex) const
{
return fElements[aIndex];
}

Cảm ơn các bác.
move toàn bộ phần khai báo cái class IntArray lên đầu đi, hiện tại mới khai báo cái tên class bên trên thôi, complier nó đọc từ trên xuống thì k thấy khai báo cái operator[] là đúng rồi còn gì.
 
Nhìn ông code gớm quá.

À lộn do compiler nó không thấy.

Muốn chạy đc thì move cái class IntArrayIterator này lên trên cùng.

Thứ 2 là tôi chưa thấy ai dùng reference để làm biến iterator cả, thay bằng pointer đi ông.

Thứ 3 là quăng cái class IntArrayIterator vào bên trong thằng IntArray để khỏi forward declaration.

Thứ 4 là mấy cái operator trỏ index như [] thì nhớ có error handling ko nó crash vỡ mặt.
throw std::eek:ut_of_range("Index out of range!");
 
Last edited:
1646058347499.png
Cho e hỏi ké luôn với, em mới học về kiểu dữ liệu có bàinhập 2 số a b tínhđến thương thì ko ra số 2.5 mà ra 2.0 dùđã khai báo float, vậy là saoạ?
 
Thứ 2 là tôi chưa thấy ai dùng reference để làm biến iterator cả, thay bằng pointer đi ông.

Bác chưa thấy ai thì bây giờ thấy. Mà hiện tại best practice người ta khuyến khích dùng reference thay thế hầu hết mọi trường hợp cho pointer. Dĩ nhiên là pointer vẫn có điểm hơn chẳng hạn check có null hay không, pointer thì ok, còn reference thì không. Link dưới nha bác:

https://stackoverflow.com/a/7058373/8707331

https://isocpp.org/wiki/faq/references#refs-vs-ptrs
 
Last edited:
Nhìn ông code gớm quá.

À lộn do compiler nó không thấy.

Muốn chạy đc thì move cái class IntArrayIterator này lên trên cùng.

Thứ 2 là tôi chưa thấy ai dùng reference để làm biến iterator cả, thay bằng pointer đi ông.

Thứ 3 là quăng cái class IntArrayIterator vào bên trong thằng IntArray để khỏi forward declaration.

Thứ 4 là mấy cái operator trỏ index như [] thì nhớ có error handling ko nó crash vỡ mặt.
throw std::eek:ut_of_range("Index out of range!");
Cảm ơn bác nhé. Thật ra cái class IntArrayIterator là nó thuộc về 1 file .h riêng, lẽ em phải #include vào nhưng để dễ đọc thì em bỏ cả vào 1 chỗ. Đó là yêu cầu của đầu bài ạ chứ nếu trộn chung vào thì nó lại không làm em đau đầu. Ngoài ra việc phải dùng reference cho iterator cũng là yêu cầu bắt buộc ạ. Còn vụ error handling thì em ghi nhận. Hiện tại em chưa làm gì vì đang tìm cách cho code nó chạy đã, error handling tính sau :D
move toàn bộ phần khai báo cái class IntArray lên đầu đi, hiện tại mới khai báo cái tên class bên trên thôi, complier nó đọc từ trên xuống thì k thấy khai báo cái operator[] là đúng rồi còn gì.
Em có forward declaration ở trên cho compiler nó đọc rồi mà. Bác đọc trả lời của em ở trên để biết em buộc phải làm thế vì thực ra 2 classes này nằm ở 2 file khác nhau, lẽ ra em phải #include cái IntArrayIterator vào IntArray mới đúng.
 
Back
Top