Sign in to follow this  
natec_k

getting an ") expected" error message. help please

Recommended Posts

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");
}

Share this post


Link to post
Share on other sites
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");
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this