【问题标题】:request for member nci is ambiguous对成员 nci 的请求不明确
【发布时间】:2014-07-10 05:35:34
【问题描述】:

我的助理教授班派生自两个班级:enseigner 班(派生自个人)和 PHD 学生(派生自 Student 班和学生班 dervie 派生自 peronal) 我收到以下错误

“对成员 nci 的请求不明确”

nci 是personal 的私有成员

enter code here

#include<iostream>
using namespace std;
#include "enseigneer.h"
#include "assistant_professor.h"
#include "associate_professor.h"
#include "professor.h"

void assistant_delete(assistant_professor**head_assis,assistant_professor **tail_assis)
{
  int number;int pos=0;int sz=0;
    assistant_professor *temp;
    assistant_professor *search_1;
    assistant_professor *current;
    assistant_professor *to_free;
    temp=*head_assis;
    current=*head_assis;
    cout<<"Enter the CIN number of the enseigner that you want to delete\n ";
    cin>>number;
  if(head_assis==NULL)cout<< "The list is empty you have to enter data first\n";
  else
   {
    while (search_1!=NULL)
       {
        search_1=search_1->next;sz=sz+1;
       }
   if ((*head_assis)-> nci==number)
    {
     to_free=*head_assis;
     (*head_assis)=(*head_assis)->next;
     delete (to_free);
     cout<< "\nThe student was deleted\n";
    }
   else if ((*tail_assis)->nci==number)
    {

     for (int i=0;i<sz-3;i++)
     current=current->next;
     delete(current->next);
     current->next=NULL;
     *tail_assis=current;
     cout<< "\nThe enseigner was deleted\n";
    }

  else
   {


      {
     while((temp!=NULL) and ((temp->nci)!=number))
      {
        temp=temp->next;pos=pos+1;
      }
    if (temp->next==NULL) cout<<"Please pay attention the enseigner you've entred        doesn't exist in the list\n";
      else
        {
           for (int i=0;i<pos-1;i++)
           current=current->next;
           to_free=current->next;
           current->next=current->next->next;
           delete(to_free);cout<< "\nThe enseigner was deleted\n";
        }
      }

【问题讨论】:

  • 这是模棱两可的,因为您的助理教授有两个个人类型的基本子对象。但是你忘了问一个问题。
  • 问题是如何解决这个错误,它是多重继承! “对成员 nci 的请求不明确”

标签: c++


【解决方案1】:

这个问题:Diamond inheritance (C++) 可能会帮助您了解发生了什么。您在两个继承路径中使用派生自 Personal 的类的实例,因此包括两个 nci 副本。因此,当简单地引用nci 时,您的类实例不知道要使用nci 的哪个副本。

有许多技术可以解决这个问题,但它们取决于您的代码结构。考虑阅读多重继承、菱形继承和虚拟继承。众所周知,这样的结构很难做好。

【讨论】:

    猜你喜欢
    • 2010-11-21
    • 2021-01-25
    • 2020-10-14
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    相关资源
    最近更新 更多