Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
C++ must be a nonstatic member function?
friend bool operator= (const char*,const S&);
friend bool operator= (char, const S&);
I'm getting a compiler error saying that both of these must be nonstatic member functions. Can someone explain what that is and/or how to fix it?
2 Answers
- ?Lv 79 years agoFavorite Answer
There are two types of static functions:
a) declared as static
i.e. static void say_hello()
{
cout << "Hello!";
}
b) declared as friend
The friend function is a static function as well with the privilege to access the private members of the object.
The member function (or method) cannot be called outside the class.
i.e.: class B{
public:
void hello()
{
cout << "Hello!";
}
};
Here you have to create an instance of B in order to have access to he hello() mehtod;
B b;
b.hello(); // in case the public modifier would be replaced by private, you wouldn't have access to that method at all from outside the class (unless through a friend function!!!!)
Because your operator overload returned bool I thought that you could refer to the comparison operator== rather than the assignment operator=.
That is why I provide explanations for both:
The quotes (13.5.3 Assignment [over.***]) come from C++2012Draft-n3337.
class S {
public:
// operator==
friend bool operator==(char *sz, const S&);
friend bool operator==(char, const S&);
friend bool operator==(const S&, const S&);
bool operator==(const S&);
bool operator==(const char *);
bool operator==(char);
// operator=
//13.5.3 Assignment [over.***]
//1 An assignment operator shall be implemented by a non-static member function with exactly one parameter.
S& operator=(char);
S& operator=(S&);
S& operator=(char *);
};
int main()
{
S a;
return 0;
}
Source(s): Here is some code sample with output: http://ideone.com/xJWh9 - oopsLv 69 years ago
First, a question for you. Why are you returning a bool from an assignment operator? That's odd, and it makes me think that you intended to write a comparison operator. That is, operator==, rather than operator=. Is that the case? If so, fix it, just add a second = to each of them. You don't have to worry about anything I say below, because the restrictions on operator= do not apply to operator==.
Otherwise:
It's saying that operator= must be written as a member of a class, and it must not be static (operator= is a very special function in C++, it has special rules). You appear to be trying to define a global operator=. You can't do that. Unfortunately, you also can't make them member functions, since you can't write member functions for built-in types. So, the functions you are trying to create are impossible.
However, you can do something to achieve a similar effect, assuming you are in control of the S class.
    class S
    {
    public:
        operator char() const {
            // do conversion and return char from this function
        }
        operator const char*() const {
            // do conversion and return const char* from this function
        }
    };
This will allow implicit conversion from your class S to char or to const char*. So you can use the built-in assignment operator. Though the return value wont be bool, it will be the char or const char* that you assigned to.