【问题标题】:c++ undefined reference in extraction operator overloadingc ++提取运算符重载中的未定义引用
【发布时间】:2016-01-16 08:11:33
【问题描述】:

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

ma​​in.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;
}*/

第一季度。为什么我们需要在单独的命名空间中创建头文件和 Person.cpp? 猜猜:是不是因为 cout 只是表示全局命名空间,然后我们又遇到了一个重载的 cout,所以编译器不确定会调用哪个定义?

第二季度。通过在 main 的 foo 命名空间中创建 Person 类的对象 p 并调用 std::cout

错误:

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

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

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

【问题讨论】:

  • 每个问题一个问题。

标签: c++ namespaces operator-overloading


【解决方案1】:

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

在命名空间foo中定义它:

namespace foo
{
    // definition
}

【讨论】:

  • OP 还问了另外两个问题。
  • @zenith thnx 它在年龄公开时有效,但如果它是私有的,则会给出错误 int foo::Person::age 是私有的,朋友函数也应该可以访问私有成员?
  • @zenith 将年龄和名称声明为私有时出错:int foo::Person::age 是私有的; std foo::Person::age 是私有的;这两行中的 in>>p.age 和 in>> p.name 错误。
  • @zenith 在您的链接中,您没有将年龄和姓名声明为私有。
  • @Rooney10 in &gt;&gt;,你说的是另一个运营商operator&gt;&gt;。也声明friend
【解决方案2】:

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

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

而不是

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

并在声明它的命名空间中定义你的类方法

#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;
 }

【讨论】:

  • 感谢回答,公开后解决。将年龄和姓名声明为私有时出错:int foo::Person::age 是私有的; std foo::Person::age 是私有的;这两行中的 in>>p.age 和 in>> p.name 错误。
  • 朋友关键字必须处理私人会员访问
  • 它必须处理它......但是它给出了上述错误,它与命名空间访问冲突有关吗?
  • 你的意思是in &gt;&gt; p.age &gt;&gt; p.name;产生错误
  • 是的.. 它指向那条线以及名称和年龄被声明为私有的那条线
猜你喜欢
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 2020-09-15
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多