-
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:
- Lỗi Visual Studio 6.0: không bắt được process để debug
- Thay thế "singer slash" bằng "double slash" trong chuỗi trong VC++.NET như thế nào?
- dấu '&' trong nạp chồng toán tử C++
- Show mã asm của code lên xem làm như thế nào?
- Viết multithread như thế nào khi sử dụng thư viện CAsyncsocket?
- Tạo hộp thoại open file trong VC++
- Chương trình đọc dữ liệu từ cổng COM
- Cách build FFMPEG thành .lib .dll sử dụng trong MSVC?
- Lỗi: debug assertion failed xử lý thế nào?
- Tăng tốc độ Load File lên List ??
-
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]
Pallet nhựa Long An đã trở thành một trong những lựa chọn phổ biến cho nhu cầu vận chuyển và lưu trữ hàng hóa trong nhiều ngành công nghiệp. Với đặc tính nhẹ nhàng, chắc chắn và dễ vận chuyển, các...
Thanh lý pallet nhựa Long An giá rẻ