0

I am trying to use an array to keep track of the totals of different types of items (up to 50 types). When I want to print the totals out, I get an error saying "'+' cannot add two pointers." I'm thinking the problem is with my totals array somehow, but I can't figure it out. Below is a sample of my code:

  string printSolution()
  {
  int totals[50];

  string printableSolution = "";

  for (int k = 0; k < itemTypeCount; k++)
  {
      totals[k] = 0;
  }

  for (int i = 0; i < itemCount; i++)
  {
      totals[items[i].typeCode]++;
  }

  for (int a = 0; a < itemTypeCount; a++)
  {
      printableSolution.append("There are " + totals[a] + " of Item type " + (a + 1) + ". \n");
  }


}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 3
    Wrap the `"There are"` with a `std::string` or use a `std::stringstream`. Right now you're adding `char*` with `int`. – Benjamin Gruenbaum Jun 19 '14 at 00:46
  • When you're working with C-style strings, you have to use `strcat()` to concatenate them. `+` only works with `std::string`. – Barmar Jun 19 '14 at 00:47
  • possible duplicate of [Error: Cannot add two pointers](http://stackoverflow.com/questions/3108965/error-cannot-add-two-pointers) and http://stackoverflow.com/q/16324599/103167 and http://stackoverflow.com/q/18580262/103167 and http://stackoverflow.com/q/2844986/103167 – Ben Voigt Jun 19 '14 at 02:03

3 Answers3

1

The string literals "Foo" are of const char*, i.e. pointer type.

To understand what happens with:

"There are " + totals[a] + " of Item type " + (a + 1) + ". \n"

Let's look at an expression:

"0123456789" + 5 

This actually just offsets 5 bytes from the start, so becomes:

"56789" 

So an expression:

"0123456789" + 5 + "foo"

becomes:

"56789" + "foo"

as pointers, and this is not defined.

What you really want is string concatenation; this can be achieved using std::string.

We can write:

   std::string("56789") + "foo"

and this generates a std::string with value: "56789foo" as you desire.

But:

 std::string("0123456789") + 5 

is also not defined. You need to use:

std::string("0123456789") + std::to_string(5)

So, finally you want:

std::string("There are ") + std::to_string(totals[a]) + " of Item type " + std::to_string(a + 1) + ". \n"    

Note now you do not need to explitly convert all the "" to std:string, as once you have one implicit type conversion will take care of the other operand in operator+. However, adding them would do no harm:

std::string("There are ") + std::to_string(totals[a]) + std::string(" of Item type ") + std::to_string(a + 1) + std::string(". \n")
Keith
  • 6,756
  • 19
  • 23
0

The problem is here:

"There are " + totals[a] + " of Item type " + (a + 1) + ". \n"

It means char* + int + char* + int + char*. You need to print them out separately or change the int to a std::string.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Fanl
  • 1,491
  • 2
  • 11
  • 15
0

Use C++-style formatting instead:

std::ostringstream oss;
oss << "There are " << totals[a] << " of Item type " << (a + 1) << ". \n";
printableSolution += oss.str();
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770