Quick Question

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "bag.h"
 
int main (int argc, const char * 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'





Thanks!
Last edited on
closed account (3hM2Nwbp)
1
2
3
// Bag one();
// to
Bag one;


It'd be helpful if the whole source was posted (that aligns with the line #s in your compiler output).
Sorry, here it is:
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
//
// 
// 
// 
// 
//
// 
//
// 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, const char * 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;
}
You're going to have to at least show us the source for Bag, as it's use is where the problem lies.
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
//  bag.h
//
//
  
#include <iostream>
#include <string>
 
using namespace 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)
};
 
 

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
//
//  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') return true;
    if ( X >= 'a' && X <= 'z') return true;
    if ( X >= '0' && X <= '9') return true;
    
    return false;
}
 
/*  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)
				return true;
		}


	return false;
}


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)

	







closed account (3hM2Nwbp)
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:

self wrote:
1
2
3
4
// @ line 21
// Bag one();
// to
Bag one;

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.
Last edited on
What are the errors after you change Bag one(); to Bag one;? The reason the former doesn't work is because it is read as a function prototype.
The errors are exactly the same.


Edit, I went through and made some other changes and I was messing around with it being Bag one() and Bag one; and Bag one ended up working.
Last edited on
...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...
Topic archived. No new replies allowed.