Hi, I'm trying to add a char to an array but I'm getting some errors. The client part was written by my instructor so I'm not supposed to make changes to it but the problem has to lie somewhere in my class function or the prototype itself.
Starting at one = one + 'A' is where the errors are coming from.
#include "bag.h"
int main (int argc, constchar * argv[])
{
cout << "Welcome to the bag operator driver" << endl;
// *** Create two bags.
Bag two("abbcccdddd1");
Bag one();
// *** testing << with cout.
cout << "******* cout test: \n";
cout << one << two;
// *** testing + to add single chars to a bag
one = one + 'A';
one = one + 'B';
one = one + 'C';
cout << "******* single char addition test: \n";
cout << one ;
Here's the prototype found in the class function of the .h file Bag operator+( char Y); //add a char to the array
And here's my function in the class..
1 2 3 4 5 6 7 8 9 10
Bag Bag::operator+( char Y)
{
Bag Temp = *this;
Temp.contents[num++] = Y;
return Temp; //Return copy of local variable.
}
Here's the errors I'm getting. I'm not sure how to fix them.
~/cs2020c$ g++ prog5.cpp bag.cpp
prog5.cpp: In function 'int main(int, const char**)':
prog5.cpp:25: warning: the address of 'Bag one()', will always evaluate as 'true'
prog5.cpp:28: error: pointer to a function used in arithmetic
prog5.cpp:28: error: assignment of function 'Bag one()'
prog5.cpp:28: error: cannot convert 'Bag (*)()' to 'Bag ()()' in assignment
prog5.cpp:29: error: pointer to a function used in arithmetic
prog5.cpp:29: error: assignment of function 'Bag one()'
prog5.cpp:29: error: cannot convert 'Bag (*)()' to 'Bag ()()' in assignment
prog5.cpp:30: error: pointer to a function used in arithmetic
prog5.cpp:30: error: assignment of function 'Bag one()'
prog5.cpp:30: error: cannot convert 'Bag (*)()' to 'Bag ()()' in assignment
prog5.cpp:32: warning: the address of 'Bag one()', will always evaluate as 'true'
//
//
//
//
//
//
//
//
// lines that begin with // *** are "real" comments
// lines with just // are for you to uncomment as you implement the functions
#include "bag.h"
int main (int argc, constchar * argv[])
{
cout << "Welcome to the bag operator driver" << endl;
// *** Create two bags.
Bag two("abbcccdddd1");
Bag one();
// *** testing << with cout.
cout << "******* cout test: \n";
cout << one << two;
// *** testing + to add single chars to a bag
one = one + 'A';
one = one + 'B';
one = one + 'C';
cout << "******* single char addition test: \n";
cout << one ;
// *** testing + to add two bags together
// one = one + two;
cout << "******* *** testing + to add two bags \n";
// cout << one ;
// *** testing - to remove a char instance from a bag
// two = one - 'd';
cout << "******* *** testing - to remove a 'd' from the bag \n";
// cout << two ;
// *** testing - to remove the first element
// two = one - 'A';
cout << "******* *** testing - to remove the first element " << endl;
// cout << two ;
// *** testing - to remove a char NOT in the bag
// two = two - 'Q';
cout << "******* *** testing - to remove a Q (not in bag) \n";
// cout << two ;
// *** testing hasA using a char NOT in the Bag
cout << "****** *** testing hasA. Is there a Z in this Bag?\n";
// cout << " " << one;
// if ( one.hasA('Z' ) )
// cout << " Yes. the bag has a Z\n";
// else
// cout << " No. the bag does not have a Z\n";
// *** testing hasA using a char n the Bag
// one = one + 'Z';
// cout << "****** *** testing hasA. Is there a Z in this Bag?\n";
// cout << " " << one;
// if ( one.hasA('Z' ) )
// cout << " Yes. The bag has a Z\n";
// else
// cout << " No. THe bag does not have a Z\n";
// *** testing getCount
cout << "****** *** testing getCount on Bag\n";
// cout << "Bag contents: " << one;
// cout << "The count is: " << one.getCount() << endl;
return 0;
}
// bag.h
//
//
#include <iostream>
#include <string>
usingnamespace std;
class Bag {
friend istream &operator>>( istream &input, Bag &Robj );
friend ostream &operator<<( ostream &output, Bag &Robj );
public:
Bag ();
Bag(string);
Bag operator+( char Y); //add a char to the array
Bag operator+(Bag &Robj);//add two arrays together
// Bag operator-(Bag Robj, char Z);
private:
char contents[1024];
int num;
void compact();
bool hasA(char X); //determines if the char is in the array (somewhere)
};
//
// bag.cpp
//
//
#include "bag.h"
Bag::Bag( )
{
num = 0;
}
/* Constructor, secondary.
fill a bag up with the characters from a string.
*/
Bag::Bag(string source) {
for (int i=0; i < (int)source.length(); i++)
contents[i]=source[i];
num = (int)source.length();
}
/*
isAlNum determines if a character is alphanumeric.
called from Bag:compact
*/
bool isAlNum( char X) {
if ( X >= 'A' && X <= 'Z') returntrue;
if ( X >= 'a' && X <= 'z') returntrue;
if ( X >= '0' && X <= '9') returntrue;
returnfalse;
}
/* compact the Bag's internal array by removing all non alphnumeric chars
up to position num
*/
void Bag::compact() {
int last = num-1;
for (int i=num-1; i>=0; i--) {
if ( !isAlNum( contents[i] ) ) {
contents[i] = contents[last];
last--;
}
num = last+1;
}
}
bool Bag::hasA (char X)
{
int i = 0;
for (i = 0; i < 1024; i++)
{
if (contents[i] == X)
returntrue;
}
returnfalse;
}
Bag Bag::operator+( char Y)
{
Bag Temp = *this;
Temp.contents[num++] = Y;
return Temp; //Return copy of local variable.
}
Bag Bag::operator+(Bag &Robj)
{
Bag Temp = *this;
int i = 0;
for (i = 0; i < num; i++)
Temp.contents[Temp.num + i] = Robj.contents[i];
return Temp;
}
//Bag Bag::operator-(Bag Rojb; char rchar)
I'm not seeing the implementation of the stream operators, so even when you get it to compile, the linker will probably still have a problem with the code. I'd hate to quote myself, but try:
I'm not seeing the implementation of the stream operators, so even when you get it to compile, the linker will probably still have a problem with the code. I'd hate to quote myself, but try:
I changed it to that but I get the same errors. I currently have it saved like that but when I changed it after I copy and pasted.
What problems to do you see what the stream operators? Sorry, I'm very new to overloading constructors.
...That's hard to believe. Without the parentheses, you should no longer be telling the compiler that "one" is a function, so there shouldn't be any errors about using function pointers...