简体   繁体   中英

How to define functions properly and implement them?

This structure resembles my code, I get an error: prototype for int A::getA() const does not match any in class A. My other problem is the operator<< overloading. I can not get it to work properly and I get an explicit qualification in declaration error. I tried getting the .hpp in a namespace because I saw something similar in another question posted here but this did not help. If you give me a solution me can you also provide me with details as to why my code is breaking?

//define.hpp

class A{
...

int getA() const;
int getAa() const;

};

ostream& operator<<(ostream& out, const A& obj); // defined outside of the class



//implement.cpp

ostream& define::operator<<(ostream& out, const A& obj){
    return out << obj.getA() 
                << obj.getAa()
                << endl;
};

int A::getA() const{ ... };
int A::getAa() const{ ... };

int main(){

    return 0
}

Functions in the global scope, like your operator<< function, does not need to be scoped. So skip the define:: part of the definition:

ostream& operator<<(...) { ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM