-2

I have an error with the class i wrote and i dont understad what is the problem.

this is worker class:

#ifndef __Worker__
#define __Worker__

#include <iostream>
#include <string> 

class Worker{
    protected:
        std::string name;
        int id;
        int salary;

    public:
        Worker(const std::string& name, int id, int salary);
        Worker(const Worker& worker );
        friend std::ostream& operator<<(std::ostream& os, const Worker& w);
        int getWorkerID(){
            return this->id;
        }
        int getsSalary(){
            return this->salary;
        }
        std::string getname(){
            return this->name;
        }

};
#endif

and this is the implemention of this class:

#include "Worker.hpp"

Worker::Worker(const std::string &names, int ids, int salarys) :
name(names), id(ids), salary(salarys)
{
}

Worker::Worker(const Worker &worker): 
name(worker.name), id(worker.id), salary(worker.salary)
{

}


std::ostream& Worker::operator<<(std::ostream& os, const Worker& w)  
{  
    os << "worker name:" << w.getname() << " id:" << std::to_string(w.getWorkerID()) << " salary:"<<std::to_string(w.getsSalary()) ;  
    return os;  
}

some one can tell me please what worng with operator<< that i wrote?

Elior Sastiel
  • 1,074
  • 2
  • 10
  • 21
  • 5
    A friend is not a member, so just remove the `Worker::` prefix. – Bo Persson Nov 12 '17 at 08:37
  • s/`Worker::operator<<`/`operator<<` – user0042 Nov 12 '17 at 08:38
  • 1
    Possible duplicate of [operator << must take exactly one argument](https://stackoverflow.com/questions/10744787/operator-must-take-exactly-one-argument) – HMD Nov 12 '17 at 08:38
  • Unrelated to your question and problem, but symbols beginning with two underscores (like `__Worker__`) are reserved in all scopes. Please read [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for more details. – Some programmer dude Nov 12 '17 at 08:39
  • @BoPersson I removed the "Worker::" and it still not work. – Elior Sastiel Nov 12 '17 at 08:42
  • 1
    @EliorSastiel: Yes, it still doesn't work, but the error message will be completely different! Please don't neglect to mention such an important fact when you say that it "still doesn't work". My answer explains how to fix those other errors as well. – Christian Hackl Nov 12 '17 at 08:44

1 Answers1

8

This line in your implementation code:

std::ostream& Worker::operator<<(std::ostream& os, const Worker& w)  

Should be:

std::ostream& operator<<(std::ostream& os, const Worker& w)  

That's because Worker::operator<< would be a member function, not a non-member friend function as you correctly declare it in the header file, and if it's a member function, then error message means exactly what it means.

Once you've fixed this error, you will find out that further errors appear, because your getter member functions aren't const. Another error to be fixed is the __Worker__ macro. A name like that is reserved for the implementation, and it's strange to have a macro with lowercase letters in its name; make it something like WORKER_H.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62