Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-18-2011, 11:07 PM   PM User | #1
QuantumFoam
New to the CF scene

 
Join Date: Jun 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
QuantumFoam is an unknown quantity at this point
C++ - Files Not Linking Properly

I should have just started with the files broken up and gone from there, but I thought it would save time to just write them all in one file. I built, compiled, linked, and successfully executed the program. Then I went to split up the program into separate files to be handed in. (its a class project on operator overloading) I created a new project, added blank header file for declarations, and 2 implementation files (1 for the class info - 1 for driver menu). None of the information has changed, but when I go to compile, it breaks.

What have I missed?

My Header:
Code:
//                      PROJECT FILES
//       LIST ALL PROGRAM AND HEADER FILES IN THE PROJECT
// 
// VectorClass.h
// Vector.cpp
//=============================================================================
// 		PROCESS THIS FILE ONLY PER PROJECT
// allows for additional
#ifndef VectorClass2_H					
#define VectorClass2_H
//=============================================================================
//             INCLUDE FILES
#include <iostream>
//
//=============================================================================
//             CONSTANT DEFINITION
// none
//=============================================================================
//		EXTERNAL CLASS VARIABLE					
// none
//=============================================================================
//             FUNCTION PROTOTYPES
// none
//=============================================================================
//              Class Object
//
class Vector {
//=============================================================================
public:
	Vector();
	Vector(double x, double y, double z);
	Vector(const Vector&);
	~Vector();

	// getters
	double getX() const {
		return pVec[0];
	}
	double getY() const {
		return pVec[1];
	}
	double getZ() const {
		return pVec[2];
	}

	// setters
	void setX(double x) {
		pVec[0] = x;
	}
	void setY(double y) {
		pVec[1] = y;
	}
	void setZ(double z) {
		pVec[2] = z;
	}

	// return magnitude of vector
	double magnitude() const {
		return sqrt(pVec[0]*pVec[0] + pVec[1]*pVec[1] + pVec[2]*pVec[2]);
	}

	Vector operator-();
  	bool operator<(const Vector&);
	bool operator==(const Vector&);
	Vector &operator=(const Vector &rhs);


private:
	const size_t VDIMENSION;
	double *pVec;
	void doCopy(const Vector&);

	friend double operator*(const Vector&, const Vector&);
	friend Vector operator*(const Vector&, double);
	friend Vector operator*(double, const Vector&);
	friend Vector operator+(const Vector&, const Vector&);
	friend Vector operator+(const Vector&, double);
	friend Vector operator+(double, const Vector&);
	friend Vector operator-(const Vector&, const Vector&);
	friend Vector operator-(const Vector&, double);
	friend Vector operator-(double, const Vector&);
	friend Vector operator^(const Vector&, const Vector&);
	friend ostream& operator<<(ostream&, const Vector&);
};

//=============================================================================
//            	END OF CONDITIONAL BLOCK
#endif
//=============================================================================
//             END OF HEADER FILE
//=============================================================================
Pretty straight-forward stuff.

Class Implementation:
Code:
//=============================================================================
// 				INCLUDE FILES
#include "VectorClass2.h"
#include <iostream>
using namespace std;
/*
 * default constructor
 * allocate memory for the three coordinates and initialize them to 0
 */
Vector::Vector()
: VDIMENSION(3) {
	pVec = new double[VDIMENSION];
	for (size_t i = 0; i < VDIMENSION; i++)
		pVec[i] = 0;
}

/*
 * parameterized constructor
 * allocate memory for the three coordinates and initialize them to the parameters
 */
Vector::Vector(double x, double y, double z)
: VDIMENSION(3) {
	pVec = new double[VDIMENSION];
	pVec[0] = x;
	pVec[1] = y;
	pVec[2] = z;
}

/*
 * copy constructor
 * performs deep copy
 */
Vector::Vector(const Vector &rhs)
: VDIMENSION(3) {
	pVec = new double[VDIMENSION];

	doCopy(rhs);
}

/*
 * overloaded assignment operator
 * performs deep copy assignment
 */
Vector &Vector::operator=(const Vector &rhs) {
	// check for self assignment
	if (this != &rhs)
		doCopy(rhs);

	return *this;
}

/*
 * does the actual copying of rhs to *this object
 */
void Vector::doCopy(const Vector &rhs) {

	// assign coordinates
	for (size_t i = 0; i < VDIMENSION; i++)
		pVec[i] = rhs.pVec[i];
}

