Question
I'm almost done with my code for c++. The only problem is that I'm getting an error statement that I'm not sure what it is.
I'm almost done with my code for c++. The only problem is that I'm getting an error statement that I'm not sure what it is.
MyComplex.cpp
February 25, 2018
This is the main file for the MyComplex program.
*/
#include
#include
#include
#include
#include
#include
#include "MyComplex.h"
using namespace std;
// A freebe
string MyComplex::getAsString() const{
stringstream s;
s << "(" << real << " + " << imaginary << "i)";
return s.str();
}
MyComplex MyComplex:: operator-(const MyComplex &right) const{
MyComplex difference;
difference.real = real-right.real;
difference.imaginary = imaginary - right.imaginary;
return difference;
}
MyComplex MyComplex:: operator*(const MyComplex &right) const{
MyComplex expression;
expression.real = real * right.real;
expression.imaginary = imaginary * right.imaginary;
return expression;
}
MyComplex MyComplex::operator/(const MyComplex &right) const{
MyComplex quotient;
quotient.real = real / right.real;
quotient.imaginary = imaginary / right.imaginary;
return quotient;
}
bool MyComplex:: operator==(const MyComplex &right)const{
if(real==right.real){
cout< } else{ cout< } if(imaginary==right.imaginary){ cout< } else{ cout< } } double MyComplex:: mag2() const{ double number; number=(real*real) -(imaginary*imaginary); return number; } double MyComplex:: dist2(const MyComplex &right) const{ double total; // int a; //int b; double r = real -right.real; double i = imaginary - right.imaginary; total = sqrt(r*r + i*i); return total; } void MyComplex::setReal(double real){ this -> real = real; } void MyComplex::setImaginary(double imaginary){ this -> imaginary = imaginary; } double MyComplex::getReal() const{ return real; } double MyComplex::getImaginary() const{ return imaginary; } #ifndef MYCOMPLEX_H #define MYCOMPLEX_H /* MyComplex.h 25 February 2018 This is the h file for MyComplex. */ #include // There is no need to change this! using namespace std; class MyComplex{ public: MyComplex(double, double); MyComplex operator+(const MyComplex &) const; MyComplex operator-(const MyComplex &) const; MyComplex operator*(const MyComplex &) const; MyComplex operator/(const MyComplex &) const; bool operator==(const MyComplex &) const; string getAsString() const; double mag2() const; double dist2(const MyComplex &) const; double getReal() const; double getImaginary() const; void setReal(double); void setImaginary(double); private: MyComplex(); double real; double imaginary; }; #endif /* sandbox.cpp February 25, 2018 Sandbox for MyComplex */ #include #include "MyComplex.h" using namespace std; int main(int argc, char* argv[]){ // Use this program to test your MyComplex implementation! MyComplex a(5,6); cout << "a: " << a.getAsString() << endl; MyComplex b(8,-2); cout << "b: " << b.getAsString() << endl; cout << "a + b: " << (a + b).getAsString() << endl; cout << "a - b: " << (a - b).getAsString()<< endl; cout << "a * b: " << (a * b).getAsString() << endl; cout << "a / b: " << (a / b).getAsString() << endl; cout << "Distance squared: " << a.dist2(b) << endl; cout << "a's magnitude squared: " << a.mag2() << endl; cout << "a == b? " << (a == b) << endl; cout << "a real: " << a.getReal() << endl; cout << "a imag: " << a.getImaginary() << endl; a.setReal(10); a.setImaginary(99); cout << "changed a: " << a.getAsString() << endl; return 0; } Error g++ MyComplex.cpp MyComplex-sandbox.cpp /tmp/ccco0wjg.o: In function `MyComplex::operator-(MyComplex const&) const': MyComplex.cpp:(.text+0x127): undefined reference to `MyComplex::MyComplex()' /tmp/ccco0wjg.o: In function `MyComplex::operator*(MyComplex const&) const': MyComplex.cpp:(.text+0x1a3): undefined reference to `MyComplex::MyComplex()' /tmp/ccco0wjg.o: In function `MyComplex::operator/(MyComplex const&) const': MyComplex.cpp:(.text+0x21f): undefined reference to `MyComplex::MyComplex()' /tmp/cc9yvnZ2.o: In function `main': MyComplex-sandbox.cpp:(.text+0x3e): undefined reference to `MyComplex::MyComplex(double, double)' MyComplex-sandbox.cpp:(.text+0xba): undefined reference to `MyComplex::MyComplex(double, double)' MyComplex-sandbox.cpp:(.text+0x117): undefined reference to `MyComplex::operator+(MyComplex const&) const' collect2: error: ld returned 1 exit status
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started