-
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:
- Get Ip Router
- cho mình hỏi về node
- Lỗi Cannot open include file, làm sao khắc phục?
- Vẽ hình trái tim
- thông báo lỗi trong khi lập trình đồ họa trong lâp trình C
- Synchronized - Cách truyền một giá trị giữa 2 project trong cùng solution.
- lập trình win form C++ trên visual 2008
- Phân tích giọng nói rồi chuyển thành chữ. C++ có làm được không?
- default value for non-static class member?
- Hỏi về copy constructor?
-
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]
Ngọc Bích xanh bản chất thuộc dòng đá đá hoa (jade). Và cẩm thạch là tên gọi chung của ngọc bích. Vì thực chất chúng thuộc dòng đá đa khoáng được hình thành từ chất Silicat dưới dạng dioxy. Ngọc bích...
Chia sẻ Vòng tay ngọc bích xanh là...