Click here to Skip to main content
15,937,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello. I have the task to overload operator + between a char* and an integer like this:

char* operator+(const int k, char* c)
	for (int i = 0; i<strlen(c); i++)
		c[i] += k;
	return c;

int main()
	char *c = new char[10];
	cin >> c;// abc
	c = c + 2;
	cout << c;// cde
	return 0;

error C2803: 'operator +' must have at least one formal parameter of class type

any ides how and why?

What I have tried:

i solved it by writing it into a class like this

char* operator+(int k)
int i;
for (i = 0; i < strlen(c); i++)
c[i] += key;
return c;

but this works only for c=c+2; any ideas and only inside that class and that is not the task i have...
CPallini 6-Feb-16 15:14pm    
As already suggested by others, C++ doesn't allow to re-define operators on builtin data types. Why don't you use an ordinary function for the purpose?

I don't think you want to do that: it's very counter-intuitive.
Normally, when you add an integer and a pointer, you get a pointer that has moved on within the memory pointed to by the original pointer:
char *c = new char[10];
cin >> c;  // abcdefg
c = c + 2;
cout << c; // cdefg
And defining an identical operator than subverts that and actually changes memory is going to make your code very, very hard to maintain.
I'd strongly suggest that you don't do this, but define a simple function called IncrementMemory and call that instead.
Share this answer
Member 12147362 6-Feb-16 11:30am    
unfortunately this task is part of a oop problem i have to solve for my oop class and this is what it asks i didn't come with it.
OriginalGriff 6-Feb-16 11:42am    
I'd re-read the question if I was you - it's very unlikely!
Stefan_Lang 8-Feb-16 2:44am    
I agree with OG.
1. it's not possible to define such an operator
2. Even if it were possible, it's bad, because it's counter-intuitive: adding an integer to a pointer is well-known as pointer arithmetic. It's expected to change the pointer, not the data!
3. Also it is not well- defined if used on a C-style string. E. g. "abc" + (-'a') would result in an empty string, because the first character would be transformed into the terminating 0-byte! This would also lead to an inconsistent behaviour regarding associativity, because "abc"+((-'a')+('a')) would be the identity, while ("abc"+(-'a'))+('a') would be an empty string.

Besides, the expression c+2 would invoke operator+(char*, int), not the one you defined. The order of operands is important!
If that is the exact wording of the question then it's a trap. C++ does not allow to override the behavior of arithmetic operators between predefined types, for example int and char*. That's what the error message C2803 is telling you, and the compiler is correct in this case.

So what can you do about it? You have to make one of the parameters a "class type" as the message says. Your parameter c is a pointer to a null-terminated character string and is the ideal candidate for replacement. For example, we could replace it with an std::string reference.
 #include <string>
 using namespace std;

string& operator+ (const int k, string& c)
	size_t len = c.length();
	for (size_t i = 0; i < len; i++)
		c[i] += k;
	return c;

Of course you had to change your main function as well.

And after all, as Griff said, this is not a very good idea in the first place and I don't see the purpose of this exercise.
Share this answer
Stefan_Lang 8-Feb-16 8:46am    
That char string in fact *needs* to be replaced by a class: otherwise you'd risk losing part of your string if you happen to modify one of the characters to 0...

Otherwise a good solution (but there's something amiss with the code formatting)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900