提取运算符重载中的c ++未定义引用

c++ undefined reference in extraction operator overloading

在codeblocks中一点点学习c++,遇到了这个错误。我已经尝试了几个小时,看到了各种链接,但仍然无法正常工作,

main.cpp

#include <iostream> 
#include "Person.h"

using namespace std;

int main()
{
  foo:: Person p;
  //std:: cin >> p;
  std:: cout << p;
  return 0;
 }

Person.h

#ifndef PERSON_H
#define PERSON_H

namespace foo{
class Person
{
   public:
   int age;
   Person();
   friend std:: ostream& operator << (std::ostream&,const Person&);
 //friend std:: istream& operator << (std:: istream& in, Person& p);
   protected:
   private:

  // std::string name;
};

}
#endif // PERSON_H

Person.cpp

#include <iostream>
#include "Person.h"

using namespace foo;

Person::Person()
{
   age = 0;
  //name = "noname";
}


std:: ostream& operator <<(std:: ostream& out, Person const &p)
{
  out << "Extraction operator" << std:: endl << p.age << std:: endl;
  // out << p.name << std::endl;
  return out;
}

/*std:: istream& operator >>(std:: istream& in, Person &p)
{
  in >> p.age >> p.name;
  return in;
}*/

Q1。为什么我们需要在单独的命名空间中创建头文件和 Person.cpp? 猜猜:是不是因为 cout 只是意味着全局命名空间,然后我们再次有一个重载的 cout,所以编译器不确定调用哪个定义?

Q2。通过在 main 中的 foo 命名空间中创建一个 Person class 的对象 p 并调用 std::cout << p,我们想做什么? (std 在 std 命名空间中,我们要调用重载运算符)

错误:

undefined reference to foo:: operator<<(std::ostream&, foo::Person const&)') 

如果我们私下写年龄,它说它是私人的,但作为朋友,它应该可以访问私人成员。我知道是和命名空间有关,但我找不到原因。

no match for operator >> in std::cin 早些时候它给出了上述和许多其他错误,所以我尝试使用两个名称空间但仍然无法正常工作。

您在全局命名空间中定义了 operator<<,但在命名空间 foo 中声明了它。

在命名空间内定义它 foo:

namespace foo
{
    // definition
}

我认为你使用了错误的运算符声明

//friend std:: istream& operator << (std:: istream& in, Person& p);

而不是

friend std:: istream& operator >> (std:: istream& in, Person& p);

并在声明它的命名空间中定义您的 class 方法

#include <iostream>
#include "Person.h"

namespace foo
{

    Person::Person()
    {
       age = 0;
      //name = "noname";
    }


    std:: ostream& operator <<(std:: ostream& out, Person const &p)
    {
      out << "Extraction operator" << std:: endl << p.age << std:: endl;
      // out << p.name << std::endl;
      return out;
    }

    std:: istream& operator >>(std:: istream& in, Person &p)
    {
      in >> p.age >> p.name;
      return in;
    }

}

下面的代码在 MS Visual C++ 上运行完美

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>

namespace foo{
    class Person
    {
       public:
       Person();
       friend std:: ostream& operator << (std::ostream&,const Person&);
       friend std:: istream& operator >> (std:: istream& in, Person& p);
       protected:
       private:
       int age;
       std::string name;
    };
}

namespace foo
{

    Person::Person()
    {
       age = 0;
       name = "noname";
    }


    std:: ostream& operator <<(std:: ostream& out, Person const &p)
    {
      out << "Extraction operator" << std:: endl << p.age << std:: endl;
      out << p.name << std::endl;
      return out;
    }

    std:: istream& operator >>(std:: istream& in, Person &p)
    {
      in >> p.age >> p.name;
      return in;
    }

}

int main()
{
  foo:: Person p;
  std:: cin >> p;
  std:: cout << p;
  _getch();
  return 0;
 }