How to make public members of a class private? - c++

I'm brand new to programming (in general) and C++ (in particular). I'm trying to take the following public member variables and make them private:
int *coeff;
int order;
Unfortunately, I'm seeing the following errors:
'Poly::coeff' : cannot access private member declared in class 'Poly'
and
'Poly::order' : cannot access private member declared in class 'Poly'
Here's my code:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
class Poly
{
public:
int *coeff;
int order;
int getData();
int display(int *coeff, int order);
void addition(Poly P1, Poly P2);
void subtraction (Poly P1, Poly P2);
void multiplication (Poly P1, Poly P2);
// ~Poly();
};
int Poly::display(int *coeff, int order)
{
int i;
int j;
for (i = order; i >= 0; i--)
{
cout << coeff[i] << "x^" << i;
if ((i - 1) != -1)
{
cout << "+";
}
}
cout << "\n";
return 0;
}
int Poly::getData()
{
int i;
cout << "Please enter the order of the polynomial: ";
cin >> order;
coeff = new int[order + 1];
for (i = order; i >= 0; i--)
{
cout << "Please enter the coefficient of x^" << i << " :";
cin >> coeff[i];
}
return 0;
}
void Poly::addition(Poly P1, Poly P2)
{
int max;
int i;
max = (P1.order > P2.order) ? P1.order : P2.order;
int *add = new int[max + 1];
if (P1.order == P2.order)
{
for (i = P1.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
if (P1.order > P2.order)
{
for (i = P1.order; i > P2.order; i--)
{
add[i] = P1.coeff[i];
}
for (i = P2.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
if (P1.order < P2.order)
{
for (i = P2.order; i > P1.order; i--)
{
add[i] = P2.coeff[i];
}
for (i = P1.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
cout << "\nAddition:";
display(add, max);
cout << "\n";
}
void Poly::subtraction(Poly P1, Poly P2)
{
int max;
int i;
max = (P1.order > P2.order) ? P1.order : P2.order;
int *sub = new int[max + 1];
if (P1.order == P2.order)
{
for (i = P1.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
if (P1.order > P2.order)
{
for (i = P1.order; i > P2.order; i--)
{
sub[i] = P1.coeff[i];
}
for (i = P2.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
if (P1.order < P2.order)
{
for (i = P2.order; i > P1.order; i--)
{
sub[i] = -P2.coeff[i];
}
for (i = P1.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
cout << "\nSubtraction:";
display(sub, max);
cout << "\n";
}
void Poly::multiplication(Poly P1, Poly P2)
{
int i;
int j;
int max;
max = P1.order + P2.order;
int *mult = new int[max + 1];
for (i = P1.order; i >= 0; i--)
for (j = P2.order; j >= 0; j--)
{
mult[i + j] += P1.coeff[i] * P2.coeff[i];
}
cout << "\nMultiplication:";
display(mult, max);
}
int main()
{
int choice;
Poly P1, P2, P3;
cout << "-------- Instructions --------" << endl;
cout << "For polynomial 1... " << endl;
P1.getData();
cout << endl;
cout << "For polynomial 2... " << endl;
P2.getData();
while (1)
{
cout << "\n******** Menu Selection ********" << endl;
cout << "1: Addition\n2: Subtraction\n3: Mutiplication\n0: Exit" << endl;
cout << "Please enter your choice (1, 2, 3 or 0):";
cin >> choice;
switch (choice)
{
case 1:
cout << "\n-------- Addition --------\n";
cout << "Polynomial 1: ";
P1.display(P1.coeff, P1.order);
cout << "Polynomial 2: ";
P2.display(P2.coeff, P2.order);
P3.addition(P1, P2);
cout << "--------------------------\n";
break;
case 2:
cout << "\n------ Subtraction ------\n";
cout << "Polynomial 1: ";
P1.display(P1.coeff, P1.order);
cout << "Polynomial 2: ";
P2.display(P2.coeff, P2.order);
P3.subtraction(P1, P2);
cout << "--------------------------\n";
break;
case 3:
cout << "\n------ Subtraction ------\n";
cout << "Polynomial 1: ";
P1.display(P1.coeff, P1.order);
cout << "Polynomial 2: ";
P2.display(P2.coeff, P2.order);
P3.multiplication(P1, P2);
cout << "--------------------------\n";
break;
case 0:
cout << "The program will now terminate. Thank you." << endl;
exit(0);
default:
cout << endl;
cout << "You have entered an invalid selection." << endl;
cout << "Please enter a positive integer between 0 and 3.";
cout << endl;
}
}
return 0;
}
Can anyone offer guidance on how to best modify my code so that int *coeff and int order are private members?
Thanks very much in advance.
-Ryan

The idea of making something private means you're not allowed to access outside of member or friend functions (that's the purpose of private). The code is failing in main at things like P1.display(P1.coeff, P1.order) because main isn't a member of the Poly class and, as such, is not allowed to access Poly::coeff or Poly::order (as the compiler is telling you).
I would suggest you change the definition of Poly::display(int *coeff, int order) to not require you pass in coeff and order, since the instance already knows what these values are. I would also take out the return 0, since it isn't buying you anything.
void Poly::display()
{
int i;
int j;
for (i = order; i >= 0; i--)
{
cout << coeff[i] << "x^" << i;
if ((i - 1) != -1)
{
cout << "+";
}
}
cout << "\n";
}
Inside that member function, the variables order and coeff are implicitly picking up the members of your class that were previously shadowed by the parameters you were passing.

"Can anyone offer guidance on how to best modify my code so that int *coeff and int order are private members?"
Your code in main()
P1.display(P1.coeff, P1.order);
still tries to access these now private class members directly, which won't work of course (that's why we make these members private).
For your case, you don't need to pass these variables as parameters to that function at all, since the actual values are available for the P1 instance of class Poly already.
Change your member function signature to
int display();
and definition to
int Poly::display() {
int i;
int j;
for (i = order; i >= 0; i--) {
cout << coeff[i] << "x^" << i;
if ((i - 1) != -1) {
cout << "+";
}
}
cout << "\n";
return 0;
}
Note that accessing order or coeff inside the Poly::display() member function, can be read as equivalent to this->order and this->coeff.
Class member functions can be considered to have a this pointer implicitly, and can access (private) class member variables directly, without need to specify these as parameters.
After changing the stuff as proposed above, you can just call
P1.display();
Change other signatures/access to coeff, order analogous.
NOTE:
There are more flaws in your class declarations. Something like
void addition(Poly P1, Poly P2);
won't work well. You want to operate with the actual instance as the left hand value, and the other as a constant right hand value. IMHO your member function should look like this
Poly& addition(const Poly& rhs) {
// perform addition operations with this and rhs
return *this;
}
Poly addition(const Poly& rhs) const {
Poly result = *this;
// perform addition operations with result and rhs
return result;
}

Related

Multidimensional array not changing elements from function for some reason

The function player_attack() changes the elements of the multi-dimensional array pc_board but when i reprint it in main, the array prints unchanged.
I removed all unnecessary code.
I tried to pass is at as a parameter to the function but i got an error for using a multidimensional array in the parameter.
$
bool game_won = false;
string board[5][5];
string pc_board[5][5];
void initialize_player_board() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
board[i][j] = "-";
}
}
}
void print_map() {
for (int i = 0; i < 5; i++) {
cout << setw(5);
cout << i << setw(5);
for (int j = 0; j < 5; j++) {
cout << board[i][j] << setw(5);
}
cout << setw(10);
for (int j = 0; j < 5; j++) {
cout << pc_board[i][j] << setw(5);
}
cout << endl;
}
}
void pc_add_battleship() {
int x = 0;
int y = 0;
int choice_generator = 0;
char choice;
x = rand() % 4 + 1;
y = rand() % 4 + 1;
choice_generator = rand() % 2;
if (choice_generator == 0) {
choice = 'h';
}
else {
choice = 'v';
}
if (choice == 'h') {
pc_board[y - 1][x] = 'O';
pc_board[y][x] = 'O';
pc_board[y + 1][x] = 'O';
}
if (choice == 'v') {
pc_board[y][x - 1] = 'O';
pc_board[y][x] = 'O';
pc_board[y][x + 1] = 'O';
}
}
void player_attack() {
int x = 0;
int y = 0;
cout << "Choose an x coordinate to attack: " << endl;
cin >> x;
cout << "Choose a y coordinate to attack: " << endl;
cin >> y;
if (pc_board[y][x] == "O") {
cout << "HIT!" << endl;
pc_board[y][x] == "H";
}
else {
cout << "Miss." << endl;
pc_board[y][x] == "M";
}
}
int main()
{
srand(time(0));
initialize_player_board();
initialize_pc_board();
cout << "Welcome to the battleship game." << endl;
print_map();
Add_battleship();
pc_add_battleship();
while (!game_won) {
print_map();
player_attack();
}
return 0;
}
$
I expected for the multidimensional array to change its elements due to the function
In your function player_attack you use wrong operator:
if (pc_board[y][x] == "O") {
cout << "HIT!" << endl;
pc_board[y][x] == "H"; // here
}
else {
cout << "Miss." << endl;
pc_board[y][x] == "M"; // and here
}
instead of == that is a comparison operator you should use = that is the assignment operator.
Using operator == in this context is still valid C++ syntax, which produces a boolean value, however it does not modify the arguments (which are left and right side of comparison), which is probably what you want to do in most cases. Enabling compiler flags like -Wall or Wextra along with Werror helps to avoid this kind of bugs.

power function no scope in function

#include<iostream>//Pls note:Only header allowed...
As this is the C++ i dont think so any other header is needed for that math function.
using namespace std;
int comparator(int audience[][2], int index1, int index2) {
int b1, e1;
int b2, e2;
b1 = audience[index1][1];
e1 = audience[index1][2];
b2 = audience[index2][1];
e2 = audience[index2][2];
double re1;
re1 = pow(b1, e1);
cout << re1 << endl;
double re2 = pow(b2, e2);
cout << re2 << endl;
if (re1 == re2)
{
return 0;
}
if (re1 > re2)
{
return -1;
}
if (re1 < re2)
{
return 1;
}
}
//Nothing has to be done with the rest of the two functions.
void sorting(int audience[][2], int N, int &i_index, int &j_index)
{
int i, j, temp;
for (i = 0; i<N - 1; i++)
{
if (audience[i][2] < audience[i + 1][2])
continue;
else
i_index = i;
break;
}
for (i = N; i > 1; i++)
{
if (audience[i][2]>audience[i - 1][2])
continue;
else
j_index = i;
break;
}
for (i = i_index + 1; i < j_index - 1; i++)
{
min = audience[i_index + 1][2];
for (i = )
if (audience[i_index][1] > audience[i_index + 1][1])
{
temp = audience[i_index + 1][1];
audience[i_index + 1][1] = audience[i_index][1];
audience[i_index][1] = temp;
}
}
for (i = i_index + 1; i <= j_index - 1; i++)
{
min = audience[i][2];
for (j = i_index + 2; j <= j_index - 1; j++)
{
if (min > audience[j][2])
{
temp = audience[i_index + 2][2];
audience[i_index + 1][2] = audience[i_index][2];
audience[i_index][2] = temp;
}
}
}
}
void merge(int audience[][2], int mergedarray[][2], int N, int i_index, int j_index)
{
}
int main()
{
int audience[100][2], mergedmarks[100][2];
int i, N;
int index1 = 0, index2 = 0;
int comp_result;
cout << "Enter the value of N : ";
cin >> N; // Enter size of the table
cout << "Enter the base and exponent for " << N << "rows " << endl;
for (i = 0; i < N; i++)
cin >> audience[i][0] >> audience[i][1]; //Enter numbers in the table
cout << endl << "Checking Function 1: Compare ecodes for 5 index pairs" << endl;
for (i = 0; i < 5; i++)
{
cout << "\nEnter indices of row1 and row2 that you want to compare: ";
cin >> index1 >> index2;
if (index1 < 0 || index2 < 0 || index1 >= N || index2 >= N)
continue;
comp_result = comparator(audience, index1, index2);
if (comp_result == -1)
cout << "ecode of index 1 is greater than ecode of index2" << endl;
else if (comp_result == 1)
cout << "ecode of index 1 is less than ecode of index2" << endl;
else if (comp_result == 0)
cout << "ecode of index 1 is equal to ecode of index2" << endl;
}
cout << endl;
int index_i = 0, index_j = N;
sorting(audience, N, index_i, index_j);
cout << "Checking Function 2: Printing sorted array " << endl;
for (i = 0; i < N; i++)
cout << audience[i][0] << " " << audience[i][1] << endl;
cout << endl;
cout << "index i: " << index_i << "\nindex j: " << index_j << endl;
cout << endl << "Checking Function 3: Printing Merged Array " << endl;
merge(audience, mergedmarks, N, index_i, index_j);
int merge_array_size = index_i + (N - (index_j + 1));
for (i = 0; i < N; i++)
cout << mergedmarks[i][0] << " " << mergedmarks[i][1] << endl;
cout << endl;
return 0;
}
This is the whole problem. I have to still edit the merge function. That is the whole problem.This is all.
You need to include the pow header, which is math.h, in order to use it.
add at the beginning of your file:
#include <math.h>
#include <iostream>
using namespace std;
POW is declared in math.h header file so use
#include<math.h>

C++ error: "no instance of function template matches the required type"

I'm a C++ newbie and am writing a class to add, subtract and multiply polynomials. I'm seeing this error:
"no instance of function template matches the required type"
resulting from these statements:
display(add, count);
display(sub, count);
display(mult, count);
P1.display(add, count); (as well as for sub and mult)
P2.display(add, count); (as well as for sub and mult)
My code is:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
class Poly
{
private:
int order; // the order of the polynomial
int *coeff; // pointer to an array of coefficients
// size of the coefficient array is predicated on [order + 1]
int *add;
int *sub;
int *mult;
public:
// Poly(); // the default constructor
int setOrderAndCoeff(); // sets the order and coefficients
int display(int *data, int count); // displays the resutling polynomial
void addition(Poly P1, Poly P2); // adds 2 polynomials
void subtraction (Poly P1, Poly P2); // subtracts 2 polynomials
void multiplication (Poly P1, Poly P2); // multiplies 2 polynomials
// ~Poly(); // the destructor
};
int Poly::display(int *data, int count)
{
for (int i = count; i >= 0; i--)
{
cout << data[i] << "x^" << i;
if ((i - 1) != -1)
{
cout << "+";
}
}
cout << "\n";
return 0;
}
int Poly::setOrderAndCoeff()
{
int i;
cout << "Please enter the order of the polynomial: ";
cin >> order;
coeff = new int[order + 1];
for (i = order; i >= 0; i--)
{
cout << "Please enter the coefficient of x^" << i << " :";
cin >> coeff[i];
}
return 0;
}
void Poly::addition(Poly P1, Poly P2)
{
int max;
int i;
max = (P1.order > P2.order) ? P1.order : P2.order;
add = new int [max + 1];
if (P1.order == P2.order)
{
for (i = P1.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
if (P1.order > P2.order)
{
for (i = P1.order; i > P2.order; i--)
{
add[i] = P1.coeff[i];
}
for (i = P2.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
if (P1.order < P2.order)
{
for (i = P2.order; i > P1.order; i--)
{
add[i] = P2.coeff[i];
}
for (i = P1.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
cout << "\nAddition:";
display(add, count);
cout << "\n";
}
void Poly::subtraction(Poly P1, Poly P2)
{
int max;
int i;
max = (P1.order > P2.order) ? P1.order : P2.order;
int *sub = new int[max + 1];
if (P1.order == P2.order)
{
for (i = P1.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
if (P1.order > P2.order)
{
for (i = P1.order; i > P2.order; i--)
{
sub[i] = P1.coeff[i];
}
for (i = P2.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
if (P1.order < P2.order)
{
for (i = P2.order; i > P1.order; i--)
{
sub[i] = -P2.coeff[i];
}
for (i = P1.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
cout << "\nSubtraction:";
display(sub, count);
cout << "\n";
}
void Poly::multiplication(Poly P1, Poly P2)
{
int i;
int j;
int max;
max = P1.order + P2.order;
int *mult = new int[max + 1];
for (i = P1.order; i >= 0; i--)
for (j = P2.order; j >= 0; j--)
{
mult[i + j] += P1.coeff[i] * P2.coeff[i];
}
cout << "\nMultiplication:";
display(mult, count);
}
int main()
{
int choice;
Poly P1, P2, P3;
cout << "-------- Instructions --------" << endl;
cout << "For polynomial 1... " << endl;
P1.setOrderAndCoeff();
cout << endl;
cout << "For polynomial 2... " << endl;
P2.setOrderAndCoeff();
while (1)
{
cout << "\n******** Menu Selection ********" << endl;
cout << "1: Addition\n2: Subtraction\n3: Mutiplication\n0: Exit" << endl;
cout << "Please enter your choice (1, 2, 3 or 0):";
cin >> choice;
switch (choice)
{
case 1:
cout << "\n-------- Addition --------\n";
cout << "Polynomial 1: ";
P1.display(add, count);
cout << "Polynomial 2: ";
P2.display(add, count);
P3.addition(P1, P2);
cout << "--------------------------\n";
break;
case 2:
cout << "\n-------- Subtraction --------\n";
cout << "Polynomial 1: ";
P1.display(sub, count);
cout << "Polynomial 2: ";
P2.display(sub, count);
P3.subtraction(P1, P2);
cout << "--------------------------\n";
break;
case 3:
cout << "\n-------- Multiplication --------\n";
cout << "Polynomial 1: ";
P1.display(mult, count);
cout << "Polynomial 2: ";
P2.display(mult, count);
P3.multiplication(P1, P2);
cout << "--------------------------\n";
break;
case 0:
cout << "The program will now terminate. Thank you." << endl;
exit(0);
default:
cout << endl;
cout << "You have entered an invalid selection." << endl;
cout << "Please enter a positive integer between 0 and 3.";
cout << endl;
}
}
return 0;
Does anyone know what's causing this?
Thanks for any guidance you can give?
-Ryan
In this code snippet
switch (choice)
{
case 1:
cout << "\n-------- Addition --------\n";
cout << "Polynomial 1: ";
P1.display(add, count);
cout << "Polynomial 2: ";
P2.display(add, count);
P3.addition(P1, P2);
cout << "--------------------------\n";
break;
case 2:
cout << "\n-------- Subtraction --------\n";
cout << "Polynomial 1: ";
P1.display(sub, count);
cout << "Polynomial 2: ";
P2.display(sub, count);
P3.subtraction(P1, P2);
cout << "--------------------------\n";
break;
case 3:
cout << "\n-------- Multiplication --------\n";
cout << "Polynomial 1: ";
P1.display(mult, count);
cout << "Polynomial 2: ";
P2.display(mult, count);
P3.multiplication(P1, P2);
cout << "--------------------------\n";
break;
case 0:
cout << "The program will now terminate. Thank you." << endl;
exit(0);
default:
cout << endl;
cout << "You have entered an invalid selection." << endl;
cout << "Please enter a positive integer between 0 and 3.";
cout << endl;
}
in all calls of function display neither the first nor the second argument is defined. For example
where is there defined add and count in main?
P1.display(add, count);
The function is defined as having two parameters
int Poly::display(int *data, int count)
If you want to pass as the first argument for example data member add then you should write at least
P1.display( P1.add, count);
making data member add public.
However I do not see where the second argument with name count is defined.
For example in member function void Poly::addition(Poly P1, Poly P2); count can be defined as an expression max + 1 But in the main you call display outside other member functions of the class. So I can conclude that the code is in general wrong.
It's not a great error message, certainly.
The issue is that when you call Poly::display() from main(), you're passing the names of the members of Poly - add, mult and count - which aren't visible from the main method as they're declared as private within the class.
To resolve this, you would need to either:
Define a method which displays the members you need without having to pass a reference to them from outside the class (for example displayMult(), etc)
move the main loop you currently have within main() somewhere where those fields are visible - either into a member of Poly or a friend function of Poly
Add public getters to retrieve the values of those member variables to Poly
int setOrderAndCoeff()
the member method would return an integer. however, in your main function,
int choice;
Poly P1, P2, P3;
cout << "-------- Instructions --------" << endl;
cout << "For polynomial 1... " << endl;
P1.setOrderAndCoeff(); '<= this is valid as long as there exists a setOrderAndCoeff overloading, return type is void.
i think one way to fix the error would be changing
int setOrderAndCoeff()
to
void setOrderAndCoeff(){
int i;
cout << "Please enter the order of the polynomial: ";
cin >> order;
coeff = new int[order + 1];
for (i = order; i >= 0; i--)
{
cout << "Please enter the coefficient of x^" << i << " :";
cin >> coeff[i];
}
}
similar changes are required to Display member method in the class.

Overloading + operator with classes containing array pointers (C++)

I am currently writing a "Polynomial" class in order to practice Operator Overloading. I have successfully overloaded the stream extraction and insertion operators, but I am having some trouble with the "+" operator.
My class has a private pointer that proceeds to create an array in the constructor. I understand how one would overload the "+" operator with a complex number class, for example, but I'm getting confused with this program.
Guidance in finding a solution would be greatly appreciated.
Thank you.
#include<iostream>
#include<stdexcept>
using namespace std;
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
class Polynomial
{
friend istream& operator>>(istream& in, Polynomial& p);
friend ostream& operator<<(ostream& out, const Polynomial& p);
public:
Polynomial(int = 10);
~Polynomial();
void assignExponent();
Polynomial operator+(const Polynomial& other);
private:
int SIZE;
int *exponents;
int *polyPtr; //***************
};
#endif
//CONSTRUCTOR
Polynomial::Polynomial(int arraySize)
{
if(arraySize > 0)
SIZE = arraySize;
else
{
cout << "Array size must be greater than 0. Program will now "
<< "Terminate..." << endl;
system("pause");
exit(0);
}
polyPtr = new int[SIZE]; //*********************
exponents = new int[SIZE];
for(int i = 0; i<SIZE; i++)
polyPtr[i] = 0;
assignExponent();
};
//DESTRUCTOR
Polynomial::~Polynomial() //******************
{
delete [] polyPtr;
};
//STREAM INSERTION
istream& operator>>(istream& in, Polynomial& p)
{
for(int i = 0; i<p.SIZE; i++)
{
in >> p.polyPtr[i]; //*************
}
return in;
};
//STREAM EXTRACTION
ostream& operator<<(ostream& out, const Polynomial& p)
{
int exponent;
for(int i = 0; i<p.SIZE; i++)
{
exponent = (p.SIZE - 1) - i;
if(p.polyPtr[i] != 1)
{
if(exponent > 0 && exponent != 1)
out << p.polyPtr[i] << "x^" << exponent << " + ";
if(exponent == 1)
out << p.polyPtr[i] << "x" << " + ";
if(exponent == 0)
out << p.polyPtr[i];
}
//In order to not display coefficient if = 1
else
{
if(exponent > 0 && exponent != 1)
out << "x^" << exponent << " + ";
if(exponent == 1)
out << "x" << " + ";
if(exponent == 0)
out << p.polyPtr[i];
}
}
return out;
};
//Assigns a value for exponent
void Polynomial::assignExponent()
{
for(int i = 0; i<SIZE; i++)
{
exponents[i] = (SIZE - 1) - i;
}
};
//OVERLOAD OF + OPERATOR
Polynomial Polynomial::operator+(const Polynomial& other)
{
Polynomial sum(SIZE);
int difference;
//If the first polynomial is larger
if (SIZE > other.SIZE)
{
difference = SIZE - other.SIZE;
for(int i = 0; i<SIZE; i++)
{
if(i - difference < 0)
sum.polyPtr[i] = polyPtr[i];
else
{
sum.polyPtr[i] = polyPtr[i] +
other.polyPtr[i - difference];
}
}
}
//If the second polynomial is larger **PROBLEM**
if(other.SIZE > SIZE)
{
difference = other.SIZE - SIZE;
for(int i = 0; i<other.SIZE; i++)
{
if(i - difference < 0)
sum.polyPtr[i] = other.polyPtr[i];
else
{
sum.polyPtr[i] = other.polyPtr[i] +
polyPtr[i - difference];
}
}
}
//If the polynomials are equal
if(SIZE == other.SIZE)
{
for(int i = SIZE-1; i >= 0; i--)
{
sum.polyPtr[i] = polyPtr[i] + other.polyPtr[i];
}
}
return sum;
};
int main()
{
int polySize;
//User enters a size for the first & second polynomial
cout << "Enter a size for the first polynomial: ";
cin >> polySize;
Polynomial pOne(polySize);
cout << "\nEnter a size for the second polynomial: ";
cin >> polySize;
Polynomial pTwo(polySize);
//User enters in values (Overload of >> operator
cout << "\n\nEnter in values for the first polynomial, "
<< "in the format - (x x x x): " << endl;
cin >> pOne;
cout << "\nEnter in values for the second polynomial, "
<< "in the format - (x x x x): " << endl;
cin >> pTwo;
//Overload << operator for output
cout << "\nPolynomial 1 is: " << pOne << endl
<< "Polynomial 2 is: " << pTwo << endl;
Polynomial pThree = pOne + pTwo;
cout << "\nAfter being added together, the new polynomial is: "
<< pThree << endl;
system("pause");
}
I have updated my current code because I did not think opening another question would be the best way to go. Anyway, in trying to line up my polynomial's to be added, I've partially succeeded. The only problem that arises is when the second polynomial, pTwo, is larger than the first. I've labeled the section of code, PROBLEM. Thanks in advance.
I guess the + should write like below:
class Polynomial
{
// ...
Polynomial operator+(const Polynomial& other);
// ...
};
Polynomial Polynomial::operator+(const Polynomial& other)
{
Polynomial sum(SIZE);
for(int i = 0; i< SIZE; i++)
{
sum.polyPtr[i] = polyPtr[i] + other.polyPtr[i];
}
return sum;
}

Why isnt my simpleVector.cpp linking? (starting out with c++)

i got the .cpp and .h straight from the book, it seems as if nothing is missing, but im getting these linking errors....
#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include <iostream>
#include <new> // needed for bad__alloc exception
#include <cstdlib> // needed for the exit function
using namespace std;
template <class T>
class simplevector
{
private:
T *aptr;
int arraysize;
void memerror(); // handles mem aloc errors
void suberror(); // handles subscripts out of range
public:
simplevector() // default constructor
{ aptr = 0; arraysize = 0; }
simplevector(int); // constructor
simplevector(const simplevector &); // coppy constructor
~simplevector();
int size()
{ return arraysize; }
T &operator[](const int &); // overloaded [] operator
};
template <class T>
simplevector<T>::simplevector(int s)
{
arraysize = s;
// allocate memory for the array
try
{
aptr = new T [s];
}
catch (bad_alloc)
{
memerror();
}
// initialize the array.
for (int count = 0; count < arraysize; count++)
*(aptr + count) = 0;
}
template <class T>
simplevector<T>::simplevector(const simplevector &obj)
{
arraysize = obj.arraysize;
aptr = new T [arraysize];
if (aptr == 0)
memerror();
for(int count = 0; count < arraysize; count++)
*(aptr + count) = *(obj.aptr + count);
}
template <class T>
simplevector<T>::~simplevector()
{
if (arraysize > 0)
delete [] aptr;
}
template <class T>
void simplevector<T>::memerror()
{
cout << "ERROR: Cannot allocate memory.\n";
exit(0);
}
template <class T>
T &simplevector<T>::operator [](const int &sub)
{
if (sub < 0 || sub >= arraysize)
suberror();
return aptr[sub];
}
#endif
This program demonstrates the simplevector template:
#include <iostream>
#include "simplevector.h"
using namespace std;
int main()
{
simplevector<int> inttable(10);
simplevector<float> floattable(10);
int x;
//store values in the arrays.
for (x = 0; x < 10; x++)
{
inttable[x] = (x * 2);
floattable[x] = (x * 2.14);
}
//display the values in the arrays.
cout << "these values are in inttable:\n";
for (x = 0; x< 10; x++)
cout << inttable[x] << " ";
cout << endl;
cout << "these values are in floattable:\n";
for (x = 0; x< 10; x++)
cout << floattable[x] << " ";
cout << endl;
//use the standard + operator on array elements.
cout << "\nAdding 5 to each element of inttable"
<< " and floattable.\n";
for (x = 0; x< 10; x++)
{
inttable[x] = inttable[x] + 5;
floattable[x] = floattable[x] + 1.5;
}
//display the values in the array.
cout << "These values are in inttable:\n";
for (x = 0; x< 10; x++)
cout << inttable[x] << " ";
cout << endl;
cout << "These values are in floattable:\n";
for (x = 0; x< 10; x++)
cout << floattable[x] << " ";
cout << endl;
// use the standard ++ operator on array elements.
cout << "\nIncrementing each element of inttable and"
<< " floattable.\n";
for (x = 0; x< 10; x++)
{
inttable[x]++;
floattable[x]++;
}
//display the values in the array.
cout << "These values are in inttable:\n";
for (x = 0; x< 10; x++)
cout << inttable[x] << " ";
cout << endl;
cout << "These values are in floattable:\n";
for (x = 0; x< 10; x++)
cout << floattable[x] << " ";
cout << endl;
return 0;
}
It's right there in the errors you posted in the OP comments - you declared simplevector::suberror without ever defining it.
Where are the guts of suberror()? I don't see that in the class implementation.