This topic is 4845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi Im trying to learn operator overloading. This code here is fine.
void operator = (char* str)
{
m_str = str;
}


But this code errors out saying error in function declaration; skipping function body, does anyone know why. Im a retard when it comes to this. Thanks.
char* operator = (void)
{
return m_str;
}

char* operator << (void)
{
return m_str;
}



##### Share on other sites
operator=() and operator<<() are binary operators, you have to have something on the right hand side in order to use the = sign or <<. By declaring operator=(void) you try to declare operator=() as a unary operator. I'm not really sure what you are trying to accomplish with those two functions so I can't say how to fix them.

##### Share on other sites

char* str = Class;

OR

std::cout << Class;

##### Share on other sites

Edit: Sorry, misunderstood what you were trying to do

##### Share on other sites
how does the string class do it then

std::string str = "Text";

std::cout << str;

##### Share on other sites
For the first one, you need to create a conversion operator. It looks like:

class Class {  public:    operator char *() { return str; }        char * str;};

That being said, providing conversion operator are generally a bad idea. Implicit conversions can bite you in the rear. Prefer to write a member function that returns a pointer instead.

For the second one, you need a non-member overload of operator<< since the object on the left hand side is a std::ostream. It looks like:
std::ostream & operator<<(std::ostream & lhs, Class & rhs) {  lhs << /* whatever */}

You may need to friend the function to get the insides to compile.

##### Share on other sites
To overload the stream insertion operator (the << operator) you will need to declare it as:

ostream& operator<<(ostream&, ClassName);

Typically this declaration is used within a class, allowing the user to print out various class information / data.

Now when you define the overloaded operator function, its defined like this:

ostream& operator<<(ostream& osObject, ClassName& name){   osObject << "Whatever info you want printed out here.";}

##### Share on other sites
Quote:
 Original post by cptrnetstd::string str = "Text";

This one is done by providing a constructor that takes a const char * argument, it has nothing to do with operator overloading. Though

std::string str;
str = "Text";

Is done by providing an overload of operator=() that takes a const char * argument like the first thing in your original post.

##### Share on other sites
Cool thanks alot SiCrane you've helped out of so many jams, And thanks to the other posters I will take a look at the page about overloading.

1. 1
Rutin
64
2. 2
3. 3
4. 4
5. 5

• 16
• 10
• 29
• 20
• 9
• ### Forum Statistics

• Total Topics
633413
• Total Posts
3011759
• ### Who's Online (See full list)

There are no registered users currently online

×