/*
 * destructor
 * deallocate memory
 */
Vector::~Vector() {
	delete [] pVec;
}

/*
 * vector dot product
 */
double operator*(const Vector& a, const Vector& b) {

	return (a.getX() * b.getX() + a.getY() * b.getY() + a.getZ() * b.getZ());
}

/*
 * scalar multiplication
 */
Vector operator*(double a, const Vector& b) {
	Vector result;

	result.setX(a * b.getX());
	result.setY(a * b.getY());
	result.setZ(a * b.getZ());

	return result;
}

/*
 * scalar multiplication
 */
Vector operator*(const Vector& a, double b) {
	Vector result;

	result.setX(a.getX() * b);
	result.setY(a.getY() * b);
	result.setZ(a.getZ() * b);

	return result;
}

/*
 * vector addition
 */
Vector operator+(const Vector& a, const Vector& b) {
	Vector result;

	result.setX(a.getX() + b.getX());
	result.setY(a.getY() + b.getY());
	result.setZ(a.getZ() + b.getZ());

	return result;
}

/*
 * scalar addition
 */
Vector operator+(double a, const Vector& b) {
	Vector result;

	result.setX(a + b.getX());
	result.setY(a + b.getY());
	result.setZ(a + b.getZ());

	return result;
}

/*
 * scalar addition
 */
Vector operator+(const Vector& a, double b) {
	Vector result;

	result.setX(a.getX() + b);
	result.setY(a.getY() + b);
	result.setZ(a.getZ() + b);

	return result;
}

/*
 * vector substraction
 */
Vector operator-(const Vector& a, const Vector& b) {
	Vector result;

	result.setX(a.getX() - b.getX());
	result.setY(a.getY() - b.getY());
	result.setZ(a.getZ() - b.getZ());

	return result;
}


/*
 * scalar substraction
 */
Vector operator-(double a, const Vector& b) {
	Vector result;

	result.setX(a - b.getX());
	result.setY(a - b.getY());
	result.setZ(a - b.getZ());

	return result;
}

/*
 * scalar substraction
 */
Vector operator-(const Vector& a, double b) {
	Vector result;

	result.setX(a.getX() - b);
	result.setY(a.getY() - b);
	result.setZ(a.getZ() - b);

	return result;
}

/*
 * vector unary negation
 */
Vector Vector::operator-() {
	Vector result;

	result.setX(-getX());
	result.setY(-getY());
	result.setZ(-getZ());

	return result;
}

/*
 * scalar division
 */
Vector operator/(const Vector& a, double b) {
	Vector result;

	if (b != 0) {
		result.setX(a.getX() / b);
		result.setY(a.getY() / b);
		result.setZ(a.getZ() / b);
	}

	return result;
}

/*
 * vector cross product
 */
Vector operator^(const Vector& a, const Vector& b) {
	Vector result;

	result.setX(a.getY() * b.getZ() - a.getZ() * b.getY());
	result.setY(a.getZ() * b.getX() - a.getX() * b.getZ());
	result.setZ(a.getX() * b.getY() - a.getY() * b.getX());

	return result;
}

/*
 * vector equality
 */
bool Vector::operator==(const Vector &rhs) {
	return (getX() == rhs.getX() && getY() == rhs.getY() && getZ() == rhs.getZ());
}

/*
 * vector less than
 */
bool Vector::operator<(const Vector &rhs) {
	return (magnitude() < rhs.magnitude());
}

/*
 * overloaded insertion operator <<
 */
ostream& operator<<(ostream& os, const Vector& v) {
	os << "[ " << v.getX() << ", " << v.getY() << ", " << v.getZ() << " ]";
	return os;
}
And the Menu Driver:
Code:
// 				INCLUDE FILES
#include <iostream>
#include <iomanip>
#include "VectorClass2.h"
using namespace std;



