-
27-04-2016, 06:11 PM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Class CVector bị lỗi, các pro chỉ em với :)
File CVector.h
Mã:#include<iostream> using namespace std; class CVector { int n; double *coords; public: CVector(int number); CVector(CVector &a); ~CVector(); void Input(); void Output(); CVector Plus(CVector &a, CVector &b); CVector Minus(CVector &a, CVector &b); CVector Mul(CVector &a, int &k); CVector TVH(CVector &a, CVector &b); };
Mã:#include "Vector.h" CVector::CVector(int number) { n = number; coords = new double[n]; } CVector::~CVector() { delete coords; } CVector::CVector(CVector &a) { coords = a.coords; coords = new double[n]; for (int i = 0; i < n; i++) { coords[i] = a.coords[i]; } } void CVector::Input() { for (int i = 0; i < n; i++) { cout << "coords[" << i << "]: "; cin >> coords[i]; } } void CVector::Output() { for (int i = 0; i < n; i++) { cout << coords[i] << " "; } } CVector CVector::Plus(CVector &a, CVector &b) { CVector c = a; for (int i = 0; i < n; i++) { c.coords[i] = a.coords[i] + b.coords[i]; } return c; } CVector CVector::Minus(CVector &a, CVector &b) { CVector c = a; for (int i = 0; i < n; i++) { c.coords[i] = a.coords[i] - b.coords[i]; } return c; } CVector CVector::Mul(CVector &a, int &k) { CVector c = a; for (int i = 0; i < n; i++) { c.coords[i] = a.coords[i] * k; } return c; } CVector CVector::TVH(CVector &a, CVector &b) { CVector c = a; for (int i = 0; i < n; i++) { c.coords[i] = a.coords[i] * b.coords[i]; } return c; }
View more random threads:
- Sau khi inject code làm thế nào để chèn code.
- [Hỏi] Vùng Client trắng xóa khi sử dụng DC ảo
- Cách compiler trong VS C++ Express, chương trình không thay đổi khi sửa code?
- Thay đổi icon trong File Resource của một chương trình?
- Làm thế nào để thay đổi kick thước Icon trên Dialog trong MFC
- Cách nào tối ưu để DLL liên lạc với MyApplication
- Chương trình liệt kê các process của windows. Giúp mình sửa lỗi?
- Biến public trả về NULL khi gọi hàm trong cùng một Struct
- Viết chương trình chuyển trang website mà không sửa trong file host
- Chương trình chuyển đổi | Convert project từ Visual Basic 6 sang Visual C++ 6
-
28-04-2016, 06:00 PM #2Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Bạn xem lại cái này :
Mã:CVector::CVector(CVector &a){ coords = a.coords; coords = new double[n]; // n chưa được khởi tạo, dẫn đến vòng lặp bên dưới không chính xác. Hệ quả là ảnh hưởng tới các phương thức mà bạn nêu. for (int i = 0; i < n; i++) { coords[i] = a.coords[i]; }}
-
29-04-2016, 11:58 AM #3Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Code mình viết chạy được với điều kiện không định nghĩa hàm huỷ [IMG]images/smilies/biggrin.png[/IMG] Bạn tham khảo thử:
//Vector.h
Mã:class CVector { private: int n; double *p; public: CVector(); CVector(int); CVector(const CVector &); //~CVector(); void Nhap(); void Xuat(); int LayN(); CVector Cong(const CVector &); CVector Tru(const CVector &); CVector Nhan(double); //Nhân với một số thực double Nhan(const CVector &); //Nhân hai vector };
Mã:CVector::CVector() { n = 0; p = new double[n]; for (int i = 0; i <= n; i++) p[i] = 0; } CVector::CVector(int nn) { n = nn; p = new double[n]; for (int i = 0; i <= n; i++) p[i] = 0; } CVector::CVector(const CVector &u) { n = u.n; p = new double[n]; for (int i = 0; i < n; i++) { this->p[i] = u.p[i]; } } int CVector::LayN() { return n; } void CVector::Nhap() { for (int i = 0; i < n; i++) { cout << "Nhap toa do thu [" << i << "]: "; cin >> this->p[i]; } } void CVector::Xuat() { cout << "Vector: ("; for (int i = 0;i < n - 1; i++) { cout << this->p[i] << ","; } cout << this->p[n - 1] << ")"; } CVector CVector::Cong(const CVector &u) { CVector temp; temp.n = u.n; temp.p = new double[temp.n]; for (int i = 0; i < n; i++) { temp.p[i] = this->p[i] + u.p[i]; } return temp; } CVector CVector::Tru(const CVector &u) { CVector kq; kq.n = n; kq.p = new double[kq.n]; for (int i = 0; i < n; i++) { kq.p[i] = this->p[i] - u.p[i]; } return kq; } CVector CVector::Nhan(double x) { CVector kq; kq.n = n; kq.p = new double[kq.n]; for (int i = 0; i < n; i++) { kq.p[i] = this->p[i] * x; } return kq; } double CVector::Nhan(const CVector &u) { double kq1=0; for (int i = 0; i < n; i++) { kq1 = kq1 + (this->p[i] * u.p[i]); } return kq1; }
-
01-05-2016, 05:35 PM #4Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Thanks các bác nhé [IMG]images/smilies/smile.png[/IMG]
How to make gift basket bows? There are a couple ways to tackle making a bow for your gift basket, depending on the look you're going for and your desired level of complexity. Here are two...
How to make and tie a gift basket...