Outputting addresses instead of dereferenced value
Apr 9, 2021 at 6:29am Apr 9, 2021 at 6:29am UTC
Hi, I'm just trying to write something simple up that will print out a polynomial.
Issue I'm having is, if I print out the list of Term objects within the default constructor for Poly, it'll work properly and print out whatever the dereferenced pointer is pointing at. However, when I try to move it to a void member function, it'll dereference the first Term object properly, and then proceed to print referenced addresses instead of the next Term object. I'm not sure if it's something I did when overloading the ostream operator to accept the Term objects or what, but it's rather frustrating. Was looking to see if someone could point me in the right direction.
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>
#include <list>
#include "poly.h"
#include "term.h"
int main() {
//initialize Poly/Term objects and add second Term object to linked list
Poly p(Term(4, 5));
p.add(Poly(Term(2,3)));
//test the member function method
p.print();
}
term.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef TERM_H
#define TERM_H
#include <iostream>
class Term {
public :
Term();
Term(int ,int );
std::string get_term();
bool is_neg(int );
friend std::ostream &operator <<(std::ostream&, const Term&);
friend class Poly;
private :
int co;
int exp;
};
#endif
term.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <iostream>
#include <string>
#include <list>
#include "term.h"
Term::Term()
{
//blank default constructor
}
Term::Term(int coefficient, int exponent)
{
//Enter coefficient and exponent into private class member variables
co = coefficient;
exp = exponent;
}
/*
std::string Term::get_term()
{
//create temp string to store term as it's being constructed
std::string t;
if(exp != 0)
{
t += std::to_string(co) + "x";
if(exp != 1)
{
t += "^" + std::to_string(exp);
}
} else {
t = std::to_string(co);
}
return t;
}
*/
std::ostream& operator <<(std::ostream& out, const Term& t)
{
if (t.exp != 0)
{
out << std::to_string(t.co) + "x" ;
if (t.exp != 1)
{
out << "^" << std::to_string(t.exp);
}
} else {
out << std::to_string(t.co);
}
return out;
}
bool Term::is_neg(int x)
{
return co < 0;
}
poly.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef POLY_H
#define POLY_H
#include <iostream>
#include <string>
#include <list>
#include "term.h"
class Poly : public Term {
public :
Poly();
Poly(const Term&);
void add(const Poly&);
void print();
private :
std::list<Term> terms;
};
#endif
poly.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <iostream>
#include <string>
#include <list>
#include "poly.h"
#include "term.h"
Poly::Poly()
{
//default (blank constructor)
}
//constructor with Term parameter (begins building the polynomial)
Poly::Poly(const Term& t)
{
terms.push_back(t);
//test print (only way to display term list contents currently)
for (const Term& element : terms)
{
if (is_neg(co))
{
std::cout << element;
} else if (!is_neg(co))
std::cout << element << std::endl;
}
}
void Poly::add(const Poly& t)
{
terms.push_back(t);
}
void Poly::print() //prints an address
{
std::list<Term>::iterator pos = terms.begin();
for (pos = terms.begin(); pos != terms.end(); pos++)
{
if (pos == terms.begin())
{
std::cout << *pos;
} else {
if (!is_neg(co))
{
std::cout << "+" << *pos;
}
}
}
}
Thanks in advance for any help/insights.
Apr 9, 2021 at 10:12am Apr 9, 2021 at 10:12am UTC
std::cout << pos;
(without the '*')
* is the dereferencing operator which when applied to a pointer it derefrences it
If you do not use, you simply get the value of the pointer
Apr 9, 2021 at 10:29am Apr 9, 2021 at 10:29am UTC
@dakotad8218
Your code produces the following output for me.
If you want help then I suggest:
- putting everything in a single file (otherwise you are asking people to download 5 separate files)
- removing all extraneous material and keeping it simple.
Apr 9, 2021 at 10:48am Apr 9, 2021 at 10:48am UTC
The following may solve your immediate problem ... but not your ultimate design problem.
Fundamentally, a polynomial is just an array of coefficients (over a field).
1 2 3 4
void Poly::add(const Poly& t)
{
for ( auto &e : t.terms ) terms.push_back(e);
}
Apr 9, 2021 at 11:24am Apr 9, 2021 at 11:24am UTC
Why is Poly derived from Term?
As a base to start from, consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#include <iostream>
#include <string>
#include <list>
class Term {
public :
Term(int , int );
//std::string get_term() const;
int getco() const { return co; }
int getexp() const { return exp; }
bool is_neg() const ;
friend std::ostream& operator <<(std::ostream&, const Term&);
private :
int co {};
int exp {};
};
class Poly {
public :
Poly(const Term&);
void add(const Poly&);
friend std::ostream& operator <<(std::ostream&, const Poly&);
private :
std::list<Term> terms;
};
Term::Term(int coefficient, int exponent) : co(coefficient), exp(exponent) {}
/*
std::string Term::get_term()
{
//create temp string to store term as it's being constructed
std::string t;
if(exp != 0)
{
t += std::to_string(co) + "x";
if(exp != 1)
{
t += "^" + std::to_string(exp);
}
} else {
t = std::to_string(co);
}
return t;
}
*/
std::ostream& operator <<(std::ostream& out, const Term& t)
{
if (t.exp != 0) {
out << t.co << 'x' ;
if (t.exp != 1) {
out << '^' << t.exp;
}
} else
out << t.co;
return out;
}
bool Term::is_neg() const
{
return co < 0;
}
//constructor with Term parameter (begins building the polynomial)
Poly::Poly(const Term& t)
{
terms.push_back(t);
}
void Poly::add(const Poly& p)
{
for (const auto & t : p.terms)
terms.push_back(t);
}
std::ostream& operator <<(std::ostream& os, const Poly& py)
{
for (auto pos = py.terms.cbegin(); pos != py.terms.cend(); ++pos)
if (pos == py.terms.cbegin())
os << *pos;
else {
if (!(pos->is_neg()))
os << "+" ;
os << *pos;
}
return os;
}
int main()
{
//initialize Poly/Term objects and add second Term object to linked list
Poly p(Term(4, 5));
p.add(Poly(Term(2, 3)));
//test the member function method
std::cout << p << '\n' ;
}
which displays as expected:
Topic archived. No new replies allowed.