int main() {
	// 1. Declare three vector objects A, B, C with the  values [1.0,0.0,0.0] , [0.0,1.0,0.0] and [0.0,0.0,1.0] respectively. 
	Vector A(1.0, 0.0, 0.0),
		   B(0.0, 1.0, 0.0),
		   C(0.0, 0.0, 1.0);

	cout.precision(3);
	cout << fixed;
    cout << "1. Three Vectors: A, B, & C." << endl;

	// 2. Display the three vectors.
	cout << "2.  A = " << A << " = " << A.magnitude() << endl;
	cout << "    B = " << B << " = " << B.magnitude() << endl;
	cout << "    C = " << C << " = " << C.magnitude() << endl;

	// 3. Display the dot product of A and B. 
	cout << "3.  A * B = " << A * B << endl;
	
	// 4. Display the cross product of A and B.
	Vector crossAB = A ^ B;
	cout << "4.  A ^ B = " << crossAB << " = " << crossAB.magnitude() << endl;

	// 5. Display the cross product of B and A (The cross product is not commutative,
	//    you should get a different answer than you got for test 4.) 
	Vector crossBA = B ^ A;
	cout << "5.  B ^ A = " << crossBA << " = " << crossBA.magnitude() << endl;

	// 6. Perform the assignment statement, A = A * 3.27; and then display the value and magnitude of vector, A. 
	A = A * 3.27;
	cout << "6.  A = " << A << " = " << A.magnitude() << endl;

	// 7. Perform the assignment statement, B = 4.5 + B; and then display the value and magnitude of vector, B. 
	B = 4.5 + B;
	cout << "7.  B = " << B << " = " << B.magnitude() << endl;

	// 8. Perform the assignment statement, C = C – 1.36; and then display the value and magnitude of vector, C. 
	C = C - 1.36;
	cout << "8.  C = " << C << " = " << C.magnitude() << endl;

	// 9. Declare vector, D(A – B), and display D. 
	Vector D(A - B);
	cout << "9.  D = " << D << " = " << D.magnitude() << endl;

	// 10. Display A and B to ensure that their values didn’t change. 
	cout << "10. A = " << A << " = " << A.magnitude() << endl;
	cout << "    B = " << B << " = " << B.magnitude() << endl;

	// 11. Declare vector, E(B + C), and display E.
	Vector E(B + C);
	cout << "11. E = " << E << " = " << E.magnitude() << endl;

	// 12. Display B and C to ensure that their values didn’t change. 
	cout << "12. B = " << B << " = " << B.magnitude() << endl;
	cout << "    C = " << C << " = " << C.magnitude() << endl;

	// 13. Display the dot product of D and E. 
	cout << "13. D * E = " << D * E << endl;

	// 14. Display D and E to ensure that their values didn’t change. 
	cout << "14. D = " << D << " = " << D.magnitude() << endl;
	cout << "    E = " << E << " = " << E.magnitude() << endl;

	// 15. Display the cross product of D and E. 
	Vector crossDE = D ^ E;
	cout << "15. D ^ E = " << crossDE << " = " << crossDE.magnitude() << endl;
	
	// 16. Display D and E to ensure that their values didn’t change. 
	cout << "16. D = " << D << " = " << D.magnitude() << endl;
	cout << "    E = " << E << " = " << E.magnitude() << endl;

	// 17. Perform the unary negation operator on vector E and then display it 
	Vector negE = -E;
	cout << "17. -E = " << negE << " = " << negE.magnitude() << endl;

	// 18. A Unit vector is a vector that points in the same direction as the original vector,
	// but whose magnitude is unity (1). It is calculated by dividing the vector by its magnitude
	// (e.g., VectorA/VectorA.mag()).  Calculate and display the Unit vector for E. 
	Vector unitE = E / E.magnitude();
	cout << "18. Unit vector of E = " << unitE << " = " << unitE.magnitude() << endl;

	// 19. Display the result (i.e., true or false – NOT 1 or 0) of the operation, A = = B. 
	cout << "19. A == B = " << ((A == B)? "true": "false") << endl;

	// 20. Display the result (i.e., true or false – NOT 1 or 0) of the operation,  D < E.
	cout << "20. D < E = " << ((D < E)? "true": "false") << endl;

	// 21. Perform the cascaded assignment A = B = C = Vector(1.5, 2.5, 3.5); and then display all of 
	// these vector objects to verify they are indeed equal.
	A = B = C = Vector(1.5, 2.5, 3.5);
	cout << "21. A = " << A << " = " << A.magnitude() << endl;
	cout << "    B = " << B << " = " << B.magnitude() << endl;
	cout << "    C = " << C << " = " << C.magnitude() << endl;

	return(0);
}

Did I break it up in the wrong places? Did I forget to include something? Please help me figure this out.
QuantumFoam is offline   Reply With Quote
Reply

Bookmarks

Tags
c++, link, operator, overload, vector

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:51 PM.


Advertisement
Log in to turn off these ads.