getting an ") expected" error message. help please

Started by
7 comments, last by natec_k 13 years, 4 months ago
trying to write a simple operator overload statement bu it will not compile because of the above mentioned error. am not sure what am doing wrong. can someone help me:


#include <conio>
#include <iostream>
#include <stdlib>

class opover{
private:
char* ab,cd;
public:
operator+(char* ab &c1,cd &c2); //error here//
};
void opover::operator+(char* ab &c1,cd &c2){ //error here//
opover temp;
temp=c1.ab+c2.cd;
return temp;
}
void main(){
opover s1("hello");
opover s2("yute");
s1+s2;
system("pause");
}
Advertisement
This doesn't look like any variable declaration I've ever seen:

char* ab &c1

What are you trying to do exactly? What are ab,cd,c1,and c2?
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
ok, what i'm trying to do is to over load the plus operator so that it concatenates the string variables loaded in ab and cd. the c1 and c2 are sposed to be objects. i was trying to create the objects wihtin the declaration. i changed it below, hope fully this looks more like what i want to accomplish. i really dont think am doing it right.




#include <conio>
#include <iostream>
#include <stdlib>

class opover{
private:
char* ab,cd;
public:
void operator+(char* ab,cd);
};
void opover::operator+(char* ab,cd){
opover c1;
opover c2;
opover temp;
temp=c1.ab+c2.cd;
return temp;
}
void main(){
opover s1("hello");
opover s2("yute");
s1+s2;
system("pause");
}
The class you are looking for is called std::string, and it's already part of the standard library, with operator+ and all.

<conio> is not a standard header, so I suggest you don't use it.

char* ab,cd is very confusing. ab is a pointer but cd is not. You should probably put the space before the `*', so this is more clear. Other people prefer to never declare more than one variable in one line.

There are too many other mistakes in your code, which means that you are too confused. What are you trying to do exactly? Is this homework?
First off, use std::string.

Secondly, function declarations require you to indicate the variable type for all variables.
void foo(char* ab, cd);
is not valid. You have to specify the types on all variables, like so:
void foo( const char *ab, const char *cd );

Thirdly, operator+ should be defined as
foo foo::operator +( const bar &b );

Next up, what are you actually "adding"? Hint: it isn't char*'s.
Your final declaration for the operator overload should look like:
#include <string>class opoverTest{  public:    opoverTest( const std::string &val ) :      value(val)    {    }    opoverTest operator+ (const opoverTest &b ) const    {       return opoverTest( value + b.value );    }//...//  private:     std::string value;//...};


Ok, so what's left to fix?
s1+s2 in your main function doesn't do anything. You should probably assign it to something and output it. Something like:
opoverTest s3 = s1 + s2;std::cout << s3.value << std::endl;

And, you probably shouldn't be using system("pause") at the end of your program. Prefer not to do that at all. "But then my program instantly closes!". Yes, but that's what a console program is supposed to do. Most IDEs will insert pause code for you in debug mode. Otherwise, learn to run the application from the command line instead of double-clicking the exe in explorer.

So, I changed your char * to a std::string. Without that, there is a LOT more work. You can't do "c1.ab+c2.cd" with char*'s, you'd have to allocate a new char* with malloc, and strcat the two strings together into the new location. But, std::string already has operator+ overloaded, so in the end, you wouldn't want to wrap it up like this anyway. But it's decent practice to try this out anyway.

[Edited by - KulSeran on December 11, 2010 4:32:22 PM]
KulSeran's post is correct, except for the fact that the operator+ should return by value, not by reference.
Quote:Original post by alvaro
KulSeran's post is correct, except for the fact that the operator+ should return by value, not by reference.


And also the fact that the class member binary operator takes two parameters, does not use the data in the this instance and is not const.
Fixed! (i hope). Thats what I get for typing things out without thinking too hard about it. sorry.
thanx everyone. it is homework, and i'm still confused by the way. but thanx anyway

This topic is closed to new replies.

Advertisement