stockListType.cpp: 58: khởi tạo từ đâylỗi: đi qua 'const ...' 'là 'này' đối số của' ...' loại bỏ vòng loại
/usr/include/c++/4.2.1/bits/stl_algo.h:91: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers
/usr/include/c++/4.2.1/bits/stl_algo.h:92: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers
/usr/include/c++/4.2.1/bits/stl_algo.h:94: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers
/usr/include/c++/4.2.1/bits/stl_algo.h:98: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers
/usr/include/c++/4.2.1/bits/stl_algo.h:100: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers
Trên đây là lỗi tôi nhận và sẽ như ai đó giải thích cho tôi ý nghĩa của nó. Tôi đã giải quyết lỗi bằng cách đặt một hằng số ở phía trước toán tử nạp chồng. Chương trình của tôi là một ứng dụng thị trường chứng khoán mà đọc một tập tin bao gồm một chuỗi, 5 đôi và một int. Chúng tôi sắp xếp chương trình bằng các ký hiệu chuỗi và tăng chỉ số. Cuốn sách hướng dẫn tôi sử dụng vectơ để lưu trữ từng dữ liệu. Như bạn thấy bên dưới toán tử overload so sánh từng biểu tượng và sắp xếp nó bằng cách sử dụng hàm thành viên sắp xếp của các thùng chứa. Câu hỏi của tôi là tại sao tôi phải đặt một hằng số trước toán tử quá tải cho> và <. nhưng không cho> =, < =, ==,! = các toán tử quá tải.
//function was declared in stockType.h and implemented in stockType.cpp
bool operator<(const stockType& stock)//symbol is a string
{
return (symbols < stock.symbols)
}
//The function below was defined in stockListType.h and implemented in
// stockListType.cpp where I instantiated the object of stockType as a vector.
//vector<stockType> list; was defined in stockListType.h file
void insert(const& stockType item)
{
list.push_back(item);
}
void stockListType::sortStockSymbols()
{
sort(list.begin(), list.end());
}
'const & stockType item' nên: 'const stockType & item' – billz
nhờ OK một chàng trai rất nhiều tôi đánh giá cao nó – Emy