I'm new to C++ and SO to I apologize in advance if everything isn't up to snuff.
Below are the first few lines of my main function. I get an error on the 6th line: 'missing template arguments before c1'. As you can see further below the class 'complex' has to double data members which should both be initialized to 0 with the default constructor. entering complex fixed this error but I don't believe I should need a template in the first place. Another issue is that after I make this change, the compiler does not recognize the first member function that I use c1.get_real()
which I suspect is due to the addition of the template argument.
#include <fstream> // For ofstream class declaration
#include <iostream> // For cout, endl
#include <Complex> // So we can access the complex class declaration (hint: see Complex.cpp)
#include <string>
int main()
{
string filename; filename = "complex-test.txt";
ofstream fout(filename.c_str());
fout<<"Defining complex object c1 and calling default ctor"<<endl;
complex c1;
fout<<"Testing accessors and that default ctor initialized c1 to 0 + 0i"<<endl;
fout<<"c1.get_real() returned "<<c1.get_real()<<endl;
My Complex.cpp file which implements my class member functions:
#include <iomanip> // For fixed, setprecision()
#include <sstream> // For stringstream class
#include "Complex.hpp" // For the complex class declaration
using namespace std;
//--------------------------------------------------------------------------------------------------
// + complex() :
// PSEUDOCODE
// Call init() passing 0 and 0 as the arguments.
//--------------------------------------------------------------------------------------------------
complex::complex()
{
init(0,0);
}
complex::complex(double init_real, double init_imag)
{
init(init_real, init_imag);
}
double complex::get_imag()
{
return m_imag;
}
double complex::get_real()
{
return m_real;
}
void complex::init(double init_real, double init_imag)
{
m_real = init_real;
m_imag = init_imag;
}
void complex::set_imag(double s)
{
m_imag = s;
}
void complex::set_real(double s)
{
m_real = s;
}
//--------------------------------------------------------------------------------------------------
// + to_string() : string
//
// DESCRIPTION
// Returns a string representation of the complex object. For example, if m_real is -2.3333333
// and m_imag is -21.456789123, this function will return the string "(-2.3333 - 21.4568i)".
//
//--------------------------------------------------------------------------------------------------
string complex::to_string()
{
stringstream sout;
sout << fixed << setprecision(4);
sout << "(" << get_real();
double imag = get_imag();
if (imag < 0) {
sout << " - " << -imag << 'i';
} else if (imag > 0) {
sout << " + " << imag << 'i';
}
sout << ")";
return sout.str();
}
Here is the header file for the class:
#ifndef COMPLEX_HPP // See the comments in the header comment block regarding these two lines.
#define COMPLEX_HPP
#include <string> // Included because we are using the string class in to_string()
using namespace std;
class complex
// Write the public section of the class declaration.
{
public:
complex();
complex(double , double);
double get_imag();
double get_real();
void set_imag(double);
void set_real(double);
string to_string();
private:
void init(double, double);
double m_real;
double m_imag;
};
#endif